Binding dataset to datagrid and textboxes

J

jason

hi,

I am trying to bind a dataset to datagrid and textboxes (selected columns),
when you selected a row in datagrid, the textboxes controls should refresh.


thanks.
 
D

Dhominator

jason said:
hi,

I am trying to bind a dataset to datagrid and textboxes (selected columns),
when you selected a row in datagrid, the textboxes controls should refresh.


thanks.
Do it right in the designer and look at the generated code. It will
[mostly] work the way you expect. Look at CurrencyManager as you'll
probably want to do EndCurrentEdit() when the user accepts changes.

Updates and deletes will work as expected. Inserts (new records into
dataset) won't :( I've not seen a good summary of adding new rows...
User clicks New button... then what do you do? If you add a new blank
row to the dataset it'll likely violate some constraint... I treat New
as a mode and do dataset.EnforceConstraints = false (I don't unbind the
controls). User enters data in the textbox controls (whatever)... when
they click save I validate the data and set dataset.EnforceConstraints =
true;

The above assumes that the grid (selection list) and the controls are
bound to the same dataset. Play around with it... it may be easier to
drive the grid off one dataset and the individual controls off another
dataset (but then you have to synch the two... like re-retrieve the grid
after changes/updates/deletes).

Best,
John
p.s. Don Esposito has good overview of this at
http://www.devx.com/codemag/Article/22028... Chris Sells has one too (or
get his WinForms book... very good).
 
D

Dhominator

jason said:
hi,

I am trying to bind a dataset to datagrid and textboxes (selected columns),
when you selected a row in datagrid, the textboxes controls should refresh.


thanks.

Previous assumes you know how to bind grid to dataset... Drop dataset on
form... select grid and set DataSource=someDataset, DataMember=someTable.

For textbox, select it, Data, DataBindings, Text set it to the
table/column (dropdown) you want. It'll automatically synch when you
change rows in the grid ;)

You have to be consistent in the way you bind things as Chris Sells says...

// 1. Bind to data set + table.column == GOOD
textBox1.DataBindings.Add( "Text", someDataset, "TableName.ColumnName" );

// 2. Bind table + column == BAD!
textBox1.DataBindings.Add( "Text", someDataset.TableName, "ColumnName" );

Same goes for binding in the designer... DataSource=someDataset,
DataMember="TableName.ColumnName" (aka #1). This way there is only a
single instance of the BindingManager... and everything will stay
synched... otherwise you get multiple instances and things do work.


Best,
John
 

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