如何使GridView DropDown列表选择行并执行OnSelectedIndexchange方法?

时间:2018-11-21 13:24:24

标签: c# asp.net gridview dropdown

我希望我提出的问题对我要解决的问题是正确的。我有一个GridView,它显示来自SQL源的数据。我需要将其中一列作为下拉列表。我也在寻找一种方法,当我打开下拉菜单时,它选择该行,然后执行添加的OnSelectedIndexChange事件。

我正在提取相应所选行的单元格数据,并将该数据提交到SQL表。有没有一种方法可以不选择该行,而将下拉列表的更改用作该行的标识符?我之所以这么问,是因为在选择该行时还有一个额外的回发。我正在寻找下拉菜单的能力,并在选择新值时执行更改事件(它将行数据提交到数据库。)

如果必须选择,则必须选择。但是我当前遇到的最大问题是选择的下拉列表值没有传递给C#。这是Gridview和C#,显示了我在做什么。我有很多评论,因为我看不到为什么它不会传递值。

<div class="CurrentMonthTab" style ="height:350px; width:90%; margin-left:15px;  overflow-y: scroll; overflow-x: hidden">        
    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" 
                  Width="100%" Font-Size = "11pt" 
                  CellPadding="5" ClientIDMode="Static" ShowHeader="False" 
                  HeaderStyle-BackColor="YellowGreen" DataSourceID="SqlDataSource2">
            <Columns>
                <asp:CommandField ShowSelectButton="True" />

                <asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" ItemStyle-Width="16%" />
                <asp:BoundField DataField="UniqClient" HeaderText="UniqClient" SortExpression="UniqClient" ItemStyle-CssClass="hiddencol"  HeaderStyle-CssClass="hiddencol" />
                <asp:BoundField DataField="Expiring_Policies" HeaderText="Expiring_Policies" ReadOnly="True" SortExpression="Expiring_Policies" ItemStyle-Width="18%" ItemStyle-Wrap="True" />
                <asp:BoundField DataField="Premiums" HeaderText="Premiums" ReadOnly="True" SortExpression="Premiums" ItemStyle-Width="8%" ItemStyle-CssClass="ExpDateCSS" />
                <asp:BoundField DataField="TotalPremium" HeaderText="TotalPremium" ReadOnly="True" SortExpression="TotalPremium" ItemStyle-Width="8%" />
                <asp:BoundField DataField="Lines" HeaderText="Lines" SortExpression="Lines" ItemStyle-Width="20%" ReadOnly="True" />
                <asp:BoundField DataField="ExpDate" HeaderText="ExpDate" ReadOnly="True" SortExpression="ExpDate" ItemStyle-Width="10%" />                      
                <asp:BoundField DataField="Company" HeaderText="Company" SortExpression="Company" ItemStyle-Width="6%" ItemStyle-CssClass="ExpDateCSS" ReadOnly="True" />
                <asp:BoundField DataField="Broker_Name" HeaderText="Broker_Name" SortExpression="Broker_Name" ItemStyle-CssClass="hiddencol"  HeaderStyle-CssClass="hiddencol" />
                <asp:TemplateField HeaderText="NameOf" SortExpression="NameOf">
                    <ItemTemplate>  
                        <asp:DropDownList ID="ddlUnderWriter" runat="server"  AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_OnSelectedIndexChanged" SelectedValue='<%# Bind("NameOf") %>' DataSourceID="SqlDataSource3" DataTextField="NameOf" DataValueField="NameOf">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="UniqBroker" HeaderText="UniqBroker" ReadOnly="True" SortExpression="UniqBroker" ItemStyle-CssClass="hiddencol"  HeaderStyle-CssClass="hiddencol" />
            </Columns>


        <HeaderStyle BackColor="YellowGreen"></HeaderStyle>
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="Red" />

    </asp:GridView>

这是GridView,下拉菜单中有一个模板字段。

 protected void DropDownList1_OnSelectedIndexChanged(object sender, EventArgs e )
    {

        //TableCell client = GridView1.Rows[c.RowIndex].Cells[1];
       // string Client = GridView1.SelectedRow.Cells[1].Text;//Client Name
        //int UniqCNT = Int32.Parse(GridView1.SelectedRow.Cells[2].Text); //UniqClient
        //string ExpPolicyNums = GridView1.SelectedRow.Cells[3].Text;
       //int Ub = Int32.Parse(GridView1.SelectedRow.Cells[11].Text);//UniqBroker
        //string ExperationDate = GridView1.SelectedRow.Cells[7].Text; //ExpDate
        string NewUw = GridView1.SelectedRow.Cells[10].Text; //This inserts the Under Writer
       /* string Company = GridView1.SelectedRow.Cells[8].Text; //Company issuer
        string Broker = GridView1.SelectedRow.Cells[9].Text;  //Broker_Name
        string Premium = GridView1.SelectedRow.Cells[4].Text; //Premiums
        string TotalPremium = GridView1.SelectedRow.Cells[5].Text; //Total premiums
        string Reviewed = "No"; //Updates the DB and shows that it hasn't been reviewed by the Message Creator
                                //DateCreated gets inserted when record is created */
       // string NewUw = ddlUnderWriter1.SelectedValue;


        DateTime dateUpDated = DateTime.Now; //Inserts a dateUpdated record
        if (String.IsNullOrEmpty(NewUw))
        {
            string empty = "empty fin box";
            MessageBox.Show(empty);
        }
        else
        {
            MessageBox.Show(NewUw);
        }

        // int UpR = Int32.Parse(vRenewalReport.SelectedRow.Cells[9].Text); //UniqProducer. This will always submit NULL due to not 
        // intially receving the UniqProducer data in the GridView

        /*
        string query = "INSERT INTO [GTU_Apps].[dbo].[Reviewed_Renewal_Policy] (UniqClient, Client, [Expiring_Policies], Premiums, TotalPremium, UniqBroker, ExpDate, NewUw, Company, Broker_Name,  Reviewed, DateUpdated) " +
            "VALUES (@UniqCNT, @Client, @ExpPolicyNums, @Premium, @TotalPremium, @Ub, @ExperationDate, @NewUw, @Company, @Broker,  @Reviewed, @dateUpDated)";

        using (SqlConnection conn = new SqlConnection("Data Source=GTU-BDE01;Initial Catalog=GTU_Apps;Integrated Security=True"))
        {
            using (SqlCommand comm = new SqlCommand(query, conn))
            {
                comm.Parameters.AddWithValue("@UniqCNT", UniqCNT);
                comm.Parameters.AddWithValue("@Client", Client);
                comm.Parameters.AddWithValue("@ExpPolicyNums", ExpPolicyNums);
                comm.Parameters.AddWithValue("@Premium", Premium);
                comm.Parameters.AddWithValue("@TotalPremium", TotalPremium );
                comm.Parameters.AddWithValue("@Ub", Ub);
                comm.Parameters.AddWithValue("@ExperationDate", ExperationDate);
                comm.Parameters.AddWithValue("@NewUw", NewUw);
                comm.Parameters.AddWithValue("@Company", Company);
                comm.Parameters.AddWithValue("@Broker", Broker);

                comm.Parameters.AddWithValue("@Reviewed", Reviewed);
                comm.Parameters.AddWithValue("@dateUpDated", dateUpDated);
                conn.Open();
                comm.ExecuteNonQuery();
                conn.Close();
            }
        }*/
        End(sender, e);
    }

这是背后的代码。其中大部分内容已被注释掉,因为我需要隔离下拉列表并弄清楚为什么它没有通过值。您可以看到我正在检查字符串是否为null或为空。然后使用消息框快速显示它以进行测试。即使我故意选择该行,然后更改下拉列表,它也会告诉我它是空的。任何帮助将不胜感激。

0 个答案:

没有答案