Reputation: 645
How to bind control to the current row in DataGrid?
Upvotes: 2
Views: 2213
Reputation: 7
int i=dataGridView1.CurrentRow.Index;
string ID=dataGridView1.Row[i].Cell["id"].Value.ToString();
// Now you can adopt the table value to your query strin
string strData="SqlDataAdapter da=new SqlDataAdapter ("select * from Table1 Where ID='"+ID+"',cn);
DataTable dt=new DataTable();
da.Fill(dt);
//Now bind the data to corresponding controls.
txtID.Text=dt.Row[0]["ID"].ToString();
//Happy Coding
Upvotes: 1
Reputation: 645
{Binding /} binds to the current item in the data context (when it's a collection).
Upvotes: 2
Reputation: 26362
You haven't provided us with a lot of information, but I thought I would share my solution with you.
Although keep in mind that this is based on the Model-View-ViewModel Design Pattern
.
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
A good video introduction to MVVM
available here:
http://blog.lab49.com/archives/2650
Sample code (for MVVM) here:
// XAML
<DataGrid ...SomeCode... SelectedItem="{Binding SelectedItem}"/>
// Inside my ViewModel I have:
private object _SelectedItem;
public object SelectedItem
{
get { return this._SelectedItem; }
set
{
if (value != null)
{
this._SelectedItem = value;
OnPropertyChanged(new PropertyChangedEventArgs(SelectedItemProperty));
}
}
}
// To resolve the SelectedItem you can use the following
var item = (MyNamespace.MyDataSource)SelectedItem;
Although you may want to double check that you're getting passed the correct datatype. :)
Upvotes: 1