Doing something when the current row in the datagrid changes

N

Nevyn Twyll

I have a windows form.
It fetches some data that it keeps in a Dataset (in a property).
I want to bind a grid to that datasource, and have 2 textboxes (they could
also be labes if need be, but textboxes would be better) on the form that
display (read-only) the contents of a couple of text fields.
The only thing I can really get to happen is to have the textboxes display
the data from the first row of the dataset, but not change .

Here's a sample of my code:

// Fetch the data and name the table
DataSet ds = sqlconnection.ExecuteDataSet(...)
ds.Tables[0].TableName = "MyTable";


// Bind the datagrid
myDataGrid.DataMember = "MyTable";
myDataGrid.DataSource = ds;

// Bind the textbox?
txtMyTextBox.DataBindings.Add(new Binding("Text", ds,
"MyTable.MyTextField"));


So I guess my question might be: how do I tell (get an event handler, etc.)
when the current row in the datagrid changes, and which row it is. Is there
anything like in asp.net datalists?

Thanks for the help!
 
P

Pete Wright

Set up the binding on the text box to the table, not the dataset and then
just set up the name of the field you are interested in in the DataBindings
collection. As long as you are bound to the same data source as that the
grid, all should be fine. I just wrote a simple app that had a grid and a
text box on the form to try things out. I dragged the customers table from
the server explorer onto the form, then right clicked the data adapter that
the form editor created and chose Generate Dataset. With that done the
following code in my form's load event set up my textbox to automatically
show a field from the currently selected row in the datagrid.

customersDataAdapter.Fill( northwindDS );

dataGrid1.DataSource = northwindDS.Customers;

textBox1.DataBindings.Add("Text", northwindDS.Customers, "CompanyName");

Hope that helps

--
 

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