Tleck Aipov
Tleck Aipov

Reputation: 494

How could i take id from invisible cell in gridview?

I did invisible first column in my gridview.:

<asp:GridView ID="tableResults" runat="server" DataMember="Table"
    EnableModelValidation="True" CssClass = "GridViewStyle"
    AutoGenerateColumns="False" OnRowDataBound = "tableResults_DataBound"
    OnRowDeleting = "tableResults_RowDeleting" AutoGenerateDeleteButton="True">                     
   <HeaderStyle CssClass = "GridViewHeaderStyle" />
   <RowStyle CssClass = "GridViewRowStyle"/>    
   <Columns>                   
    <asp:BoundField DataField="I_ID" Visible="false" HeaderText=""/>
    <asp:BoundField DataField="I_MAJOR" HeaderText="Major"/>
    <asp:BoundField DataField="I_MINOR" HeaderText="Minor"/>
    <asp:BoundField DataField="I_RELEASE" HeaderText="Release"/>
    <asp:BoundField DataField="I_BUILD" HeaderText="Build"/>
   </Columns>           
</asp:GridView>

Now when I get value from column 1 it null:

TableCell rowData = tableResults.Rows[e.RowIndex].Cells[1];
oracleCom.CommandText = "Delete From TBL_VERSIONS Where i_id = " + rowData.Text;

If i did column visible = true, i could get value. But this column must be invisible.

Upvotes: 0

Views: 1712

Answers (2)

sajanyamaha
sajanyamaha

Reputation: 3198

One simple way:

Keep the column visible

columnName.visible = true

but make it invisible with HTML

style="visibility:hidden;"

OR

style="display:none;"

ie like

<asp:BoundField DataField="I_ID" Visible="false" style="visibility:hidden;" HeaderText=""/>

This way you can keep it hidden and also access its value.

Upvotes: 0

Damith
Damith

Reputation: 63095

set DataKeyNames as I_ID

<asp:GridView ID="tableResults" runat="server" DataMember="Table"
    EnableModelValidation="True" CssClass = "GridViewStyle"
    AutoGenerateColumns="False" OnRowDataBound = "tableResults_DataBound"
    OnRowDeleting = "tableResults_RowDeleting" AutoGenerateDeleteButton="True"
    DataKeyNames = "I_ID" >

then you can get id as below

 int idVal= (int)tableResults.DataKeys[e.RowIndex].Value;

or you can set the cell invisible on gridview row data bound event, remove the invisible property on DataField="I_ID".

protected void tableResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
   e.Row.Cells[1].Visible = false;
}

Upvotes: 1

Related Questions