我想在我的 GridView 中隐藏 ID 列,我知道代码
GridView1.Columns[0].Visible = false;
但令人惊讶的是,我的 GridView
列的计数属性为 0!虽然我可以在 GridView
中看到数据,所以有什么想法吗?
谢谢,
更新:
这里是填充 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 将其 AutoGenerateColumns
属性设置为 true
时,
GridView.Columns.Count
将始终为 0(默认为 true
)。
您可以显式声明您的列并将 AutoGenerateColumns
属性设置为 false
,或者您可以在代码隐藏中使用它:
GridView.Rows[0].Cells.Count
在绑定(bind) GridView 数据后获取列数,或者这样:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[index].Visible = false;
}
使用 GridView 的 RowDataBound
事件将列设置为不可见。
关于c# - GridView 按代码隐藏列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3819247/