[Solved] Duplicate item added to DropDownList on setting SelectedItem in ASP.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

when i selecting row from gridview and data getting loading into textbox and dropdownlist, when data get load in dropdown list value getting double to display.

DDLCat.selecteditem.text = GVcon.selectedRow.cells[6].Text;

here is screen shot.  (Goodwill) getting double display.

https://i.ibb.co/tY6crmj/drop.jpg

protected void GVCon_SelectedIndexChanged(object sender, EventArgs e)
{
    cno.Text = GVCon.SelectedRow.Cells[1].Text;
    txtcontainer.Text = GVCon.SelectedRow.Cells[2].Text;
    txtWeight.Text = GVCon.SelectedRow.Cells[3].Text;
    txtbaletotal.Text = GVCon.SelectedRow.Cells[5].Text;
    //string SesnId = dm.GetData("select Cat_ID from dbo.ConCatagory where Cat_Name='" + GVCon.SelectedRow.Cells[6].Text.Trim() + "'").Rows[0][0].ToString();
    //DDLCAT.SelectedValue = SesnId;
    DDLCAT.SelectedItem.Text = GVCon.SelectedRow.Cells[6].Text;   
}

 

Posted 4 months ago

Hi akhter,

Refer below sample.

You need to write like below code for set selected text in dropdownlist from gridview.

HTML

<asp:GridView runat="server" AutoGenerateColumns="false" ID="gvCustomers" OnSelectedIndexChanged="gvCustomers_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
        <asp:CommandField ShowSelectButton="true" />
    </Columns>
</asp:GridView>
<br />
<asp:DropDownList runat="server" ID="ddlName">
</asp:DropDownList>
<br />
<asp:DropDownList runat="server" ID="ddlCountry">
</asp:DropDownList>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = GetData();
        gvCustomers.DataSource = dt;
        gvCustomers.DataBind();

        ddlCountry.DataSource = dt;
        ddlCountry.DataValueField = "Country";
        ddlCountry.DataTextField = "Country";
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, new ListItem("Select", ""));

        ddlName.DataSource = dt;
        ddlName.DataValueField = "Name";
        ddlName.DataTextField = "Name";
        ddlName.DataBind();
        ddlName.Items.Insert(0, new ListItem("Select", ""));
    }
}

private static DataTable GetData()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM CustomerTest", con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
        }
    }
}

protected void gvCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
    int index = gvCustomers.SelectedIndex;
    string name = gvCustomers.SelectedRow.Cells[1].Text;
    string country = gvCustomers.SelectedRow.Cells[2].Text;

    if (ddlName.Items.FindByText(name) != null)
    {
        ddlName.ClearSelection();
        ddlName.Items.FindByText(name).Selected = true;
        if (ddlCountry.Items.FindByText(country) != null)
        {
            ddlCountry.ClearSelection();
            ddlCountry.Items.FindByText(country).Selected = true;
        }
        else
        {
            ddlCountry.SelectedIndex = 0;
        }
    }
    if (ddlCountry.Items.FindByText(country) != null)
    {
        ddlCountry.ClearSelection();
        ddlCountry.Items.FindByText(country).Selected = true;
        if (ddlName.Items.FindByText(name) != null)
        {
            ddlName.ClearSelection();
            ddlName.Items.FindByText(name).Selected = true;
        }
        else
        {
            ddlName.SelectedIndex = 0;
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load
        If Not Me.IsPostBack Then
            Dim dt As DataTable = GetData()
            gvCustomers.DataSource = dt
            gvCustomers.DataBind()
            ddlCountry.DataSource = dt
            ddlCountry.DataValueField = "Country"
            ddlCountry.DataTextField = "Country"
            ddlCountry.DataBind()
            ddlCountry.Items.Insert(0, New ListItem("Select", ""))
            ddlName.DataSource = dt
            ddlName.DataValueField = "Name"
            ddlName.DataTextField = "Name"
            ddlName.DataBind()
            ddlName.Items.Insert(0, New ListItem("Select", ""))
        End If
    End Sub

    Private Shared Function GetData() As DataTable
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(constr)
            Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM CustomerTest", con)
                Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable()
                    da.Fill(dt)
                    Return dt
                End Using
            End Using
        End Using
    End Function
    Protected Sub gvCustomers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim index As Integer = gvCustomers.SelectedIndex
        Dim name As String = gvCustomers.SelectedRow.Cells(1).Text
        Dim country As String = gvCustomers.SelectedRow.Cells(2).Text
        If ddlName.Items.FindByText(name) IsNot Nothing Then
            ddlName.ClearSelection()
            ddlName.Items.FindByText(name).Selected = True

            If ddlCountry.Items.FindByText(country) IsNot Nothing Then
                ddlCountry.ClearSelection()
                ddlCountry.Items.FindByText(country).Selected = True
            Else
                ddlCountry.SelectedIndex = 0
            End If
        End If
        If ddlCountry.Items.FindByText(country) IsNot Nothing Then
            ddlCountry.ClearSelection()
            ddlCountry.Items.FindByText(country).Selected = True
            If ddlName.Items.FindByText(name) IsNot Nothing Then
                ddlName.ClearSelection()
                ddlName.Items.FindByText(name).Selected = True
            Else
                ddlName.SelectedIndex = 0
            End If
        End If
    End Sub

Screenshot