当我单击 Grid 中的 EDIT 按钮时,它使所有字段都可编辑并提供两个选项(更新和取消)。在这个网格中,有两个下拉列表、3 个日历和一些文本框。如果我单击更新,则数据库中的所有文本框值都会更新,但数据库中的所有其他字段(下拉列表和日历)值自动为 NULL。
下面是主页面GridView代码:
<asp:GridView ID="GridView1" runat="server" CellPadding="5" ForeColor="#333333" width="1000px"
GridLines="None" OnPageIndexChanging="gridView_PageIndexChanging"
AllowSorting="True" OnSorting="gridView_Sorting" AutoGenerateColumns="False" OnRowUpdating="GridView1_RowUpdating"
BorderStyle="Outset" CellSpacing="1" Font-Names="Cambria"
Font-Size="Small" AllowPaging="True" ShowFooter="True"
ShowHeaderWhenEmpty="True"
DataSourceID="SqlDataSource1" onselectedindexchanged="GridView1_SelectedIndexChanged"
>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="case_number" HeaderText="Case Number"
SortExpression="case_number" />
<asp:BoundField DataField="case_name" HeaderText="Case Name"
SortExpression="case_name" />
<asp:BoundField DataField="Case_Type_Text" HeaderText="Case Type"
SortExpression="Case_Type_Text" />
<asp:TemplateField HeaderText="Case Status" SortExpression="Case_Status_Text">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource3" DataTextField="Case_Status_Text" DataValueField="Case_Status_Text"
SelectedValue='<%# Bind("Case_Status_Text") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Case_Status_Text") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="assigned_date" HeaderText="Assigned Date"
SortExpression="assigned_date" DataFormatString="{0:d}" />
<asp:BoundField DataField="assigned_to" HeaderText="Assigned To"
SortExpression="assigned_to" />
<asp:TemplateField HeaderText="Date Withdrawn" SortExpression="date_withdrawn">
<EditItemTemplate>
<cc1:DatePicker ID="DatePicker5" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server"
Text='<%# Bind("date_withdrawn", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Delivered" SortExpression="date_delivered">
<EditItemTemplate>
<cc1:DatePicker ID="DatePicker7" runat="server"
/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server"
Text='<%# Bind("date_delivered", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QC By" SortExpression="qc_by">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server"
DataSourceID="SqlDataSource4" DataTextField="User_Name" DataValueField="User_Name"
SelectedValue='<%# Bind("qc_by") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("qc_by") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QC Date" SortExpression="qc_date">
<EditItemTemplate>
<cc1:DatePicker ID="DatePicker6" runat="server"
/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("qc_date", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Additional Notes">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("additional_notes") %>' TextMode="MultiLine"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("additional_notes") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ButtonType="Button"
CausesValidation="False" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:con %>"
ProviderName="<%$ ConnectionStrings:con.ProviderName %>"
SelectCommand="SELECT * FROM [View_Intakesheet]"
UpdateCommand="UPDATE intakesheet SET case_number = @case_number, case_name=@case_name, Case_Type=@case_type, Case_Status = @case_status, assigned_date = @assigned_date, assigned_to = @assigned_to, date_withdrawn= @date_withdrawn, date_delivered= @date_delivered, qc_by = @qc_by, qc_date=@qc_date, additional_notes = @additional_notes WHERE (case_number = @case_number)">
<UpdateParameters>
<asp:Parameter Name="case_number"/>
<asp:Parameter Name="case_name" />
<asp:Parameter Name="case_type" />
<asp:Parameter Name="case_status" />
<asp:Parameter Name="assigned_date" Type="DateTime" />
<asp:Parameter Name="assigned_to" />
<asp:Parameter Name="date_withdrawn" Type="DateTime" />
<asp:Parameter Name="date_delivered" Type="DateTime" />
<asp:Parameter Name="qc_by" />
<asp:Parameter Name="qc_date" Type="DateTime" />
<asp:Parameter Name="additional_notes" />
</UpdateParameters>
</asp:SqlDataSource>
</td>
</tr>
以下是更新事件::
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
DropDownList ct = (DropDownList)row.FindControl("case_type");
DropDownList cs = (DropDownList)row.FindControl("case_status");
con.Open();
SqlCommand cmd = new SqlCommand("UPDATE intakesheet SET case_number = @case_number, case_name = @case_name, Case_Type = Case_Type, Case_Status = Case_Status, assigned_date = assigned_date, assigned_to = assigned_to, date_withdrawn= date_withdrawn, date_delivered= date_delivered, qc_by = qc_by, qc_date=qc_date, additional_notes = additional_notes WHERE (case_number = case_number)", immigration);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
con.Close();
bind();
}
public void bind()
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from intakesheet", con);
DataSet ds = new DataSet();
da.Fill(ds, "intakesheet");
//GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
con.Close();
}