(转)aspxgridview记录的批量修改
使用GridView自带的编辑功能,只能一条一条记录的修改,如果要求批量修改,必须使用模板。
<dxwgv:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" DataSourceID="DSCourse" KeyFieldName="CourseID" ClientInstanceName="grid" > <SettingsPager Visible="False"> </SettingsPager> <Columns> <dxwgv:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0"> </dxwgv:GridViewCommandColumn> <dxwgv:GridViewDataTextColumn Caption="序号" VisibleIndex="1" Width="25px"> <DataItemTemplate> <%# Container.ItemIndex+1%> </DataItemTemplate> </dxwgv:GridViewDataTextColumn> <dxwgv:GridViewDataTextColumn FieldName="CourseName" VisibleIndex="2"> </dxwgv:GridViewDataTextColumn> <dxwgv:GridViewDataTextColumn FieldName="Credit" VisibleIndex="3"> <DataItemTemplate> <dxe:ASPxTextBox ID="ASPxTextBox1" runat="server" Text='<%# Bind("Credit") %>' Value='<%# Bind("Credit") %>' Width="170px"> </dxe:ASPxTextBox> </DataItemTemplate> </dxwgv:GridViewDataTextColumn> <dxwgv:GridViewDataTextColumn FieldName="CreditHour" VisibleIndex="4"> <DataItemTemplate> <dxe:ASPxTextBox ID="ASPxTextBox2" runat="server" Text='<%# Bind("CreditHour") %>' Value='<%# Bind("CreditHour") %>' Width="170px"> </dxe:ASPxTextBox> </DataItemTemplate> </dxwgv:GridViewDataTextColumn> </Columns> </dxwgv:ASPxGridView>
<asp:AccessDataSource ID="DSCourse" runat="server" DataFile="~/WebCourse.mdb" SelectCommand="SELECT [CourseID], [CourseName], [Credit], [CreditHour],[zp] FROM [Course]" UpdateCommand="update Course set Credit=?,CreditHour=?,zp=Credit+CreditHour where CourseID=?"> <UpdateParameters> <asp:Parameter Name="Credit" Type="Single" /> <asp:Parameter Name="CreditHour" Type="Single" /> <asp:Parameter Name="CourseID" Type="Int32" /> </UpdateParameters> </asp:AccessDataSource>
CS代码文件
protected void ASPxButton3_Click(object sender, EventArgs e) { for (int i = 0; i < grid.VisibleRowCount; i++) { ASPxTextBox tb1 = grid.FindRowCellTemplateControl(i, null, "ASPxTextBox1") as ASPxTextBox; ASPxTextBox tb2 = grid.FindRowCellTemplateControl(i, null, "ASPxTextBox2") as ASPxTextBox; DSCourse.UpdateParameters["Credit"].DefaultValue = tb1.Text; DSCourse.UpdateParameters["CreditHour"].DefaultValue = tb2.Text; DSCourse.UpdateParameters["CourseID"].DefaultValue = grid.GetRowValues(i, "CourseID").ToString(); DSCourse.Update(); } }