Databinding - bind textbox and datagridview

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 ?
 
M

Morten Wennevik [C# MVP]

--
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top