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

    How to show dropdownlist selected item name in grid only once

    hi,

    I want to select my dropdownlist and entered value in textbox

    that will be shown in grid view

    dropdownlist contains name and text box contains price.

    if same name i will be selected in dropdownlist that will be not shown in gridview and price will be added how to do it.

    my gridview like this. iwant output like this

    itemname price itemname price
    pen 10 pen 20(10+10)
    book 20 book 20
    bag 200 bag 200
    pen 10
  • #762271
    please find attached image

    Delete Attachment

  • #762280
    Hi dineshkumar,

    I think this might be helpful to you.


    DataTable dt = new DataTable();
    dt.Columns.Add("ID");
    dt.Columns.Add("Item");
    dt.Columns.Add("Qty");

    DataRow dr1 = dt.NewRow();
    dr1["ID"] = 1;
    dr1["Item"] = "Pen";
    dr1["Qty"] = 5;
    dt.Rows.Add(dr1);

    DataRow dr2 = dt.NewRow();
    dr2["ID"] = 2;
    dr2["Item"] = "Book";
    dr2["Qty"] = 20;
    dt.Rows.Add(dr2);

    DataRow dr3 = dt.NewRow();
    dr3["ID"] = 3;
    dr3["Item"] = "bag";
    dr3["Qty"] = 200;
    dt.Rows.Add(dr3);

    DataRow dr4 = dt.NewRow();
    dr4["ID"] = 4;
    dr4["Item"] = "Pen";
    dr4["Qty"] = 5;
    dt.Rows.Add(dr4);

    DataRow dr5 = dt.NewRow();
    dr5["ID"] = 5;
    dr5["Item"] = "bag";
    dr5["Qty"] = 200;
    dt.Rows.Add(dr5);

    DataTable dtRsult = dt.Clone();
    var distinctRows = dt.DefaultView.ToTable(true, "Item").Rows.OfType<DataRow>().Select(k => k["Item"] + "").ToArray();
    foreach (string name in distinctRows)
    {
    var rows = dt.Select("Item = '" + name + "'");
    int value = 0;
    int id = Convert.ToInt32(rows[0]["ID"]);
    foreach (DataRow row in rows)
    {
    value += Convert.ToInt32(row["Qty"]);
    }


    dtRsult.Rows.Add(id,name, value);
    value = 0;


    }


    Final result set is dtResult, which contains all the records, if there is any duplicate it's increment the existing result.

    Hope this will helpful to you...

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

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762281

    Hello Dinesh Kumar,

    Refer the below code :

    HTML Mark up :

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem>Pen</asp:ListItem>
    <asp:ListItem>Pencil</asp:ListItem>
    <asp:ListItem>Book</asp:ListItem>
    <asp:ListItem>Eraser</asp:ListItem>
    </asp:DropDownList>
    <asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
    <asp:Button ID="ButtonAdd" runat="server" onclick="ButtonAdd_Click"
    Text="Add" />
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" CellPadding="4"
    Font-Names="Segoe UI" ForeColor="#333333">
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>
    </div>
    </form>
    </body>
    </html>


    Code behind :

    using System.Data;

    DataTable dt;
    string item;
    int price;

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    dt = new DataTable();
    MakeDataTable();
    }
    else
    {
    dt = (DataTable)ViewState["DataTable"];
    }
    ViewState["DataTable"] = dt;
    }

    private void MakeDataTable()
    {
    dt.Columns.Add("Item");
    dt.Columns.Add("Price");
    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
    if (GridView1.Rows.Count == 0)
    {
    DataRow dr = dt.NewRow();
    dr["Item"] = DropDownList1.SelectedValue;
    dr["Price"] = txtPrice.Text;
    dt.Rows.Add(dr);

    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    else
    {
    int j = 0, index = 0;
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
    item = GridView1.Rows[i].Cells[1].Text;
    index = i;
    price = Convert.ToInt32(GridView1.Rows[i].Cells[2].Text);

    if (item == DropDownList1.SelectedValue)
    {
    j++;
    break;
    }
    }
    if (j == 0)
    {
    DataRow dr = dt.NewRow();
    dr["Item"] = DropDownList1.SelectedValue;
    dr["Price"] = txtPrice.Text;
    dt.Rows.Add(dr);

    dt = (DataTable)ViewState["DataTable"];
    ViewState["DataTable"] = dt;

    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    else
    {
    price = Convert.ToInt32(txtPrice.Text) + price;

    DataRow dr = dt.Rows[index];
    dr["Price"] = price.ToString();

    dt = (DataTable)ViewState["DataTable"];
    ViewState["DataTable"] = dt;

    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    }
    }

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

    Hope this will help you.


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

    Delete Attachment

  • #762298
    Thank you nirav lalan i need data to dropdownlist from database

    when selected item in dropdownlist that items price is placed in that text box.

    i have done but iam confusing how to use your code.

    i will send my code please see once.

  • #762299
    public static DataTable dt;
    SqlConnection con = new SqlConnection("");
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    SqlCommand cmd = new SqlCommand("SELECT * FROM tblinvoice", con);
    cmd.CommandType = CommandType.Text;
    cmd.Connection.Open();
    SqlDataReader ddlValues;
    ddlValues = cmd.ExecuteReader();

    DropDownList1.DataSource = ddlValues;
    DropDownList1.DataValueField = "ID";
    DropDownList1.DataTextField = "Item";
    DropDownList1.DataBind();
    DropDownList1.Items.Insert(0, "Select");
    Session["Item"] = DropDownList1.SelectedItem.ToString();
    cmd.Connection.Close();

    //SetInitialRow();
    }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    SqlCommand cmd = new SqlCommand("SELECT Price FROM tblinvoice where Item='" + DropDownList1.SelectedItem.Text + "'", con);
    cmd.CommandType = CommandType.Text;
    cmd.Connection.Open();
    SqlDataReader price;
    price = cmd.ExecuteReader();
    while (price.Read())
    {
    txtPrice.Text = price[0].ToString();
    txtPrice.Text = price[0].ToString();
    Session["Price"] = txtPrice.Text;
    }
    cmd.Connection.Close();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    if (Session["ItemName"] != DropDownList1.SelectedItem.Text)
    {
    if (ViewState["CurrentData"] != null)
    {
    dt = (DataTable)ViewState["CurrentData"];
    int count = dt.Rows.Count;
    BindGrid(count);

    }
    else
    {
    BindGrid(1);


    }
    }
    private void BindGrid(int rowcount)
    {

    dt = new DataTable();
    DataRow dr;
    dt.Columns.Add(new System.Data.DataColumn("ID", typeof(String)));
    dt.Columns.Add(new System.Data.DataColumn("Item", typeof(String)));
    dt.Columns.Add(new System.Data.DataColumn("Quantity", typeof(int)));

    if (ViewState["CurrentData"] != null)
    {
    for (int i = 0; i < rowcount + 1; i++)
    {
    dt = (DataTable)ViewState["CurrentData"];
    if (dt.Rows.Count > 0)
    {
    dr = dt.NewRow();
    dr[0] = dt.Rows[0][0].ToString();

    }
    }
    dr = dt.NewRow();
    dr[0] = rowcount + 1;
    dr[1] = DropDownList1.SelectedItem.ToString();
    dr[2] = Session["price"].ToString();
    dt.Rows.Add(dr);
    Session["ItemName"] = dr[1].ToString();
    }
    else
    {

    dr = dt.NewRow();
    dr[0] = 1;
    dr[1] = DropDownList1.SelectedItem.ToString(); ;
    dr[2] = Session["price"].ToString();
    //Request.Form["sub"] = Session["price"].ToString();
    //Request.Form["tax"] = Label1.Text;
    //Request.Form["total"] = Label1.Text;
    dt.Rows.Add(dr);
    Session["ItemName"] = dr[1].ToString();
    }

    // If ViewState has a data then use the value as the DataSource
    if (ViewState["CurrentData"] != null)
    {
    Grd.DataSource = (DataTable)ViewState["CurrentData"];
    Grd.DataBind();
    }
    else
    {
    // Bind GridView with the initial data assocaited in the DataTable
    Grd.DataSource = dt;
    Grd.DataBind();

    }
    // Store the DataTable in ViewState to retain the values
    ViewState["CurrentData"] = dt;


    }

  • #762300
    Hello Dinesh Kumar,

    Set AutoPostBack event = true of DropDownList

    Refer the updated code for Page Load Event and Dropdown Selected Index Change event :

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


    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Item.mdf;Integrated Security=True;User Instance=True");

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT ItemName FROM ItemDetails",con);
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
    DropDownList1.Items.Add(dr[0].ToString());
    }
    dr.Close();
    con.Close();
    dt = new DataTable();
    MakeDataTable();
    }
    else
    {
    dt = (DataTable)ViewState["DataTable"];
    }
    ViewState["DataTable"] = dt;
    }


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT Price FROM ItemDetails WHERE ItemName = '" + DropDownList1.SelectedValue + "'", con);
    SqlDataReader dr = cmd.ExecuteReader();
    if(dr.HasRows)
    {
    dr.Read();
    txtPrice.Text = dr[0].ToString();
    dr.Close();
    }
    con.Close();
    }


    Hope this is the exact answer you are looking for.

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


Sign In to post your comments