GridView 按代码隐藏列 [英] GridView Hide Column by code
问题描述
我想在我的 GridView 中隐藏 ID 列,我知道代码
I want to hide ID column in my GridView, I knew the code
GridView1.Columns[0].Visible = false;
但令人惊讶的是,我的 GridView
列的计数属性是 0 !虽然我可以在 GridView
中看到数据,但有什么想法吗?
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?
谢谢,
更新:
这是填充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;
}
推荐答案
GridView.Columns.Count
当您的 GridView 将其 AutoGenerateColumns
属性设置为 true
(默认为 true
).
GridView.Columns.Count
will always be 0 when your GridView has its AutoGenerateColumns
property set to true
(default is true
).
您可以显式声明您的列并将 AutoGenerateColumns
属性设置为 false
,或者您可以在代码隐藏中使用它:
You can explicitly declare your columns and set the AutoGenerateColumns
property to false
, or you can use this in your codebehind:
GridView.Rows[0].Cells.Count
在绑定 GridView 数据后获取列数,或者:
to get the column count once your GridView data has been bound, or this:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[index].Visible = false;
}
使用 GridView 的 RowDataBound
事件将列设置为不可见.
to set a column invisible using your GridView's RowDataBound
event.
这篇关于GridView 按代码隐藏列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!