MrTony78
MrTony78

Reputation: 35

gridview datakeys has count=0

e.RowIndex is 5 but gridView1.DataKeys.count=0

Can you help me?

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) { }
}

Upvotes: 0

Views: 410

Answers (1)

ADyson
ADyson

Reputation: 61904

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

says

When the DataKeyNames property is set, the GridView control automatically creates a DataKey object for each row in the control....The DataKey objects are then added to the control's DataKeys collection.

You need to set the DataKeyNames property on your GridView with a suitable value before this will work.

Upvotes: 1

Related Questions