c# - GridView 按代码隐藏列

标签 c# asp.net

我想在我的 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/

相关文章:

asp.net - 连接到远程 sql 时我的用户名和密码是否以明文形式发送

c# - GDI+ 中的 System.Drawing.Image.Save 发生一般性错误

c# - 系统.安全.SecurityException : That assembly does not allow partially trusted callers

c# - 如何从pdb读取源路径

c# - 使用 .NET 进行多线程文件处理

javascript - 如何禁用 cookie 覆盖?

asp.net - context.Get() 和它的通用版本有什么区别?

c# - 从代码隐藏 Web 方法调用 Javascript 方法

c# - 从具有相同名称的多个元素和 xml 中另一个元素的属性值获取属性值

c# - 模拟CloudStorageAccount和CloudTable for Azure表存储