You must Sign In to post a response.
  • Category: ASP.NET

    Display gridview selected row to dropdownlist using c#

    want to display gridview selected row value to dropdownlist.

    for that my code as follows
    protected void gvFacdet_SelectedIndexChanged(object sender, EventArgs e)
    {
    txt_name.Text = gvFacdet.SelectedRow.Cells[1].Text.ToString();
    ddldesg.Items.Add(gvFacdet.SelectedRow.Cells[2].Text.ToString());

    }

    In gridview row as follows
    Name Designation
    select Ramesh Courseofficer

    in run mode as follows
    Name textbox
    Designation dropdownlist

    When i select the gridview row value that gridview selected row value to be displayed in respective textbox and dropdownlist

    In textbox name to be displayed and dropdownlist designation to be displayed.

    please help me what is the problem in my above code.
  • #761489
    Hello Rao,

    Refer the below code :

    HTML Markup :

    <asp:GridView ID="gv" runat="server">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" OnClick = "LinkButton1_Click">Select</asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>


    Code behind :

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
    if (ddldesign.Items.Count > 0)
    {
    txtName.Text = string.Empty;
    for (int i = 0; i < ddldesign.Items.Count; i++)
    {
    ddldesign.Items.RemoveAt(i);
    }
    }

    GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
    int index = gvRow.RowIndex;

    txtName.Text = gv.Rows[index].Cells[0].Text;
    ddldesign.Items.Add(gv.Rows[index].Cells[1].Text);
    }


    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761492
    Hi
    You can try this code


    <asp:GridView ID="Grd1" runat="server" OnSelectedIndexChanged="Grd1_SelectedIndexChanged" AutoGenerateSelectButton="True">
    <Columns>
    <asp:BoundField DataField="Id" HeaderText="" />
    <asp:BoundField DataField="EmpNo" HeaderText="" />
    <asp:BoundField DataField="EmpName" HeaderText="" />
    <asp:BoundField DataField="Amount" HeaderText="" />

    <asp:TemplateField>
    <ItemTemplate>

    <asp:DropDownList ID="drp1" runat="server" AutoPostBack="true">
    <asp:ListItem Text="aa1" Value="11"></asp:ListItem>
    <asp:ListItem Text="aa2" Value="12"></asp:ListItem>
    <asp:ListItem Text="aa3" Value="13"></asp:ListItem>
    </asp:DropDownList>


    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>




    protected void Grd1_SelectedIndexChanged(object sender, EventArgs e)
    {
    GridViewRow grd1 = Grd1.SelectedRow;
    string ss = Grd1.SelectedRow.Cells[1].Text;
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments