0

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

here is a sample scenario:

enter image description here

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.

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;


        }
    }
Leo Derramas
  • 95
  • 1
  • 11

1 Answers1

0

In GridView1_RowCommand, Instead of

   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();
Ajoe
  • 1,397
  • 4
  • 19
  • 48