由code GridView中隐藏列 [英] GridView Hide Column by code
问题描述
我想隐藏ID列在我的GridView中,我知道了code
GridView1.Columns [0] =。可见假的;
但出人意料的是,我为我的GridView列计数属性为0!而我可以看到在GridView的数据,所以任何想法?
感谢您,
更新:
下面是完整的code,对于它填充GridView的方法
公共数据集GetAllPatients()
{
SqlConnection的连接=新的SqlConnection(this.ConnectionString);
字符串的sql =SELECT [ID],[名],[年龄],[电话],[MedicalHistory],[药物],[诊断] FROM [DBO]。[ AwadyClinc_PatientTbl] ORDER BY ID DESC; 的SqlCommand命令=新的SqlCommand(SQL,连接); SqlDataAdapter的大=新SqlDataAdapter的(命令); DataSet的DS =新的DataSet(); da.Fill(DS); 返回DS;}
GridView.Columns.Count
将始终为0,当你的GridView有的AutoGenerateColumns
属性设置为真正
(默认为真正
)。
您可以显式声明的列和的AutoGenerateColumns
属性设置为假
,也可以在使用你的codebehind:
GridView.Rows [0] .Cells.Count
要得到列数,一旦你的GridView的数据已经绑定,或这样的:
保护无效GridView_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
e.Row.Cells [指数]。可见=虚假的;
}
使用GridView的的RowDataBound
事件来设置一个列不可见。
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;
}
GridView.Columns.Count
will always be 0 when your GridView has its AutoGenerateColumns
property set to true
(default is true
).
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
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;
}
to set a column invisible using your GridView's RowDataBound
event.
这篇关于由code GridView中隐藏列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!