4

I have a GridView just like this:

<asp:GridView ID="gvwStudents" runat="server" 
    AutoGenerateColumns="False" DataKeyNames="ID"
    ShowHeader="False" onrowdeleting="gvwStudents_RowDeleting">
    <Columns>
        <asp:BoundField DataField="FirstName" />
        <asp:BoundField DataField="LastName" />
        <asp:BoundField DataField="Email" />
        <asp:CommandField ShowDeleteButton="True" DeleteText="Remove" />
    </Columns>
</asp:GridView>

Here is how I am creating my DataTable, which the GridView is bound to, so that you know what data I am dealing with:

private DataTable MakeStudentsTable()
{
    DataTable students = new DataTable();

    DataColumn ID = students.Columns.Add("ID", typeof(int));
    ID.AutoIncrement = true;

    DataColumn firstName = students.Columns.Add("FirstName", typeof(string));
    DataColumn lastName = students.Columns.Add("LastName", typeof(string));
    DataColumn email = students.Columns.Add("Email", typeof(string));


    return students;
}

Why, oh, why are there no keys passed in the EventArgs of the RowDeleting event? I need to remove the record from the ADO.NET DataTable that I am keeping in session state when this event is fired.

Why isn't this working? Is it that DataKeys only work when using a DataSource control?

2
  • I think you should add the "databinding" tag to this question. BTW, I've been banging my head over exactly the same problem. Feb 25, 2009 at 7:01
  • How are you adding students to your table? Is the data being populated in your table before the Deleting method is called (i.e. in Page_Load)?
    – cjk
    Feb 25, 2009 at 11:50

3 Answers 3

8

This works:

private DataTable MakeStudentsTable()
{
    DataTable students = new DataTable();

    DataColumn ID = students.Columns.Add("ID", typeof(int));
    ID.AutoIncrement = true;

    DataColumn firstName = students.Columns.Add("FirstName", typeof(string));
    DataColumn lastName = students.Columns.Add("LastName", typeof(string));
    DataColumn email = students.Columns.Add("Email", typeof(string));

    DataRow student = students.NewRow();
    student["FirstName"] = "foo";
    student["LastName"] = "bar";
    student["Email"] = "[email protected]";
    students.Rows.Add(student);

    return students;
}

protected void gvwStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string id = this.gvwStudents.DataKeys[e.RowIndex].Value.ToString();   
}
3
  • Shouldn't I be able to access in the event args? Feb 25, 2009 at 13:17
  • Your assertion is correct, GridViewDeletedEventArgs.Keys collection is only populated when using a DataSourceID.
    – Phaedrus
    Feb 25, 2009 at 15:29
  • This is a workaround to the problem that GridViewDeletedEventArks.Keys collection isn't populated. Are we just giving up on ever having this populated? Feb 25, 2009 at 21:22
1

Set the DataKeyNames Property of the GridView control to the name(s) of the primary key(s) of your data.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx

0

add

<asp:BoundField DataField="ID" />

and see if the field even has a value.

1
  • I have checked this. It does have a value. Feb 24, 2009 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.