Gridview更新无法正常工作

时间:2013-08-01 04:28:59

标签: asp.net .net datagridview

当我点击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>

以下是Update事件::

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

}

2 个答案:

答案 0 :(得分: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

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

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

检查此行

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

类似地为案例类型

提供正确的id

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

更改

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)“,移民);

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

为其他参数做类似的事情

答案 1 :(得分:0)

我还遇到了一个没有更新的GridView的问题。在GridView的RowUpdating事件中检查e.Newvalues Dictionary显示记录的旧值被发送到数据库UPDATE查询。 DataKeyNames不是我的问题;我把它设置正确。我的SELECT查询的WHERE子句引用了一个针对我的表单上的TextBox的控件参数。我无意中将此文本框的EnableViewState设置为&#34; False&#34;。因此,GridView在UPDATE发生之前重新绑定。将TextBox上的EnableViewState设置为&#34; True&#34;解决了这个问题。以下是查看UPDATE参数的代码:

    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