tags:

views:

67

answers:

3

Hi all,

I want to hide ID column in my GridView, I knew the code

GridView1.Columns[0].Visible = false;

but the surprise was that my count property for my GridView columns is 0 !!! while I can see data in the GridView, so any ideas?

Thank you,

Update:

here is the complete code for the method which populate the GridView

public DataSet GetAllPatients() { SqlConnection connection = new SqlConnection(this.ConnectionString);

    String sql = "SELECT [ID],[Name],[Age],[Phone],[MedicalHistory],[Medication],[Diagnoses] FROM [dbo].[AwadyClinc_PatientTbl]order by ID desc";

    SqlCommand command = new SqlCommand(sql, connection);

    SqlDataAdapter da = new SqlDataAdapter(command);

    DataSet ds = new DataSet();

    da.Fill(ds);

    return ds;

}
+1  A: 

If you wanna hide that column while grid populating, you can do it in aspx page itself like this

<asp:BoundField DataField="test" HeaderText="test" Visible="False" />
Bala
A: 

GridView.Columns.Count will be 0 when you set AutoGenerateColumns="true" in your GridView.

You could try explicitly declaring your columns and setting AutoGenerateColumns="false", or you could try using this in your codebehind:

GridView.Rows[0].Cells.Count

to get the column count once the data has been bound, or this:

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

to set a column invisible using the GridView's RowDataBound event.

Brissles
A: 
naval