[英]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... 这就是我现在要踢的东西...
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.