使用复选框更新多行
本文关键字:更新 复选框 | 更新日期: 2023-09-27 18:04:11
创建一个gridview,允许用户在gridview中每条记录旁边勾选复选框。在选择复选框之后,他们可以选择一个按钮来改变被选中记录的每个状态。下面的代码做更新,如果我检查1或2行,但错误,如果我尝试一次更新所有的记录。
<asp:GridView ID="GridViewwork" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSourcework" Width="90%" ShowFooter="True"
onselectedindexchanged="GridViewwork_SelectedIndexChanged"
DataKeyNames="WorkID" CellPadding="0" CellSpacing="2" GridLines="None">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="CheckBoxcomplete" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DIAS Employee" SortExpression="employee">
<EditItemTemplate>
<asp:TextBox ID="employee_txt" runat="server" Text='<%# Bind("employee") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("employee") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btSubmit" runat="server" Text="Mark as Complete" onclick=" btButton_Click"></asp:Button>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File Number" SortExpression="filenumber">
<EditItemTemplate>
<asp:TextBox ID="filenumber_txt" runat="server" Text='<%# Bind("filenumber") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("filenumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" SortExpression="action">
<EditItemTemplate>
<asp:TextBox ID="action_txt" runat="server" Text='<%# Bind("action") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("action") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Start Date" SortExpression="workdate">
<EditItemTemplate>
<asp:TextBox ID="workdate_txt" runat="server" Text='<%# Bind("workdate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("workdate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remarks" SortExpression="remarks">
<EditItemTemplate>
<asp:TextBox ID="remarks_txt" runat="server" Text='<%# Bind("remarks") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("remarks") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
背后的代码protected void btButton_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridViewwork.Rows)
{
CheckBox chkUpdate = (CheckBox)row.FindControl("CheckBoxcomplete");
if (chkUpdate.Checked)
{
SqlConnection sqlcon = <my connection.
string query = "Update WorkFlowActivity set completedate=@completedate, workstatus=@workstatus where WorkID = @WorkID";
SqlCommand sqlcom = new SqlCommand(query, sqlcon);
sqlcom.Parameters.AddWithValue("@completedate", DateTime.Now.ToString());
sqlcom.Parameters.AddWithValue("@workstatus", "Yes");
sqlcom.Parameters.AddWithValue("@WorkID", GridViewwork.DataKeys[row.RowIndex].Value);
sqlcon.Open();
try
{
sqlcom.ExecuteNonQuery();
GridViewwork.DataBind();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
sqlcon.Close();
}
}
}
当我尝试一次更新所有记录时,我得到指数超出范围。必须非负且小于集合的大小。参数名称:index
这是我添加参数@WorkID的行。
网格视图行集合包括数据行(包括页眉,页脚等)
为了让你克服它,你需要检查行的索引不是-1,而从GridView行获取数据。类似的;
if(row.RowIndex != -1)
string dataValue = row["columnName"].ToString();
在您的示例中,您需要在从行中获取复选框控件之前完成此操作。
希望对你有帮助。
编码. .快乐! !
Nayan