Azri Zakaria
Azri Zakaria

Reputation: 1364

Get hidden value on row deleting event in datagrid (ASP Webforms)

I cannot retrieve the value of invisible column from datagrid . How can I get the value if for column invisible?

Here my code :

Datagrid :

<asp:BoundField DataField="id" HeaderText="ID" ReadOnly="True" 
            Visible="False" />
        <asp:BoundField DataField="category" HeaderText="Category" />
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
            ShowHeader="True" />

on Row Deleting event

 protected void dgvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = dgvCategory.Rows[e.RowIndex].Cells[0].Text;

        string name = dgvCategory.Rows[e.RowIndex].Cells[1].Text;

        runDelete(id, name);

        loadDataCategory();            

    }

How can I resolved my problem?

Upvotes: 3

Views: 2101

Answers (1)

Damith
Damith

Reputation: 63095

you can add this ID column as DataKey

in your aspx

<asp:GridView DataKeyNames="id" ....

in your event

protected void dgvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    var key = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
}

Other option is using css style to hide the column

create new css class with display: none; and apply it to ItemStyle-CssClass and HeaderStyle-CssClass of your BoundField. remove Visible="False" property.

now you can get values as other columns but it will not display in client UI.

Upvotes: 4

Related Questions