1

I have gridview and I want to hide a column after databind to gridview but I get the error below.

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

My C# code is below,

 protected void grid_all_posts_DataBound(object sender, EventArgs e)
{
    if (grid_all_posts.Columns[1].Visible)
    {
        grid_all_posts.Columns[1].Visible = false;
    }
}

// Read all posts and fill  gridview
    //////////////////////////////////////////////////
    DbCommand dbCommand2;
    dbCommand2 = db.GetStoredProcCommand("SP_Select_News");
    db.AddInParameter(dbCommand2, "UserId", DbType.Guid, new Guid(Session["UserId"].ToString().Trim()));
    DataSet ds = db.ExecuteDataSet(dbCommand2);
    grid_all_posts.DataSource = ds;
    grid_all_posts.DataBind();
    //////////////////////////////////////////////////

My ASPX code,

<asp:gridview runat="server" ID="grid_all_posts" OnRowDataBound="grid_all_posts_DataBound"></asp:gridview>

What do you think the problem is? How I can hide the first column

Arif Yilmaz
  • 121
  • 2
  • 4
  • 14

2 Answers2

1

Try like below it will work....

    DbCommand dbCommand2;
    dbCommand2 = db.GetStoredProcCommand("SP_Select_News");
    db.AddInParameter(dbCommand2, "UserId", DbType.Guid, new Guid(Session["UserId"].ToString().Trim()));
    DataSet ds = db.ExecuteDataSet(dbCommand2);
    grid_all_posts.DataSource = ds;
    grid_all_posts.DataBind();

**//after Databind Write the below code**

    if (grid_all_posts.Columns.Count > 0)
       grid_all_posts.Columns[0].Visible = false;
    else
    {
       grid_all_posts.HeaderRow.Cells[0].Visible = false;
       foreach (GridViewRow gvr in grid_all_posts.Rows)
       {
           gvr.Cells[0].Visible = false;
       }
     }
Pandian
  • 8,848
  • 2
  • 23
  • 33
  • here's the VB version of Pandian's code If dgvMyLeave.Columns.Count > 0 Then dgvMyLeave.Columns(0).Visible = False Else dgvMyLeave.HeaderRow.Cells(0).Visible = False For Each gvr As GridViewRow In dgvMyLeave.Rows gvr.Cells(0).Visible = False Next End If – DJDave Mar 04 '14 at 09:47
0

A simple and versatile approach for this has been bugging me for ages - something that does not require any kind of column counting, I eventually found it today; DataControlFieldCell.ContainingField.HeaderText

Private Sub GridView_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView.RowDataBound

    For Each r As GridViewRow In GridView.Rows

        If r.RowType = DataControlRowType.DataRow Then
            For Each c As DataControlFieldCell In r.Cells

                Dim h As String = c.ContainingField.HeaderText
                If h <> "ColumnName" Then c.ContainingField.Visible = False
                If IsNumeric(c.Text) Then c.Text = Format(CInt(c.Text), "#,##0")

            Next
        End If

    Next

End Sub

I loop through the DataControlFieldCells as I perform some formatting on the values in my fields as well (as illustrated above) but you could just loop through the header I imagine if you wanted to cut it down further still.

See msdn for more details.

Ryan Gillies
  • 413
  • 4
  • 8
  • 19