【问题标题】:Why column count in gridview is 0 when columns are binded dynamically from datatable为什么从数据表动态绑定列时gridview中的列数为0
【发布时间】:2017-02-13 08:47:24
【问题描述】:

我有一个 gridview,当 gridview 直接绑定到数据源并且列未静态定义时,它显示列数为 0。我无法解决这个问题。

我尝试了以下代码:

foreach (GridViewRow row in gv_services.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < gv_services.Columns.Count; i++)
        {
            if (row.Cells[i].Controls[0].GetType() == typeof(CheckBox))
            {
                CheckBox checkBox = row.Cells[i].Controls[0] as CheckBox;

                checkBox.Enabled = true;
                //  checkBox.CheckedChanged += new EventHandler(chck_CheckedChanged);
            }      
        }
    }    
}

【问题讨论】:

  • autogeneratedcolumns==true column-count 始终为 0。这是设计使然。但是您甚至没有使用过GridView.Columns 属性,因此不清楚实际问题是什么。
  • 实际上代替了cells.count 我使用了columns.count ..任何人都知道我们该如何解决这个问题????
  • 上面的代码不能正常工作。在实际代码中,尽管有 cells.count 我使用了 columns.count
  • 再次,始终显示真实代码。到底有什么问题?也显示相关的 aspx 代码。以上代码需要在哪里执行?
  • 我已经编辑了我上面的代码

标签: asp.net gridview webforms


【解决方案1】:

如果我理解正确,您正在尝试遍历 gridview 行并根据您的条件启用复选框

试试这个代码

foreach (GridViewRow row in yourgridview.Rows)
        {
            CheckBox myChk = row.FindControl("myControlName") as CheckBox;
            if (myChk != null)
            {
                myChk.Enabled = true;
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 2014-02-13
    • 1970-01-01
    相关资源
    最近更新 更多