Reputation: 2442
I am getting a datagridview default error dialog box. There it said handle the dataerror event. I am loading the data in myy DGV from a datatable. When I try to add a new row, I click on the empty row with a (*). When I start typing some values in the new row cell, then the dataerror is shown and goes in a infinte loop.
DataGridViewDataError.context = display.
Error: Index 3 does not have a value.
My grid has 3rows and here the new row will have index 3.
Here's the code where the DGV is loaded
private void frmPlant_Load(object sender, EventArgs e)
{
Program.fMain = this;
Program.LoadAllForms();
string selectCommand = "select * from T1";
FillData(selectCommand);
}
private void FillData(string selectCommand)
{
using (SQLiteConnection conn = new SQLiteConnection(connString))
{
conn.Open();
dataGridView1.AutoGenerateColumns = true;
da = new SQLiteDataAdapter("selectCommand", connString);
ds = new DataSet();
commandBuilder = new SQLiteCommandBuilder(da);
da.Fill(ds, "T1");
dt = ds.Tables[0];
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.DataMember = "T1";
dataGridView1.Columns["TID"].ReadOnly = true;
}
}
I don't know where exactly in the code this is happening. Handled the DataError event. Not sure if UserAddedRows, RowsValidating would be useful
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show(e.Context.ToString());
e.Cancel = true;
}
Thank you Sun
Upvotes: 1
Views: 2679
Reputation: 444
MSDN sais the following:
The ColumnIndex and RowIndex properties of the DataGridViewDataErrorEventArgs object associated with this event normally indicate the cell in which the data error occurred. When the error occurs in an external data source, however, the data source may not provide the column in which the error occurred. In this case, the ColumnIndex property typically indicates the column of the current cell at the time of the error.
Use those properties to find out in which column does the error occur.
Upvotes: 2
Reputation: 7189
Not sure. Try to replace MessageBox.Show statements with System.Diagnostics.Debug.WriteLine and do the same in RowsValidating event. MessageBox could cause the problems in this case because it "pauses" the processing.
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.Context.ToString());
e.Cancel = true;
}
Upvotes: 0