在<asp:datagrid DataKeyField="StaffID">中可以设置DataKeyField跟某个属性联系起来。那如果这个表示有好几个关键属性呢?我能否把他们所有都获取,以方便我下一步处理数据。

解决方案 »

  1.   

    可以的。
    键值,表示你当前绑定的SQL里面的唯一列。假设你当前想同时取出字段里面的id1,id2,id3,那么你可以这样写:
    sql = "select id1 & id2 & id3 as id,name from table";
    则可将id设置为键值,取出来的时候,就是三个值(当然你必须添加分隔符)
      

  2.   

    you should add multiple Keys when you building the datatable as the datasource of the datagrid , not when you create the structure of the datagrid:----------------------------------------------------------------          // Create sample data for the DataGrid control.
             DataTable dt = new DataTable();
             DataRow dr;
     
             // Define the columns of the table.
             dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
             dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
             dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));         // Define the primary key for the table as the IntegerValue 
             // column (column 0). To do this, first create an array of 
             // DataColumns to represent the primary key. The primary key can
             // consist of multiple columns, but in this example, only
             // one column is used.
             DataColumn[] keys = new DataColumn[1];
             keys[0] = dt.Columns[0];         // Then assign the array to the PrimaryKey property of the DataTable. 
             dt.PrimaryKey = keys;
     
             // Populate the table with sample values.
             for (int i = 0; i < 9; i++) 
             {
                dr = dt.NewRow();
     
                dr[0] = i;
                dr[1] = "Item " + i.ToString();
                dr[2] = 1.23 * (i + 1);
     
                dt.Rows.Add(dr);
             }