Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with editing data via databound Textbox

Problem with editing data via databound Textbox

Scheduled Pinned Locked Moved C#
csharpdatabasevisual-studiowpfwcf
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kbalias
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups