开发者

Easiest way to synchronize a dialog form with a datagridview row?

I have a datagridview with a row id from a database. I want to open a dialog form pointing to to the same row. What's the syntax for doing something like

Detail.BindingContext开发者_StackOverflow = Gridform.BindingContext

Since my other question was close https://stackoverflow.com/questions/1527887/how-to-point-to-use-same-datasource-and-currencymanager-from-a-second-c-form

I update this one: I prefer a solution with the currency manager.


I'm not sure what you're trying to do. I believe you want to be able to select a row in a DataGridView, do something to open a dialog with another DGV that has the same record and select that record in the dialog.

If this is what you want to do then you need to do two things: get the id of a selected row in the main form's DGV, and then programmatically select a row in another DGV. Here's how you might do this:

Step 1. Get the id of the selected row on the main form.
Something roughly like this should work:

string id = dataGridView.SelectedRows[0].Cells[colIdColumn.Index].Value.ToString();

where:
a. The column that has the id you mention is named colIdColumn
b. The data type of id is string

After you've validated id, open your dialog and pass it id. When the dialog opens conitnue to step 2.

Step 2. Programmatically select a row on another DataGridView
Look at the BindingSource.Find method to return the index that a value appears in a BindingSource and look at the BindingSource.Position property to select a record in a BindingSource.

Your code might look something like this:

// Get index of row with your id.
int index = yourBindingSource.Find("YourIdProperty", "Id");
yourBindingSource.Position = index;

Hope that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜