A
AlanChong
Guest
Hello all. I use both Delphi and VS. When I click a Delphi grid, it always creates a new row if there is none. If you didn't type anything when you leave the grid, the newly added row is automatically deleted. VS implements another approach. When you click a DataGridView, it doesn't add a new row if there is none. You type something and hit TAB to leave the cell, a new row is created.This raises a problem.
I click a DataGridView with a DataTable of no DataRow. I type a customer code then I press TAB to leave the cell. On Cell_Leave event, codes use the customer code to fetch the customer name from a database, trying to put it on CusName field on the DataRow. It gives an error since the corresponding DataRow doesn't exists.
Below is my code. It works for normal DataTables but not for a DataTable with Filter applied. Table1 filters Table2. Table2 go with dataGridView1. The first row of Table1 is always fine. When I add a second row on Table1 and shift to dataGridView1 to type for Table2, the above problem appears. Finally I had to add a DataRow to Table2 when I add a row to Table1, this solves the problem. But this is just trouble. It is much better to have a DataGridView to automatically add a row to its DataTable when a user clicks on a DataGridView if the underlying DataTable has no rows. Any ideas ?
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
DataRow row;
dataGridView1.EndEdit();
if (dataGridView1.IsCurrentCellDirty || dataGridView1.IsCurrentRowDirty) // 把最後一筆新增列存起來
{
dataGridView1.EndEdit();
CurrencyManager cm = (CurrencyManager)dataGridView1.BindingContext[dataGridView1.DataSource, dataGridView1.DataMember];
cm.EndCurrentEdit();
}
// I tried adding a new row here. No more error but Customer Code then disappeared.
row = (bindingSource3.Current as DataRowView).Row; // Error : DataRowView is null !
//..... other code
row["CusName"] = ....
//..... other code
}
Continue reading...
I click a DataGridView with a DataTable of no DataRow. I type a customer code then I press TAB to leave the cell. On Cell_Leave event, codes use the customer code to fetch the customer name from a database, trying to put it on CusName field on the DataRow. It gives an error since the corresponding DataRow doesn't exists.
Below is my code. It works for normal DataTables but not for a DataTable with Filter applied. Table1 filters Table2. Table2 go with dataGridView1. The first row of Table1 is always fine. When I add a second row on Table1 and shift to dataGridView1 to type for Table2, the above problem appears. Finally I had to add a DataRow to Table2 when I add a row to Table1, this solves the problem. But this is just trouble. It is much better to have a DataGridView to automatically add a row to its DataTable when a user clicks on a DataGridView if the underlying DataTable has no rows. Any ideas ?
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
DataRow row;
dataGridView1.EndEdit();
if (dataGridView1.IsCurrentCellDirty || dataGridView1.IsCurrentRowDirty) // 把最後一筆新增列存起來
{
dataGridView1.EndEdit();
CurrencyManager cm = (CurrencyManager)dataGridView1.BindingContext[dataGridView1.DataSource, dataGridView1.DataMember];
cm.EndCurrentEdit();
}
// I tried adding a new row here. No more error but Customer Code then disappeared.
row = (bindingSource3.Current as DataRowView).Row; // Error : DataRowView is null !
//..... other code
row["CusName"] = ....
//..... other code
}
Continue reading...