Problem with editing data via databound Textbox
-
Hi I am having some problems with binding a TextBox to a data source. Actually the problem is in editing the data via the TextBox. I am using Visual Studio 2005, C# and MS Access 2003 to develop a Windows Application. On the Form I have the following controls: DataGridView (name = dgv) TextBox (name = txtWord) BindingSource (name = bindingSource1) DataSet (name = ds) Button (name = btnSave) I also have a class that I created to manage the data interaction. This class is called TestDataManager_MSAccess. In this class I specify the table name of the relevant table in the database. (This is also the name that the DataTable will be called after the data loaded). The class also has a load data method (TestDataManager_MSAccess.LoadTestData()) and a method to update the database (TestDataManager_MSAccess.UpdateDataTable()). When the Form loads the data is loaded from the database and bound to the DataGridView via the BindingSource.
this.bindingSource1.DataSource = ds;
this.bindingSource1.DataMember = TestDataManager_MSAccess.TableName;dgv.DataSource = bindingSource1;
I want the following to happen: When the user double-clicks on a DataGridViewCell, the TextBox must be bound to the value in the DataGridViewRow. I bind the TextBox as follows:
private void dgv_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
dgv.EndEdit();//when a cell is double clicked, the textbox is populated with the relevant value txtWord.DataBindings.Add("Text", this.bindingSource1, TestDataManager\_MSAccess.Word\_ColName);
}
This works ok and the relevant value in the DataGridViewCell is displayed in the TextBox. But I also want to be able to edit the value via the TextBox, but it does not seem to work. I look at the DataTable and even though the new value shows in the DataRow in the DataTable, the RowState still shows Unchanged. So when I call the Update method in my TestDataManager_MSAccess class the row will not update the database. My code in the Form:
public Form_TxtBinding()
{
InitializeComponent();
}private void Form_TxtBinding_Load(object sender, EventArgs e)
{
try
{
this.LoadDataController();
this.DisplayData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}private void LoadDataController()
{
try
{
if (ds == null)
{
ds = new D