简体   繁体   中英

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. 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.

Here is what I'm kicking around right now...

  1. Add a template field to my gridview that provides a checkbox (Done!)
  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.
  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.
  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.
    • 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.

For the sake of understanding what I'm tinkering with here I've provided a screen shot of the left side of the GridView:

在此处输入图片说明

Thoughts on building the array from the CheckBoxes?

UPDATE 04/13/17 1PM Central US

Adding the code I added to approach the population of an array from the CheckBoxes per example #2 below...

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).

Option 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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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