简体   繁体   English

将 gridview 选定行的值显示到下拉列表

[英]Display value of a gridview selected row to dropdownlist

So I have a problem in displaying the selected row value to dropdownlist.所以我在将所选行值显示到下拉列表时遇到问题。 Can someone help me please有人能帮助我吗

here is a sample scenario:这是一个示例场景:

在此处输入图像描述

As you can see, the selected row value of Department and Charge in the gridview does not display on the department and charge dropdownlist.如您所见,在 gridview 中选择的 Department and Charge 行值不会显示在 Department and Charge 下拉列表中。

How can I display the value of the selected row inside the dropdown???如何在下拉列表中显示所选行的值??? the value of dropdown should change whenever a I select a new row.每当我 select 一个新行时,下拉列表的值应该改变。

This is my griview这是我的看法

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="CASE_KEY" DataSourceID="SqlDataSource1" Height="250px" 
    Width="1109px" BackColor="White" BorderColor="#999999" BorderStyle="None" 
    BorderWidth="1px" CellPadding="3" GridLines="Vertical" onrowcommand="GridView1_RowCommand"
    OnRowDataBound="GridView1_RowDataBound">
    <AlternatingRowStyle BackColor="Gainsboro" />
    <Columns>
        <asp:buttonfield buttontype="Link" commandname="Select" text="Select" Visible="False" />
        <asp:BoundField DataField="CASE_KEY" HeaderText="CASE_KEY" ReadOnly="True" 
            SortExpression="CASE_KEY" Visible="true" />
        <asp:BoundField DataField="DEPARTMENT_CASE_NUMBER" 
            HeaderText="Department Case #" SortExpression="DEPARTMENT_CASE_NUMBER" />
        <asp:BoundField DataField="DEPARTMENT_NAME" HeaderText="Department" 
            SortExpression="DEPARTMENT_NAME" />
        <asp:BoundField DataField="CHARGE" HeaderText="Charge" 
            SortExpression="CHARGE" />
        <asp:BoundField DataField="LAB_CASE" HeaderText="Lab Case #" 
            SortExpression="LAB_CASE" />
        <asp:BoundField DataField="OFFENSE_DATE" HeaderText="Incident Report Date" 
            SortExpression="OFFENSE_DATE" />
    </Columns>
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>

This is my textbox/dropdown这是我的文本框/下拉列表

<table class="style2" >
    <tr>
        <td class="style3" >Department Case #</td>
        <td> <asp:TextBox ID="TextBox1" runat="server" Enabled="False" ontextchanged="btnCancel_Click"></asp:TextBox></td>
    </tr>

    <tr>
         <td class="style3">Department</td>
         <td> 
             <asp:DropDownList ID="DropDownList1" runat="server" 
                  Height="18px" Width="153px" Enabled="False" AppendDataBoundItems="true"  
                 AutoPostBack="true" DataSourceID="SqlDataSource2" 
                 DataTextField="DEPARTMENT_NAME" DataValueField="DEPARTMENT_CODE" 
                 onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
                <asp:ListItem Text="--- Select ----" Value=" " />
             </asp:DropDownList>
             <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:****ConnectionString %>" 
                 SelectCommand="SELECT [DEPARTMENT_CODE], [DEPARTMENT_NAME] FROM [TV_DEPTNAME]">
             </asp:SqlDataSource>
         </td>
    </tr>

    <tr> 
         <td class="style3">Charge</td>
         <td>
             <asp:DropDownList ID="DropDownList2" runat="server" 
                 Height="22px" Width="153px" Enabled="False" AppendDataBoundItems="true" 
                 AutoPostBack="true" DataSourceID="SqlDataSource3" 
                 DataTextField="OFFENSE_DESCRIPTION" DataValueField="OFFENSE_CODE" 
                 onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
                 <asp:ListItem Text="--- Select ----" Value=" " />
             </asp:DropDownList>
             <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:*****ConnectionString %>" 
                 SelectCommand="SELECT [OFFENSE_CODE], [OFFENSE_DESCRIPTION] FROM [TV_OFFENSE]">
             </asp:SqlDataSource>
         </td>
    </tr>

    <tr>
        <td class="style3">Lab Case #</td>
        <td><asp:TextBox ID="TextBox4" runat="server" Enabled="False"  ontextchanged="btnCancel_Click"></asp:TextBox></td>
   </tr>

   <tr>
       <td class="style3">Incident Report Date</td>
       <td><asp:TextBox ID="TextBox5" runat="server" Enabled="False" ontextchanged="btnCancel_Click"></asp:TextBox></td>
   </tr>

</table>

CODE BEHIND代码背后

protected void Page_Load(object sender, EventArgs e)
    {
        string connetionString;
        SqlConnection cnn;

        connetionString = @"Data Source=A**S****D****\MSSQL****;Initial Catalog=*****;User ID=****;Password=****";

        cnn = new SqlConnection(connetionString);

        cnn.Open();

        DropDownList1.DataBind();
        DropDownList2.DataBind();
        GridView1.DataBind();   



    }


    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {    ///<summary> Change the mouse cursor to Hand symbol to show the user the cell is selectable</summary>
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer'";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            ///<summary> Attach the click event to each cells</summary>
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
        }
    }

    protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "Select")
        {
            ///<summary>
            ///Convert the row index stored in the CommandArgument
            ///property to an Integer.
            ///</summary>
            int index = Convert.ToInt32(e.CommandArgument);

            ///<summary>
            /// Retrieve the row that contains the button clicked 
            /// by the user from the Rows collection.
            ///</summary>
            GridViewRow row = GridView1.Rows[index];

            ///<summary> Populate the input box with the value of selected row.</summary>
            GridViewRow gr = GridView1.Rows[index];
            TextBox1.Text = gr.Cells[2].Text;
            DropDownList1.Items.Add(gr.Cells[3].Text.ToString());
            DropDownList2.Items.Add(gr.Cells[4].Text.ToString());
            TextBox4.Text = gr.Cells[5].Text;
            TextBox5.Text = gr.Cells[6].Text;
            TextBox6.Text = gr.Cells[1].Text;


        }
    }

In GridView1_RowCommand, Instead of在 GridView1_RowCommand 中,而不是

   DropDownList1.Items.Add(gr.Cells[3].Text.ToString());
   DropDownList2.Items.Add(gr.Cells[4].Text.ToString());

Add添加

   DropDownList1.SelectedValue=gr.Cells[3].Text.ToString();
   DropDownList2.SelectedValue=gr.Cells[4].Text.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM