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

    How to avoid repeated items when bind data from dropdownlist to gridview when button click


    iam having dropdownlist ,text box,button when button clicks that dropdonlist selected item and textbox text should be added to gridview.

    dropdownlist containing items like coffee,tea,milk,biscuits
    textbox price that will be coming form database when dropdownlist select index changed

    when i added o gridview i dont want to show repeated items in gridview how to do it.
  • #762023
    please find attached image.

    Delete Attachment

  • #762025

    You can go through Below code

    <asp:DropDownList ID="drp1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drp1_SelectedIndexChanged">


    <asp:GridView ID="grd1" runat="server" AutoGenerateColumns="false">
    <asp:BoundField DataField="Prodid" HeaderText="Prodid" />
    <asp:BoundField DataField="ProductName" HeaderText="ProductName" />

    Server side code

    protected void drp1_SelectedIndexChanged(object sender, EventArgs e)
    adapter = new SqlDataAdapter("Select * from ProductList where Prodid="+ Convert.ToInt32(drp1.SelectedValue), con);
    txt1.Text = table.DefaultView[0]["Rate"].ToString();

    Fill Data

    public void FillData()
    adapter = new SqlDataAdapter("Select * from ProductList", con);
    drp1.DataSource = table;
    drp1.DataTextField = "ProductName";
    drp1.DataValueField = "Prodid";

    Button Click Bind Gridview

    DataTable dt = new DataTable();
    DataRow dr;
    dr = dt.NewRow();

    dr[0] = 1;
    dr[1] = drp1.SelectedItem.Text;
    dr[2] = Convert.ToDecimal(txt1.Text);
    grd1.DataSource = dt;

    Name : Dotnet Developer-2015
    Email Id :

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

  • #762027

    you are maintaining viewstate object to store the dummy datatable data and display the same into gridview right..?

    in this case while insert records into datatable you can check the below condition.

    DataRow[] drow=dt.Select("Item ="+ youritem +" And Quantity= "+yourquantity");

    if(drow!=null && drow.Length ==0)
    // insert record into DataTable and store it into Viewstate
    //don't insert data into datatable.

    Hope this will helpful to you...

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #762031
    Hello Dinesh Kumar,

    Refer the below code :

    HTML Markup :

    <html xmlns="" >
    <head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem>Colour Box</asp:ListItem>
    <asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
    <asp:Button ID="btnAdd" runat="server" onclick="Insert" Text="Add" />
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    CellPadding="4" Font-Names="Segoe UI" ForeColor="#333333" GridLines="None"
    EmptyDataText="No records added.">
    <RowStyle BackColor="#EFF3FB" />
    <asp:BoundField HeaderText="Item" DataField="Item" />
    <asp:BoundField HeaderText="Price" DataField="Price" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <AlternatingRowStyle BackColor="White" />

    Code - Behind :

    using System.Data;

    protected void Page_Load(object sender, EventArgs e)
    if (!this.IsPostBack)
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Item"), new DataColumn("Price") });
    ViewState["AllItems"] = dt;

    protected void BindGrid()
    GridView1.DataSource = (DataTable)ViewState["AllItems"];

    protected void Insert(object sender, EventArgs e)
    DataTable dt = (DataTable)ViewState["AllItems"];
    dt.Rows.Add(DropDownList1.SelectedValue, txtPrice.Text.Trim());
    ViewState["AllItems"] = dt;

    int j = 0;
    for (int i = 0; i < GridView1.Rows.Count; i++)
    string itemname = GridView1.Rows[i].Cells[0].Text;
    int itemprice = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text);
    if (itemname == DropDownList1.SelectedValue && itemprice == Convert.ToInt32(txtPrice.Text))
    if (j >= 2)
    ViewState["AllItems"] = dt;
    txtPrice.Text = string.Empty;

    See the attached image. It's an output of above code.

    Hope this will help you.

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

    Delete Attachment

  • Sign In to post your comments