在编辑GridView时,下拉列表值不存储在变量中

时间:2013-08-02 09:25:33

标签: c# asp.net .net gridview

dropdownlist(编辑gridview)中所选项的值返回NULL。它应该返回选定的值..

aspx.cs文件中的代码::

    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    DropDownList ct = (DropDownList)row.FindControl("DropDownList6");
    string ctype = ct.SelectedItem.Text;
    string sql1 = ("Select Case_Type_Value FROM Case_Type where Case_Type_Text ='" + ctype + "' ");


    DropDownList cs = (DropDownList)row.FindControl("DropDownList3");
    string cstatus = cs.SelectedItem.Text;
    string sql2 = ("Select Case_Status_Value FROM Case_Status where Case_Status_Text ='" + cstatus + "' ");


    SqlConnection con= new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    SqlCommand cmd1 = new SqlCommand(sql1, con);
    SqlCommand cmd2 = new SqlCommand(sql2, con);

    con.Open();
    int v1 = Convert.ToInt32(cmd1.ExecuteScalar());
    int v2 = Convert.ToInt32(cmd2.ExecuteScalar());
    con.Close(); 

    SqlCommand cmd = new SqlCommand("UPDATE Table SET Case_Type = @Case_Type, Case_Status = @Case_Status, con);
    cmd.Parameters.AddWithValue("@Case_Type", v1);
    cmd.Parameters.AddWithValue("@Case_Status", v2);

以下是该页面的aspx代码:

   <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:TemplateField HeaderText="Case Number" SortExpression="case_number">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("case_number") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label7" runat="server" Text='<%# Bind("case_number") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Case Name" SortExpression="case_name">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("case_name") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label8" runat="server" Text='<%# Bind("case_name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Case Type" SortExpression="Case_Type_Text">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList6" runat="server" 
                                DataSourceID="SqlDataSource2" DataTextField="Case_Type_Text" 
                                DataValueField="Case_Type_Text" SelectedValue='<%# Bind("Case_Type_Text") %>'>
                                <asp:ListItem>--Select--</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label9" runat="server" Text='<%# Bind("Case_Type_Text") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                   <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:TemplateField HeaderText="Assigned Date" SortExpression="assigned_date">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox5" runat="server" 
                                Text='<%# Bind("assigned_date", "{0:d}") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label10" runat="server" 
                                Text='<%# Bind("assigned_date", "{0:d}") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Assigned To" SortExpression="assigned_to">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList7" runat="server" 
                                DataSourceID="SqlDataSource4" DataTextField="User_Name" 
                                DataValueField="User_Name" SelectedValue='<%# Bind("assigned_to") %>'>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label11" runat="server" Text='<%# Bind("assigned_to") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>


                    <asp:TemplateField HeaderText="Date Withdrawn" SortExpression="date_withdrawn">
                        <EditItemTemplate>
                            <cc1:DatePicker ID="DatePicker5" runat="server" DateFormat="yyyy-MM-dd" 
                                CalendarDate='<%# Bind("date_withdrawn") %>' TextCssClass="" />
                        </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" 
                                CalendarDate='<%# Bind("date_delivered") %>' TextCssClass="" 
                                />
                        </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" 
                                CalendarDate='<%# Bind("qc_date") %>' TextCssClass="" 
                                />
                        </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>

2 个答案:

答案 0 :(得分:0)

尝试喜欢这个,

GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
DropDownList ct = (DropDownList)row.Cells[2].FindControl("DropDownList6");
string ctype = ct.SelectedValue;

DropDownList cs = (DropDownList)row.Cell[3].FindControl("DropDownList3");
string cstatus = cs.SelectedValue;

答案 1 :(得分:0)

GridViewRow row = grdVw.Rows[e.RowIndex];
String str = Convert.ToString(((DropDownList)(row.Cells[2].Controls[0])).SelectedItem);

获取您的单元格编号,单元格的编号从零开始,因此代码中的DropDownList6将为2。

请检查并验证它是否有效!