首页 > 代码库 > GridView设置多个DatakeyNames

GridView设置多个DatakeyNames

1.aspx页面GridView直接绑定DataKeyNames

aspx设置:<asp:GridView ID="grvGrid" runat="server" DataKeyNames="Id,Time"></asp:GridView>aspx.cs获取:protected void grvGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e){    string id = grvGrid.DataKeys[e.NewSelectedIndex][0].ToString();    string time=grvGrid.DataKeys[e.NewSelectedIndex]["Time"].ToString();}

 

2.GridView初始化时绑定DataKeyNames

初始化DataKeyNames时:   GridView1.DataKeyNames=Gridview1.DataKeyNames = new string[] { "PositionID", "DepartmentID" };获取:protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)        {            string positionID = Gridview1.DataKeys[e.RowIndex]["PositionID"].ToString();        } 

 

GridView设置多个DatakeyNames