VB.NET - how to get the Hidden Column Value? - Asked By Prabhakaran on 14-Nov-11 06:02 AM

hi friends,

            how to get the hidden column values in Datagridview?

thnx in advance...
Jitendra Faye replied to Prabhakaran on 14-Nov-11 06:06 AM

you can use UseColumnTextForButtonValue to set the cell value please check like this

Code Snippet

Dim r As DataGridViewRow
      For Each r In DataGridView1.Rows
        If r.Cells(2).Value = 2 Then
          r.DefaultCellStyle.BackColor = Color.Beige
Dim rc As DataGridViewButtonCell
          rc = r.Cells(0)
          rc.UseColumnTextForButtonValue = False
          rc.Value = "new"
        End If

Jitendra Faye replied to Prabhakaran on 14-Nov-11 06:07 AM
Try this -

you can also accsess any colomn by its name value .you can just edit the column by using

this.dgv.Rows[e.RowIndex].Cells["YOUR_HIDDEN_COLOMN"].Value = "NEW_VALUE"
Suchit shah replied to Prabhakaran on 14-Nov-11 06:08 AM

Option1: Using the DataKeyNames of GridView (recommended)

The easiest way to store the hidden fields (primary keys) is by using  DataKeyNames property of the GridView control because this provides a  convenient way to access the primary keys of each row.Here are the code blocks below:


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Column1"



      <asp:CommandField ShowSelectButton="True" />

      <asp:BoundField HeaderText="Header 2"  DataField="Column2" />




As you can see, we removed the hidden BoundField column that contains the  Column1 data in the GridView and set up the DataKeyNames in the GridView nstead.Here’s the code for accessing the primary key of the row using the DataKeys.


protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)


   //get the selected DataKey

   int rowIndex = GridView1.SelectedIndex;

   string strValue = GridView1.DataKeys[rowIndex].Value.ToString();


   //display the selected value in a pop up

   Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "SCRIPT", string.Format("alert('{0}');", strValue), true);



Option 2: Using a TemplateField Column with a HiddenField Control

Another way would be storing the primary key column in a HiddenField Control.

Here’s an example:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"



      <asp:CommandField ShowSelectButton="True" />



        <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("Column1") %>' />



      <asp:BoundField HeaderText="Header 2"  DataField="Column2" />



Riley K replied to Prabhakaran on 14-Nov-11 06:08 AM

When you want to have a value but not want to show it, is using http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx.

So, set up the GridView like this:

<asp:GridView ID="luoi_kq" runat="server" AllowPaging="True" OnRowCommand="luoi_kq_RowCommand" DataKeyNames="cvid"
          AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
          GridLines="None" Height="86px" PageSize="20" ShowFooter="True"
          Width="100%" DataSourceID="ObjectDataSource1" ForeColor="#333333"

and now you'll get the value like this:

protected void luoi_kq_RowCommand(object sender, GridViewCommandEventArgs e)
     int selected_index = int.Parse(e.CommandArgument.ToString());    
        string id = luoi_kq.DataKeys[selected_index].Value.ToString() ;
     Response.Redirect("Ho.aspx?id=" + id);

Suchit shah replied to Prabhakaran on 14-Nov-11 06:09 AM
there are four  ways to get value of hidden column.
1. First Way - Use DataKeyNames Property of GridView
2. Second Way - Use TemplateField column in GridView
3. Third Way - Use StyleSheet
4. Fourth Way - Set Visibility of Column after Binding DataSource to GridView

for the complete listing and how to use it refer : http://www.codeproject.com/Tips/80964/How-to-get-Hidden-Column-Value-in-GridView
Prabhakaran replied to Suchit shah on 14-Nov-11 06:11 AM
in vb.net datagridview... not in asp.net
dipa ahuja replied to Prabhakaran on 14-Nov-11 06:19 AM
void getData()
  foreach (GridViewRow row in GridView1.Rows)
    string name = ((Label)row.FindControl("label1")).Text.ToString();
    string hiddenColumn = ((HiddenField)row.FindControl("hiddenField1")).Value.ToString();
    //code to write

Suchit shah replied to Prabhakaran on 14-Nov-11 06:23 AM
You can use the following to retrieve the underlying data in the hidden field in the RowDataBound event:

DataBinder.Eval(e.Row.DataItem, "StructureID")

In Behind Code do something like this:
Protected Sub linButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim linkbutton1 As LinkButton = CType(sender, LinkButton)
Dim gridrow As GridViewRow = CType(linkbutton1.Parent.Parent, GridViewRow)

Dim index As Integer = grdrow.DataItemIndex

lbl_ID.Text = GridView1.DataKeys(index).Value.ToString()

End Sub

Kirtan Patel replied to Prabhakaran on 14-Nov-11 06:42 AM
you can access hidden column value as you access normal Cell Value like below code
Dim colValue as String =DataGridView1.Rows(0).Cells(1).Value.ToString()
Prabhakaran replied to Kirtan Patel on 15-Nov-11 02:51 AM
ya thanx.. 

           it works, but it didnt works in cellformating event..

before this event i hide the column 3 by datagridview1.columns(3).visible = false.. 

  Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
       'If DataGridView1.Columns(e.ColumnIndex).Name.Equals("Added By Text") Then
        '    'DataGridView1.CurrentRow.Cells(3).Value.ToString()
        '    'If (DataGridView1.Rows(e.RowIndex).Cells(3).Value = "Krithika") Then                        
        '    'Try
        '    'If DataGridView1.CurrentRow.Cells(3).Value.ToString() = "Krithika" Then
        '    stringValue = e.Value
        '    Select Case stringValue
        '        Case "Krithika"
        '            DataGridView1.Item(2, e.RowIndex).Value = My.Resources.Play
        '            Exit Select
        '        Case Else
        '            DataGridView1.Item(2, e.RowIndex).Value = My.Resources.Activate
        '            Exit Select
        '    End Select
        'End If
    End Sub