Я знаю, что это, вероятно, очень просто, я googled, но все еще не вижу свою ошибку.Ссылка на объект при добавлении в datagrid
Мой основной код:
public static void postToImgur(string imagFilePath, string apiKey, string apiSecret, string title, string description)
{
byte[] imageData;
FileStream fileStream = File.OpenRead(imagFilePath);
imageData = new byte[fileStream.Length];
fileStream.Read(imageData, 0, imageData.Length);
fileStream.Close();
const int MAX_URI_LENGTH = 32766;
string base64img = System.Convert.ToBase64String(imageData);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < base64img.Length; i += MAX_URI_LENGTH)
{
sb.Append(Uri.EscapeDataString(base64img.Substring(i, Math.Min(MAX_URI_LENGTH, base64img.Length - i))));
}
string uploadRequestString = "client_id" + apiKey + "client_secret" + apiSecret + "&title=" + title + "&imageTitle" + title + "&description" + description + "&caption=" + title + "img" + "&image=" + sb.ToString();
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/upload.xml");
// needs a space between
webRequest.Headers.Add("Authorization", "Client-ID " + apiKey);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ServicePoint.Expect100Continue = false;
StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream());
streamWriter.Write(uploadRequestString);
streamWriter.Close();
WebResponse response = webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream);
string responseString = responseReader.ReadToEnd();
//received response is a xml file. the link to the uploaded file is in between the tag<link></ link >
//using regular expression to retrive the link to the image.
Regex regex = new Regex("<link>(.*)</link>");
var test = regex.Match(responseString).Groups[1].ToString();
//return regex.Match(responseString).Groups[1].ToString();
dgView.Rows.Add(test);
}
private void btnPostToImgur_Click(object sender, EventArgs e)
{
// validation
postToImgur(txtBoxImageLocation.Text, "ccc8d227d", "5b64c6b63908aa3e51840db3ce874137d7", txtBoxTitle.Text, txtBoxDescription.Text);
}
Я получаю ошибку
ссылка на объект, необходимый для доступа к не-статический метод или свойство
Когда я пытаюсь добавить данные в имя переменной datagrid dgView
, любая помощь будет оценили.
На какой линии точно? –
Попробуйте: dgView.Rows.Add (новый объект [] {test}); метод Add для datagrid должен быть новым массивом объектов. – jdweng