Search code examples
c#.netwinformsdata-binding

Binding a textbox to a datagridviewtextboxcolumn?


Is it possible, to bind (or similar) a standard textbox to display the contents (dynamically) of the selected cell within a datagridview textboxcolumn?

My goal is that when a cell within this column has it's value altered, the textbox.text is also changed, and when the user selects a cell then types something in this separate textbox the value is updating the datagridview textboxcolumns value on the fly.


Solution

  • Yes you may bind common dataSource to both TextBox and DataGridView.

    public class Foo
    {
      public string Item { get; set; }
    }
    
    private void Form2_Load(object sender, EventArgs e)
     {
      List<Foo> list = new List<Foo>()
          {
           new Foo() { Item="1" },
           new Foo() { Item="2" }
       };
    
      dataGridView1.DataSource = list;
      textBox1.DataBindings.Add("Text", list, "Item");
    }