【问题标题】:How to get the value of checkbox in gridview using C#?如何使用 C# 在 gridview 中获取复选框的值?
【发布时间】:2012-07-08 13:10:41
【问题描述】:

我想在GridView 中获取CheckBox 的值。我可以使用事件RowDataBound 绑定GridView。但是在ButtonClick 事件中,即使我检查了CheckBoxCheckBox 的值也始终为假。

代码背后:

    protected void GridView1_RowDatabound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList dddate = (DropDownList)e.Row.FindControl("DdlDate");
            DropDownList ddYear = (DropDownList)e.Row.FindControl("DdlYear");
            for (int i = System.DateTime.Now.Year; i > (System.DateTime.Now.Year) - 100; i--)
                ddYear.Items.Add(Convert.ToString(i));
            for (int i = 1; i < 32; i++)
                dddate.Items.Add(Convert.ToString(i)); 
        }
    }
    protected void btnRetrieveCheck_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("chkSel");
            if (cb != null && cb.Checked)
            {

                DropDownList dddate = (DropDownList)row.FindControl("DdlDate");              //Bind data to the GridView control.
                DropDownList ddYear = (DropDownList)row.FindControl("DdlYear");
                DropDownList ddmonth = (DropDownList)row.FindControl("DdlMonth");

            }
        }
    }

【问题讨论】:

  • 你的chkSel是动态创建还是设计时间?

标签: c# wpf gridview checkbox


【解决方案1】:

您应该连接到复选框上的 Checked 事件而不是 click 事件。

你的问题不是很清楚。你能发布更多的代码。例如,什么是 btnRetrieveCheck - 它是实际的复选框吗?请使用您的 XAML 更新问题,以便我们可以查看已到位的绑定。

【讨论】:

  • 我有一个带有几列的网格视图和一个包含复选框的模板字段。当用户单击复选框时,我想用 1(如果选中)或 0(如果未选中)更新数据库。这就是我使用 btnRetrieveCheck 的原因
  • 您能否更新您的问题以包含 DataGrid 的 xaml。我还是不明白。我对 EndEdit 的评论也适用于 WinForms,因此我将其删除。
猜你喜欢
  • 2012-09-23
  • 2012-03-06
  • 1970-01-01
  • 2014-07-14
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
相关资源
最近更新 更多