DataBinding between a DataTable and a TextBox
-
Hello. I have a question that i would like to ask all the codeproject members. I have this cenario: TextBox.DataBindings.Add("Text", DataTable, "Field", false, DataSourceUpdateMode.OnPropertyChanged) A textbox is binded to a DataTable field. When i change the text on the textbox the cell from the datatable receives the new value, but the RowState of the current row doesn't changes to Modified, it keeps the Unchanged state. Only when i change to another row in the DataTable (the DataTable is binded to a DataGrdiView) that the RowState of that row is changed to Modified. The problem is that when i have only one row and the user saves the information, i use a batch update to save the changes in the DataTable, but as the row keeps the Unchanged State is doesn't gets to the update. Is there any way to solve this? Thanks in advance.
-
Hello. I have a question that i would like to ask all the codeproject members. I have this cenario: TextBox.DataBindings.Add("Text", DataTable, "Field", false, DataSourceUpdateMode.OnPropertyChanged) A textbox is binded to a DataTable field. When i change the text on the textbox the cell from the datatable receives the new value, but the RowState of the current row doesn't changes to Modified, it keeps the Unchanged state. Only when i change to another row in the DataTable (the DataTable is binded to a DataGrdiView) that the RowState of that row is changed to Modified. The problem is that when i have only one row and the user saves the information, i use a batch update to save the changes in the DataTable, but as the row keeps the Unchanged State is doesn't gets to the update. Is there any way to solve this? Thanks in advance.
It is possible that the
Row
is still in Edit Mode. When the user saves, does your code callEndEdit
? If this is a possibility, see How to: Commit In-Process Edits on Data-Bound Controls Before Saving Data[^] on MSDN.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Hello. I have a question that i would like to ask all the codeproject members. I have this cenario: TextBox.DataBindings.Add("Text", DataTable, "Field", false, DataSourceUpdateMode.OnPropertyChanged) A textbox is binded to a DataTable field. When i change the text on the textbox the cell from the datatable receives the new value, but the RowState of the current row doesn't changes to Modified, it keeps the Unchanged state. Only when i change to another row in the DataTable (the DataTable is binded to a DataGrdiView) that the RowState of that row is changed to Modified. The problem is that when i have only one row and the user saves the information, i use a batch update to save the changes in the DataTable, but as the row keeps the Unchanged State is doesn't gets to the update. Is there any way to solve this? Thanks in advance.
Right before you call update on the data set, call this:
private void CommitChanges(DataSet ds)
{
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in ds.Tables)
{
dr.EndEdit();
}
}
}Or, you could first subscribe to the ColumnChanged event on the table
dt.ColumnChanged += new DataColumnChangeEventHandler(dt_ColumnChanged);
then in the event do this:
static void dt_ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
e.Row.EndEdit();
}Everything makes sense in someone's mind
modified on Wednesday, October 7, 2009 11:28 AM
-
Right before you call update on the data set, call this:
private void CommitChanges(DataSet ds)
{
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in ds.Tables)
{
dr.EndEdit();
}
}
}Or, you could first subscribe to the ColumnChanged event on the table
dt.ColumnChanged += new DataColumnChangeEventHandler(dt_ColumnChanged);
then in the event do this:
static void dt_ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
e.Row.EndEdit();
}Everything makes sense in someone's mind
modified on Wednesday, October 7, 2009 11:28 AM