将gridview选定行的显示值显示在下拉列表中 [英] Display value of a gridview selected row to dropdownlist

查看:97
本文介绍了将gridview选定行的显示值显示在下拉列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在向下拉列表显示所选行值时遇到问题.有人可以帮我吗

So I have a problem in displaying the selected row value to dropdownlist. Can someone help me please

这是一个示例场景:

如您所见,在网格和视图"中选定的部门和费用"行值不会显示在部门和费用"下拉列表中.

As you can see, the selected row value of Department and Charge in the gridview does not display on the department and charge dropdownlist.

如何在下拉列表中显示所选行的值???每当我选择新行时,下拉列表的值都应更改.

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.

这是我的见解

<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>

代码隐藏

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;


        }
    }

推荐答案

在GridView1_RowCommand中,而不是

In GridView1_RowCommand, Instead of

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

添加

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

这篇关于将gridview选定行的显示值显示在下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆