简体   繁体   English

从gridview C#中的复选框构建数组

[英]Building an array from checkboxes in gridview C#

I am working on building a process with my gridview that would allow a user to provide one edit and save it to multiple records. 我正在使用自己的gridview构建一个流程,该流程将允许用户提供一个编辑并将其保存到多个记录中。 So far I've gotten to this point of planning but not sure of how the best way would be to build an array of record ID's to be included in this edit. 到目前为止,我已经到了规划的目的,但是不确定如何最好的方法是构建一个包含在此编辑中的记录ID数组。

Here is what I'm kicking around right now... 这就是我现在要踢的东西...

  1. Add a template field to my gridview that provides a checkbox (Done!) 将模板字段添加到我的gridview中,该字段提供一个复选框(完成!)
  2. Provide a DropDownList with all the column headers in the grid and associate the source tables column name as the value for the entries in the DDL. 为DropDownList提供网格中所有列标题,并将源表的列名称与DDL中条目的值相关联。
  3. Upon selecting an item on the DDL, it would OnSelectedIndexChanged toss up an edit window to edit the content of the column selected in the DDL. 在DDL上选择一个项目后,它将OnSelectedIndexChanged抛出一个编辑窗口以编辑DDL中所选列的内容。
  4. Upon 'Save' the CodeBehind would spin through the array writing the same value to each of the rows in the column chosen in the drop down list. “保存”后,CodeBehind将旋转数组,将相同的值写入下拉列表中所选列的每一行。
    • Thinking about this I believe that I would capture the data in the first record in the array for the column of focus so as to provide a means of editing existing content in the column. 考虑到这一点,我相信我会在焦点列的数组中捕获第一条记录中的数据,以便提供一种编辑列中现有内容的方法。

Everything except the CheckBoxes in the GridView would be processed outside of the GridView and upon completion the GridView would be redrawn clearing the CheckBoxes and displaying the updated column values in those cells that were modified. GridView中除CheckBox之外的所有内容都将在GridView之外进行处理,完成后将重新绘制GridView,清除CheckBox并在修改的那些单元格中显示更新的列值。

For the sake of understanding what I'm tinkering with here I've provided a screen shot of the left side of the GridView: 为了理解这里要修改的内容,我提供了GridView左侧的屏幕截图:

在此处输入图片说明

Thoughts on building the array from the CheckBoxes? 关于通过CheckBoxes构建数组的想法?

UPDATE 04/13/17 1PM Central US 更新04/13/17 1PM美国中部

Adding the code I added to approach the population of an array from the CheckBoxes per example #2 below... 添加我添加的代码以通过下面每个示例#2的CheckBoxes处理数组的填充...

protected void ColumnSelectDDL_TextChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in ActVulListGV.Rows)
    {
        var ri = -1;

        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chk = (row.Cells[0].FindControl("chkid") as CheckBox);

            if (chk.Checked)
            {
                // Create and append your array here
                var recnumbers = new int[0];
                ++ri;
                  {
                     Label REC = (row.Cells[1].FindControl("RecID") as Label);
                     recnumbers[ri] = Convert.ToInt32(REC.Text);
                  }
                   recnumbers.ToList().ForEach(i => Console.WriteLine(i.ToString()));
              }
          }
      }
  }

You can bind the array of checkboxes at the time of Rowdatabound (Option 1) or after Gridview bound onclick on Save button create the loop of grid rows (Option 2). 您可以在行数据绑定时(选项1)或在绑定Gridview时绑定复选框数组,然后单击“保存”按钮以创建网格行循环(选项2)。

Option 1: 选项1:

 protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
  {

   if (e.Row.RowType == DataControlRowType.DataRow) {


     CheckBox chk = (row.Cells[0].FindControl("chkid") as CheckBox);
        if (chk.Checked)
        {

         //Create the hiddenfield or viewstate which you can access page level.
        }
     }
 }

Option 2 : 选项2:

foreach (GridViewRow row in GridView1.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chk = (row.Cells[0].FindControl("chkid") as CheckBox);
        if (chk.Checked)
        {

          // Create and append your array here

        }
    }
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM