Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
How to hide GridView column and retrieve value from hidden column cell in ASP.NET

Author: Nikki Smith
Download Source Code : 1410_HideColumnAndRetrieveValue.zip

In this article, I will show you how you can hide GridView column and how you can retrieve selected value from hidden column cell in ASP.NET.

 

Sometimes you don’t want to display one of your GridView control column and you want to hide it from users but you may need the value from this column. For example, you don’t want to show ID column of your database table but ID is primary key for your table so you need value from this column. You can easily hide any column and can get value from hidden column cell of selected record.
 
  1. Create a new Empty Web Site in Visual Studio 2010 either in Visual Basic or Visual C#.
  2. Add a Web Form in the Web Site. No Need to change name of the Page
  3. Add a GridView control in Web Form and write code below in GridView.

    I have added a CommandField in GridView.
     
    <asp:GridView ID="GridView1" runat="server"
        AutoGenerateColumns="false"
        OnRowCreated="GridView1_RowCreated" 
        onrowcommand="GridView1_RowCommand">
        <Columns>
            <asp:BoundField HeaderText="CustomerID" DataField="CustomerID" />
            <asp:BoundField HeaderText="CompanyName" DataField="CompanyName" />
            <asp:BoundField HeaderText="ContactName" DataField="ContactName" />
           <asp:BoundField HeaderText="City" DataField="City" />
           <asp:BoundField HeaderText="Country" DataField="Country" />
           <asp:CommandField ShowSelectButton="True" />
       </Columns>
    </asp:GridView>
    <br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
     
  4. Namespaces used in the code

    Visual Basic
     
    Imports System.Data
    Imports System.Data.SqlClient
     
    Visual C#
     
    using System.Data;
    using System.Data.SqlClient;
     
  5. Write code below in page load event of code behind file

    Code below in page load event is written to get data from Customers table of NORTHWND database in a DataSet object.
     
    Visual Basic
     
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim text As String = "SELECT CustomerID, CompanyName, ContactName, City, Country FROM Customers"
        Dim connString As String = "Data Source=YourServer;Initial Catalog=NORTHWND;Integrated Security=True"
        Dim conn As New SqlConnection(connString)
        Dim cmd As New SqlCommand(text, conn)
        conn.Open()
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        conn.Close()
     
        GridView1.DataSource = ds
        GridView1.DataBind()
    End Sub
     
    Visual C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        string text = "SELECT CustomerID, CompanyName, ContactName, City, Country FROM Customers";
        string connString = "Data Source=YourServer;Initial Catalog=NORTHWND;Integrated Security=True";
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(text, conn);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
     
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
     
  6. Add RowCreated and RowCommand events for GridView and write code below in it.

    In RowCreated event, I have set “Visible” property to “false” for first column for both header and data row.
     
    In RowCommand event, CommandName is checked first and then index for select command is retrieved. I have retrieved the row for that index and then retrieved the text in the specified row cell.   
     
    Visual Basic
     
    Protected Sub GridView1_RowCreated(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(0).Visible = False
        End If
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(0).Visible = False
        End If
    End Sub
     
    Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Select" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = GridView1.Rows(index)
     
            Label1.Text = (row.Cells(0).Text)
        End If
    End Sub
     
    Visual C#
     
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Visible = false;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Visible = false;
        }
    }
     
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            Label1.Text = (row.Cells[0].Text);
        }
    }
     
  7. Now you can see website in your browser. Select row and see customer ID on Label.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to insert and update database records using ListView control in ASP.NET

Disclaimer - Privacy
© 2002-2014 DevASP.net