使用GridView自带的编辑功能,只能一条一条记录的修改,如果要求批量修改,必须使用模板。
对于ASPxGridView,方法如下:
(1)aspx文件
<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>
(2)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();
}
}
而对于VS2005自带的GridView控件,方法要简单一些,如下:
(1)aspx文件
<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False" DataKeyNames="CourseID"
DataSourceID="DSCourse" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
<Columns>
<asp:BoundField DataField="CourseName" HeaderText="CourseName" SortExpression="CourseName" />
<asp:TemplateField HeaderText="Credit" SortExpression="Credit">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Credit") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CreditHour" SortExpression="CreditHour">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CreditHour") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="zp" HeaderText="总分" SortExpression="zp" />
</Columns>
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
(2)cs文件
protected void ASPxButton2_Click(object sender, EventArgs e)
{
for (int i = 0; i < grid1.Rows.Count; i++)
grid1.UpdateRow(i, false);
}
很奇怪,ASPxGridView控件为何没有UpdateRow方法?