Databinding - bind textbox and datagridview

  • Thread starter Thread starter jogisarge
  • Start date Start date
J

jogisarge

Hi,

i want to show in a textbox the value from a selected row in my
datagridview.

DataSet dsHersteller = new DataSet();BindingSource bsHersteller = new
BindingSource();dsHersteller = herstellermanager.GetList(); // -
delivers a datasetbsHersteller.DataSource = dsHersteller.Tables
["producer"];bsHersteller.Sort =
"producer_id";dgvHersteller.DataSource =
bsHersteller.DataSource;tbxHerstellerNr.DataBindings.Add("Text",
bsHersteller, "producer_id");


Now, when i start the app, the value of the textbox is the first row
in datagridview.
If i select another row, nothing changes in my textbox.

I thougt, i bound the textbox to the grid ??

Can anybody give me tips for my problem ?
 
--
Happy Coding!
Morten Wennevik [C# MVP]


jogisarge said:
Hi,

i want to show in a textbox the value from a selected row in my
datagridview.

DataSet dsHersteller = new DataSet();BindingSource bsHersteller = new
BindingSource();dsHersteller = herstellermanager.GetList(); // -
delivers a datasetbsHersteller.DataSource = dsHersteller.Tables
["producer"];bsHersteller.Sort =
"producer_id";dgvHersteller.DataSource =
bsHersteller.DataSource;tbxHerstellerNr.DataBindings.Add("Text",
bsHersteller, "producer_id");


Now, when i start the app, the value of the textbox is the first row
in datagridview.
If i select another row, nothing changes in my textbox.

I thougt, i bound the textbox to the grid ??

Can anybody give me tips for my problem ?

Hi,

You are correctly using a BindingSource and databind the TextBox against
this BindingSource. Any change in the BindingSource.Position will then cause
the TextBox to change its value. However, you aren't binding the
DataGridView against this BindingSource, only the BindingSource's DataSource.
This means the BindingSource itself won't get notified when you select rows
in the DataGridView, and thereby won't update the TextBox either. Change one
line of code to make it work.

From
dgvHersteller.DataSource = bsHersteller.DataSource;
to
dgvHersteller.DataSource = bsHersteller;
 
Back
Top