{
dt.Columns.Add(\"id\", typeof(long));
dt.Columns.Add(\"field1\", typeof(string));
dt.Columns.Add(\"field2\", typeof(string));
dt.Columns.Add(\"field3\", typeof(string));
dt.Columns.Add(\"field4\", typeof(string));
dt.Columns.Add(\"field5\", typeof(string));
dt.PrimaryKey = new[] { AliveTable.Columns[\"id\"] };
dataGridView1.DataSource = dt;
dataGridView1.DataError += DataGridView_DataError;
}
public void ListData()
{
foreach(var item in data)
{
if(item.delete)
{
var row = dt.Rows.Find(item.id);
if(row != null) { row.Delete();}
continue;
}
InserOrUpdateData(item.id);
}
}
public void InserOrUpdateData(long id)
{
var data = dataFromDb;
bool exist = dt.AsEnumerable().Any(row => id == row.Field<long>(\"id\"));
var item1 = data [\"field1\"] != null ? data [\"field1\"] : String.Empty;
var item2 = data [\"field2\"] != null ? data [\"field2\"] : String.Empty;
var item3 = data [\"field3\"] != null ? data [\"field3\"] : String.Empty;
var item4 = data [\"field4\"] != null ? data [\"field4\"] : String.Empty;
var item5 = data [\"field5\"] != null ? data [\"field5\"] : String.Empty;
if (exist)
{
dt.Rows.Find(id)[\"field1\"] = item1;
dt.Rows.Find(id)[\"field2\"] = item2;
dt.Rows.Find(id)[\"field3\"] = item3;
dt.Rows.Find(id)[\"field4\"] = item4;
dt.Rows.Find(id)[\"field5\"] = item5;
}
else
{
dt.Rows.Add(id, item1, item2, item3, item4, item5);
}
}
private void DataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show(e.Exception.Message.ToString());
}
And The received Error object(As JSON). Note: The indexes change
{
\"Context\": 2,
\"Exception\": {
\"ClassName\": \"System.IndexOutOfRangeException\",
\"Message\": \"Index 3 does not have a value.\",
\"Data\": null,
\"InnerException\": null,
\"HelpURL\": null,
\"StackTraceString\": \" at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)\",
\"RemoteStackTraceString\": null,
\"RemoteStackIndex\": 0,
\"ExceptionMethod\": \"8\\nGetError\\nSystem.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\\nSystem.Windows.Forms.DataGridView+DataGridViewDataConnection\\nSystem.String GetError(Int32)\",
\"HResult\": -2146233080,
\"Source\": \"System.Windows.Forms\",
\"WatsonBuckets\": null
},
\"ThrowException\": false,
\"ColumnIndex\": 5,
\"RowIndex\": 3,
\"Cancel\": false
}
Uygulamamda bir datagridview kullanıyorum ve datasource olarak da bir DataTable kullanıyorum. Kayıt ekleme, silme ve güncelleme kısımlarında data olup olmadığını kontrol etmeme rağmen, DataError event'inden \"index X does not have a value.\" hatasını alıyorum. Bu hatayı kodlarımın devamında paylaştım. Her türlü fikir benim için faydalı olacaktır.
Şimdiden teşekkür ederim.
DataTable dt = new DataTable();
Form1_load()
{
dt.Columns.Add(\"id\", typeof(long));
dt.Columns.Add(\"field1\", typeof(string));
dt.Columns.Add(\"field2\", typeof(string));
dt.Columns.Add(\"field3\", typeof(string));
dt.Columns.Add(\"field4\", typeof(string));
dt.Columns.Add(\"field5\", typeof(string));
dt.PrimaryKey = new[] { AliveTable.Columns[\"id\"] };
dataGridView1.DataSource = dt;
dataGridView1.DataError += DataGridView_DataError;
}
public void ListData()
{
foreach(var item in data)
{
if(item.delete)
{
var row = dt.Rows.Find(item.id);
if(row != null) { row.Delete();}
continue;
}
InserOrUpdateData(item.id);
}
}
public void InserOrUpdateData(long id)
{
var data = dataFromDb;
bool exist = dt.AsEnumerable().Any(row => id == row.Field<long>(\"id\"));
var item1 = data [\"field1\"] != null ? data [\"field1\"] : String.Empty;
var item2 = data [\"field2\"] != null ? data [\"field2\"] : String.Empty;
var item3 = data [\"field3\"] != null ? data [\"field3\"] : String.Empty;
var item4 = data [\"field4\"] != null ? data [\"field4\"] : String.Empty;
var item5 = data [\"field5\"] != null ? data [\"field5\"] : String.Empty;
if (exist)
{
dt.Rows.Find(id)[\"field1\"] = item1;
dt.Rows.Find(id)[\"field2\"] = item2;
dt.Rows.Find(id)[\"field3\"] = item3;
dt.Rows.Find(id)[\"field4\"] = item4;
dt.Rows.Find(id)[\"field5\"] = item5;
}
else
{
dt.Rows.Add(id, item1, item2, item3, item4, item5);
}
}
private void DataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show(e.Exception.Message.ToString());
}
And The received Error object(As JSON). Note: The indexes change
{
\"Context\": 2,
\"Exception\": {
\"ClassName\": \"System.IndexOutOfRangeException\",
\"Message\": \"Index 3 does not have a value.\",
\"Data\": null,
\"InnerException\": null,
\"HelpURL\": null,
\"StackTraceString\": \" at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)\",
\"RemoteStackTraceString\": null,
\"RemoteStackIndex\": 0,
\"ExceptionMethod\": \"8\\nGetError\\nSystem.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\\nSystem.Windows.Forms.DataGridView+DataGridViewDataConnection\\nSystem.String GetError(Int32)\",
\"HResult\": -2146233080,
\"Source\": \"System.Windows.Forms\",
\"WatsonBuckets\": null
},
\"ThrowException\": false,
\"ColumnIndex\": 5,
\"RowIndex\": 3,
\"Cancel\": false
}
Uygulamamda bir datagridview kullanıyorum ve datasource olarak da bir DataTable kullanıyorum. Kayıt ekleme, silme ve güncelleme kısımlarında data olup olmadığını kontrol etmeme rağmen, DataError event'inden "index X does not have a value." hatasını alıyorum. Bu hatayı kodlarımın devamında paylaştım. Her türlü fikir benim için faydalı olacaktır.
Şimdiden teşekkür ederim.
DataTable dt = new DataTable();
Form1_load()
{
dt.Columns.Add("id", typeof(long));
dt.Columns.Add("field1", typeof(string));
dt.Columns.Add("field2", typeof(string));
dt.Columns.Add("field3", typeof(string));
dt.Columns.Add("field4", typeof(string));
dt.Columns.Add("field5", typeof(string));
dt.PrimaryKey = new[] { AliveTable.Columns["id"] };
dataGridView1.DataSource = dt;
dataGridView1.DataError += DataGridView_DataError;
}
public void ListData()
{
foreach(var item in data)
{
if(item.delete)
{
var row = dt.Rows.Find(item.id);
if(row != null) { row.Delete();}
continue;
}
InserOrUpdateData(item.id);
}
}
public void InserOrUpdateData(long id)
{
var data = dataFromDb;
bool exist = dt.AsEnumerable().Any(row => id == row.Field<long>("id"));
var item1 = data ["field1"] != null ? data ["field1"] : String.Empty;
var item2 = data ["field2"] != null ? data ["field2"] : String.Empty;
var item3 = data ["field3"] != null ? data ["field3"] : String.Empty;
var item4 = data ["field4"] != null ? data ["field4"] : String.Empty;
var item5 = data ["field5"] != null ? data ["field5"] : String.Empty;
if (exist)
{
dt.Rows.Find(id)["field1"] = item1;
dt.Rows.Find(id)["field2"] = item2;
dt.Rows.Find(id)["field3"] = item3;
dt.Rows.Find(id)["field4"] = item4;
dt.Rows.Find(id)["field5"] = item5;
}
else
{
dt.Rows.Add(id, item1, item2, item3, item4, item5);
}
}
private void DataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show(e.Exception.Message.ToString());
}
And The received Error object(As JSON). Note: The indexes change
{
"Context": 2,
"Exception": {
"ClassName": "System.IndexOutOfRangeException",
"Message": "Index 3 does not have a value.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": "8\nGetError\nSystem.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Windows.Forms.DataGridView+DataGridViewDataConnection\nSystem.String GetError(Int32)",
"HResult": -2146233080,
"Source": "System.Windows.Forms",
"WatsonBuckets": null
},
"ThrowException": false,
"ColumnIndex": 5,
"RowIndex": 3,
"Cancel": false
}
Toplam 0 cevap