1

当我单击 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();

}
4

2 回答 2

1
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);

更改此行

主要是这些问题

Case_Type = Case_Type, Case_Status = Case_Status, 在更新查询中

 DropDownList ct = (DropDownList)row.FindControl("case_type");
    DropDownList cs = (DropDownList)row.FindControl("case_status")

;

如果您已绑定其值,则发送 ct.SelectedIndex.value 如果要发送 id

如果你想发送选定的索引然后发送 dt.SelectedText

喜欢

Case_Type = ct.SelectedItem.Text, Case_Status = cs.SelectedItem.Text

检查这一行

   DropDownList cs = (DropDownList)row.FindControl("DropDownList3")

类似地为案例类型提供正确的 ID

在上面的行中,您正在传递下拉列表的 id,您应该发送其选定索引的选定值

变化

SqlCommand cmd = new SqlCommand("更新摄入量表 SET case_number = @case_number,case_name = @case_name,Case_Type = Case_Type,Case_Status = Case_Status,assigned_date =assign_date,assign_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)",移民);

cmd.parameters.add("@case_number",sqldbtype.nvarchar).value = case_number // the string variable of case number

对其他参数做类似的事情

于 2013-08-01T04:39:36.500 回答
0

我也遇到了未更新的 GridView 的问题。检查 GridView 的 RowUpdating 事件中的 e.Newvalues 字典表明记录的旧值正在发送到数据库 UPDATE 查询。DataKeyNames 不是我的问题;我把它设置正确了。我的 SELECT 查询的 WHERE 子句针对表单上的 TextBox 引用了一个控制参数。我无意中将此文本框的 EnableViewState 设置为“False”。因此,GridView 在 UPDATE 发生之前重新绑定自身。将 TextBox 上的 EnableViewState 设置为“True”解决了该问题。这是查看更新参数的代码:

    Protected Sub MyGridView_RowUpdating _
    (sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) _
    Handles MyGridView.RowUpdating

    ' Inspect the parameters being sent to the database for an ASP NET GridView UPDATE.

    Dim I As Integer

    I = 0

    For Each MVO As System.Collections.DictionaryEntry In e.OldValues
        If MVO.Value Is DBNull.Value OrElse MVO.Value Is Nothing Then
            Debug.Print(I.ToString + ": " + MVO.Key + " Value:  ")
        Else
            Debug.Print(I.ToString + ": " + MVO.Key + " Value:  " + MVO.Value.ToString)
        End If

        I += 1
    Next MVO

    I = 0
    For Each MVN As System.Collections.DictionaryEntry In e.NewValues
        If MVN.Value Is DBNull.Value OrElse MVN.Value Is Nothing Then
            Debug.Print(I.ToString + ": " + MVN.Key + " Value:  ")
        Else
            Debug.Print(I.ToString + ": " + MVN.Key + " Value:  " + MVN.Value.ToString)
        End If
        I += 1
    Next MVN


End Sub
于 2014-09-04T13:59:09.357 回答