0

I am trying to hide the gridview coumn[1]. But it shows error like

Index was out of range. Must be non-negative and less than the size of
the collection parameter name:index.

How can I perform this. I surfed a lot in internet but I didnt get a proper solution. My code I mentioned below.It is a web based platform.

protected void Page_Load(object sender, EventArgs e)
 {
   if (!IsPostBack)
     {
      qry = "select Id,Name,Age,Sex,Dob from Sample where Code='123';
      dr = conn.query(qry);
      GridView1.DataSource = dr;
      GridView1.DataBind();
      GridView1.Columns[1].Visible = false;
      }
 }

In this same way column[0] is hiding. My grid view is

 <asp:GridView ID="GridView1" HorizontalAlign="Center" runat="server" 
      onrowcommand="GridView1_RowCommand">
    <Columns>
       <asp:TemplateField>
          <ItemTemplate>
             <asp:LinkButton ID="LinkButton1" runat="server"
                   CommandArgument='<%# Eval("ID")%>'>Delete</asp:LinkButton>
             </ItemTemplate>
         </asp:TemplateField>
    </Columns>
</asp:GridView>
शेखर
  • 17,412
  • 13
  • 61
  • 117
Semil Sebastian
  • 111
  • 1
  • 5
  • 18

1 Answers1

1

You need to use RowDataBound to hide a column. You can use the code below

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

Similar question on SO

  1. GridView Hide Column by code
Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117