编程gridview复选框以自动检查相关行

时间:2016-04-28 09:00:25

标签: c# asp.net gridview checkbox

我想对复选框进行编程,以便在选择行时,程序将读取gridview行第一列中的值,并自动检查在第一列中也具有该值的所有行。我能够使用数组来存储值并遍历每一行,检查这个值是否匹配。如果匹配,则选中该复选框。这有效,但是解决方案非常有限。当我尝试撤消操作时,它只是自动重新检查该值,因为该值仍在数组中,我不知道如何区分检查或取消检查操作。它完全基于变革事件。

        int count = 0;
        foreach (GridViewRow gvrow in GridView1.Rows)
        {
            CheckBox chk = (CheckBox)gvrow.FindControl("chkRow");

            if (chk.Checked)
            {
                count++;
            }
        }
        string [] dnum = new string[count];
        int counter = 0;

        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox myCheckBox = row.FindControl("chkRow") as CheckBox;
            if (myCheckBox.Checked)
            {
                if (counter > 0)
                {
                    int number = counter - 1;
                    if (row.Cells[1].Text != dnum[number])
                    {
                        dnum[counter] = row.Cells[1].Text;
                        counter++;
                    }
                }
                else
                {
                    dnum[counter] = row.Cells[1].Text;
                    counter++;
                }
            }
        }
        return dnum;
    }

数组dnum应返回已检查行的第一列值。有了这个,我可以遍历每一行,检查是否需要检查任何复选框。

 foreach (GridViewRow gvrow in GridView1.Rows)
 {
     CheckBox ChkBox = (CheckBox)gvrow.FindControl("chkRow");
     if (ChkBox.Checked == true)
     {
        foreach (string s in first)
        {
           if (gvrow.Cells[1].Text == s)
           {
              ChkBox.Checked = true;
           }
        }
     }

但现在我无法弄清楚如何反转过程,即当我取消选中一个时,所有具有相同的第一列值必须取消选中,而只是重新检查,因为该值仍在数组中。我对完全不同的方法持开放态度。

非常感谢, 尼古拉斯

2 个答案:

答案 0 :(得分:0)

我不清楚你的问题,也许你正在寻找这个?

在检查ChkBox循环中使用此代码而不是旧代码。

MariaDB []> select id from mytab;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
+----+
15 rows in set (0.00 sec)

MariaDB []> SELECT id
    -> FROM mytab
    -> ORDER BY IF(id=18,155,id*10);
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 18 |
| 16 |
| 17 |
| 19 |
+----+
15 rows in set (0.00 sec)

MariaDB []>

使用ChkBox.Checked = !ChkBox.Checked ,它会在复选框中反转值。 如果这是真的,它将变得虚假。如果它是假的,它将成为现实。

答案 1 :(得分:0)

请给出另一个例子,不确定你是如何在你的代码中实现的,所以这里就是......

例如,您的gridview如下所示:

    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1"
                        OnCheckedChanged="CheckBox1_Click"
                        AutoPostBack="True" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

在你的代码背后.. 下面的mockData数据只是我们gridview的示例数据,用于演示基于所选复选框属性和列[1]值检查和取消检查相同值。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var mockData = new[] {"1", "1", "2", "1", "3", "2"};
            GridView1.DataSource = mockData;
            GridView1.DataBind();
        }
    }

    protected void CheckBox1_Click(object sender, EventArgs e)
    {
        var chkBox = (CheckBox) sender;
        var selectedRow = chkBox.Parent.Parent;
        var itemValue = ((GridViewRow)selectedRow).Cells[1].Text; 

        foreach (var chkItem in GridView1.Rows.Cast<GridViewRow>()
            .Where(item => item.Cells[1].Text == itemValue)
            .Select(item => item.Cells[0].FindControl("CheckBox1")).OfType<CheckBox>())
        {
            chkItem.Checked = chkBox.Checked;
        }
    }
}

如果我点击一行中值为“1”的复选框,则所有值为“1”的行应自动设置为所选复选框的属性。

如果我误解了你的要求,请告诉我。你可以申请你的代码并让我知道它是否有效:-)欢呼。

相关问题