由于我的表是两个主键,怎么才能在DataGrid中设置两个DataKeyField

解决方案 »

  1.   

    DataGrid只可以有一个DataKeyField
    但是你可以把你的主键绑到在Label.Text上面,并且把样式设置为Display:NONE就可以了
    效果一样
      

  2.   

    add an expression columnYourDataTable.Columns.Add("MyKey", typeof(string), "Column1 + '#' + Column2");DataGrid1.DataSource = YourDataTable.DefaultView;
    DataGrid1.DataKeyField = "MyKey";
    DataGrid1.DataBind();and when you get the key value, use split
      

  3.   

    来自saucer
    add another column to your data tableYourDataTable("MyKey", typeof(string), "key1 + '#' + key2 + '#' + key3");you might need to convert your key to a string type, for example 
    YourDataTable("MyKey", typeof(string), "Convert(key1,'System.String') + '#' + key2 + '#' + key3");YourDataGrid.DataKeyField = "MyKey";
    ...string [] slist = YourDataGrid.DataKeys[YourItemIndex].ToString().Split('#');