0

How to get the value of some specific cell on editing a gridview when boundfield is set to false.

<asp:GridView ID="GridViewInvoice" runat="server" ShowHeaderWhenEmpty="true" DataKeyNames="DOID" AutoGenerateColumns="False" CssClass="mGrid" HeaderStyle-CssClass="th" PagerStyle-CssClass="pgr"
    Width="100%" BorderStyle="Solid" Style="overflow: auto" OnRowDataBound="GridViewInvoice_RowDataBound" OnRowEditing="GridViewInvoice_RowEditing" OnRowDeleting="GridViewInvoice_RowDeleting">
    <AlternatingRowStyle BackColor="#CCCCFF" />
    <Columns>
        <asp:BoundField DataField="Tax1" Visible="false" HeaderText="Tax1" SortExpression="Tax1" HeaderStyle-Width="7%" ItemStyle-CssClass="colHidden" HeaderStyle-CssClass="colHidden" ItemStyle-HorizontalAlign="Right" ReadOnly="false" />
    </Columns>
</asp:GridView>
xoxo
  • 1,248
  • 14
  • 19
Lima
  • 47
  • 2
  • 6
  • you mean.. you want the value to be visible when you update gridview – Usha phulwani Sep 20 '18 at 08:56
  • use this line of code in code behind.. start counting from 0 and determine what number the column is, then just do this replacing # with the number you came up with. `myGridView.Columns[#].Visible = True`; – Usha phulwani Sep 20 '18 at 09:03
  • no...when i update the value i want to get that value to a variable – Lima Sep 20 '18 at 09:09
  • use column index in place of # to get the value of whatever column you want (in your case hidden column). `protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { string s=GridView1.Rows[e.NewEditIndex].Cells[#].Text; }` – Usha phulwani Sep 20 '18 at 09:18
  • see this link.. https://stackoverflow.com/a/10680513/9650643 – Usha phulwani Sep 20 '18 at 09:33

1 Answers1

0

You cannot access the BoundField value in GridView if its visibility is set to Hidden. Instead, Add a HiddenField and access that in GridView Edit Event.

Add the below as a GridView Column

<asp:GridView ID="GridViewInvoice" runat="server" ShowHeaderWhenEmpty="true" DataKeyNames="DOID" AutoGenerateColumns="False" CssClass="mGrid" HeaderStyle-CssClass="th" PagerStyle-CssClass="pgr"
    Width="100%" BorderStyle="Solid" Style="overflow: auto" OnRowDataBound="GridViewInvoice_RowDataBound" OnRowEditing="GridViewInvoice_RowEditing" OnRowDeleting="GridViewInvoice_RowDeleting">
    <AlternatingRowStyle BackColor="#CCCCFF" />
    <Columns>
        <asp:BoundField DataField="Tax1" Visible="false" HeaderText="Tax1" SortExpression="Tax1" HeaderStyle-Width="7%" ItemStyle-CssClass="colHidden" HeaderStyle-CssClass="colHidden" ItemStyle-HorizontalAlign="Right" ReadOnly="false" />
       <asp:HiddenField ID="Tax1" runat="server" Value='<%#Eval("Tax1") %>'/>
    </Columns>
</asp:GridView>

Code-Behind

protected void GridViewInvoice_RowEditing(object sender, GridViewEditEventArgs e)
{
  HiddenField hf = (HiddenField)GridViewInvoice.Rows[e.RowIndex].FindControl("Tax1");

  if (hf != null)
  { 
      var value = hf.Value;
  }  
}

Alternatively, you can also get the value with DataKeyNames if you have specified one in the GridView.

Gets or sets an array that contains the names of the primary key fields for the items displayed in a GridView control.

Something like this,

<asp:GridView ID="GridViewInvoice" runat="server" DataKeyNames="Tax1">
<Columns>
   <asp:HiddenField ID="Tax1" runat="server" Value='<%#Eval("Tax1") %>'/>
</Columns>
</asp:GridView>

Then this to get the value,

protected void GridViewInvoice_RowEditing(object sender, GridViewEditEventArgs e)
{
    var value = GridViewInvoice.DataKeys[e.RowIndex]["Tax1"].ToString();
}
Hary
  • 5,690
  • 7
  • 42
  • 79