gridview datakeys的count = 0

时间:2018-01-24 12:08:59

标签: c# asp.net gridview

e.RowIndex为5但gridView1.DataKeys.count=0

你能帮助我吗?

protected void gridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {
        string T10_CodGar = gridView1.DataKeys[e.RowIndex].Values["T10_CodGar"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from [PUMA2_FINANZIARIAFAMILIARE].[dbo].[TCOR10] where T10_CodGar=" + T10_CodGar, con);
        int result = cmd.ExecuteNonQuery();
        con.Close();

        if (result == 1)
        {
            loadStores1();
            //    lblmsg.BackColor  = Color.Red;
            //    lblmsg.ForeColor = Color.White ;
            //    lblmsg.Text = stor_id + "      Deleted successfully.......    ";
        }
    }
    catch (Exception ex) { }
}

1 个答案:

答案 0 :(得分:1)

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys(v=vs.110).aspx

的DataKeys文档

  

设置DataKeyNames属性时,GridView控件   自动为。中的每一行创建一个DataKey对象   控制....然后将DataKey对象添加到控件中   DataKeys集合。

您需要在GridView上使用合适的值设置DataKeyNames属性,然后才能使用。