locked
Hide Data Dynamic Column in Gridview RRS feed

  • Question

  • User-807418713 posted

    Hello

    In my gridview its bind data automatically

    my gridview look like this

     <asp:GridView ID="GridView1" runat="server" Font-Bold="False" 
                        Font-Names="Calibri" Font-Size="12pt" OnRowDataBound="GridView1_RowDataBound1">
                        </asp:GridView>

    i want to hide 3 column how to do so

    Tuesday, September 17, 2019 5:59 PM

Answers

  • User288213138 posted

    Hi Gopi.MCA,

    i want to hide 3 column how to do so

    You can set columns invisible in your GridView's RowDataBound event.

    The code:

    <asp:GridView ID="GridView1" runat="server" Font-Bold="False" 
                        Font-Names="Calibri" Font-Size="12pt" OnRowDataBound="GridView1_RowDataBound">
                        </asp:GridView>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country"), new DataColumn("Salary") });
                    dt.Rows.Add(1, "John Hammond", "United States", 70000);
                    dt.Rows.Add(2, "Mudassar Khan", "India", 40000);
                    dt.Rows.Add(3, "Suzanne Mathews", "France", 30000);
                    dt.Rows.Add(4, "Robert Schidner", "Russia", 50000);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                e.Row.Cells[1].Visible = false;
                e.Row.Cells[2].Visible = false;
                e.Row.Cells[3].Visible = false;
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 18, 2019 2:08 AM

All replies

  • User288213138 posted

    Hi Gopi.MCA,

    i want to hide 3 column how to do so

    You can set columns invisible in your GridView's RowDataBound event.

    The code:

    <asp:GridView ID="GridView1" runat="server" Font-Bold="False" 
                        Font-Names="Calibri" Font-Size="12pt" OnRowDataBound="GridView1_RowDataBound">
                        </asp:GridView>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country"), new DataColumn("Salary") });
                    dt.Rows.Add(1, "John Hammond", "United States", 70000);
                    dt.Rows.Add(2, "Mudassar Khan", "India", 40000);
                    dt.Rows.Add(3, "Suzanne Mathews", "France", 30000);
                    dt.Rows.Add(4, "Robert Schidner", "Russia", 50000);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                e.Row.Cells[1].Visible = false;
                e.Row.Cells[2].Visible = false;
                e.Row.Cells[3].Visible = false;
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 18, 2019 2:08 AM
  • User-807418713 posted

    Hi

    i used above code im getting one problem

    column it hided perfect still it showing header how to solve this?

    Thanking You

    Wednesday, September 18, 2019 2:50 AM
  • User288213138 posted

    Hi Gopi.MCA,

    Can you show me your aspx code?

    In my side, the header also be hided.

    Best regards,

    Sam

    Wednesday, September 18, 2019 3:09 AM