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

    How to Datagrid view particular Datas Binding inCombobox

    I am creating windows application using c#.net 2010, here I am using grid view, inside my grid view I am set combo box in page load option, I am run the application binding all database data's are shown in combo box, but I want only particular data's only binding for combobox how to create.
    Ex:
    Product nu prices
    40 1000(combobox here binding (40 number values only) 1000, 900 how to bind )
    50 1500(combobox)
    40 900(combobox)

    Give me any one idea
  • #766265
    Hi,
    While adding each database value to your combobox just check whether product number already exist in combobox values.
    If exist then don't add that value, instead just concatenate the Text with current value.
    Sample code snippet:

    comboBox1.Items.Clear();
    SqlConnection con = new SqlConnection("Data Source=XYZSERVER;Initial Catalog=DB123;User ID=ABC;Password=PQR");
    con.Open();
    string sql = "SELECT product_number, product_price FROM PRODUCTTable";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Connection = con;
    SqlDataReader myReader = cmd.ExecuteReader();
    while (myReader.Read())
    {
    int iIndex = -1;
    iIndex = comboBox1.FindString(myReader["product_number"]);
    if (iIndex >= 0)
    {
    //... Edit Old Item Text
    comboBox1.Items[iIndex] = comboBox1.Items[iIndex] + " " + "90";
    }
    else
    {
    //... Add new Item
    comboBox1.Items.Add(myReader["product_number"] + " " + myReader["product_price"]);
    }
    }
    myReader.Close();
    con.Close();

  • #766267
    Hi
    try this code


    DataTable dtGrid = new DataTable();
    int id = Convert.ToInt32(gvDetails.DataKeys[0].Values[0]);
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    string strSelect = "SELECT FirstName,LastName,Location FROM Details where Id="+id ;
    SqlCommand cmd = new SqlCommand(strSelect, con);
    SqlDataAdapter dAdapter = new SqlDataAdapter(cmd);
    dAdapter.Fill(dtGrid);

    ComboBox CmbData = new ComboBox();
    CmbData.DataSource = dtGrid;
    CmbData.DisplayMember = "EmpId";
    CmbData.ValueMember = "EmpName";


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

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

  • #766269
    Hi
    You can use DataTable or DataSet to bind data to combobox.
    e.q.
    combboxObject.DataSource= Table/DataSet object
    combboxObject.DisplayMember= "Column Name which you want to display to user";
    combboxObject.ValueMember= "Column Name which you want to assign to respective item";

    /Hope it will work

    Thanks
    Umesh Bhosale

  • #766280
    if you want to bind only particular data, then do not directly bind data with 'DisplayMember' and 'ValueMember' properties, it just directly bind all data to combo
    better way is to fill data one by one
    see below snippet

    DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)myDataGrid.Rows[0].Cells[2];
    cbCell.Items.Add(.....);
    //OR
    DataGridViewComboBoxCell Cmb;
    cmb.item.add("Item1");
    cmb.item.add("Item2");
    GRidView1(CulumnIndex,RowIndex)=cmb

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766302
    Hi,

    As per my understand the post you need to bind 2 columns records into combobox right?

    If that is your requirement then on SQL Server side just concatenate those 2 columns and give a name for that column and bind that column while display it in combobox.

    Ex:

    select Column1+' - ' +Column2 as Column, Id from tablename


    and in combobox binding call like below,

    DataSet ds=//get data from database;
    cmb1.DataSource=ds;
    cmb1.DataTextField="Column";
    cmb1.DataValueField="Id";
    cmb1.DataBind();


    Try something like above 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/


  • Sign In to post your comments