如何将控件绑定到DataGrid中的当前行?

时间:2011-02-19 14:59:29

标签: c# .net wpf

如何将控件绑定到DataGrid中的当前行?

4 个答案:

答案 0 :(得分:2)

绑定到DataGrid的SelectedItem属性。

答案 1 :(得分:2)

{Binding /}绑定到数据上下文中的当前项(当它是一个集合时)。

答案 2 :(得分:1)

您没有向我们提供大量信息,但我想我会与您分享我的解决方案。

尽管请记住这是基于Model-View-ViewModel Design Patternhttp://msdn.microsoft.com/en-us/magazine/dd419663.aspx

此处提供MVVM的精彩视频介绍: http://blog.lab49.com/archives/2650

示例代码(适用于MVVM):

// 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;

虽然您可能想要仔细检查是否传递了正确的数据类型。 :)

答案 3 :(得分:1)

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
相关问题