创建一个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的行。真的不确定我哪里出错了。
答案 0 :(得分:-1)
网格视图行的集合是所有行以及数据行(包括页眉,页脚等)
为了让你过来,你需要在从GridView行获取数据时检查行的索引不是-1。喜欢的东西;
if(row.RowIndex != -1)
string dataValue = row["columnName"].ToString();
在您的情况下,您需要在从行中获取复选框控件之前完成此操作。
希望这有帮助。
快乐编码.. !!
拿烟</ P>