I’m trying to write a simple Compact Framework winforms app. The main form has a DataGrid bound to a DataTable (with data from an xml file). I want to bring up another form that displays the details of the current record. I have something like the following code as the constructor for the detail form.
public DetailsForm(DataTable dtLandlords, int Index) //the constructor
{
InitializeComponent();
lLandlordCode.DataBindings.Add("Text", dtLandlords, "LandlordID");
.......
}
I’m Calling the constructor with the following code
Form frm = new LandlordDetailsForm(dtLandlords, dataGrid1.CurrentRowIndex);
frm.Show();
How do I get it to display the current record (specified in Index – currently not used) rather than just the first record. Or is there a better way that I should be doing this?
Databindings “bind” to a provided “View”, currently you are binding to the DataTable without setting the default view (So it will default to the complete table). Eg. dtLandlords.DefaultView.RowFilter = “LandlordID = TheIdYouWant”;
The other way to do it is to add the DataGrid/GridView itself to the DataBingings which will provide a default view containing it’s currently selected item.
Edit: Added example of binding to a DataGridView
An example of this is:
First create a form with a TextBox and DataGridView (default names). Then put this code in the constructor of the form.
Then run, and select items in the GridView and the TextBox should automagically be updated with the selected details. Note: I used a DataGridView here, I assume it will also work for DataGrids (which I think you are using)