Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
con = new SqlConnection(cs);
              con.Open();

              string ct = "select distinct RTRIM(faheads) from FA_Details ";

              cmd = new SqlCommand(ct);
              cmd.Connection = con;

              rdr = cmd.ExecuteReader();

              while (rdr.Read())
              {
                  // dataGridView1.Rows[0].Cells[2].Value.Add(rdr[0]);
                  DataGridViewComboBoxCell c =new DataGridViewComboBoxCell();
                  c.Items.Add(rdr[0]);
                  dataGridView1.Rows[0].Cells[2] = c;

              }
              con.Close();




but it showing only one value from database.
Posted
Comments
[no name] 30-Jun-15 11:56am    
Seeing that we do not have access to your database or your data, what is it exactly that you would expect us to be able to tell you based on this? Fix your query so that it returns the data you want.

For each record returned from the database, you're creating a new DataGridViewComboBoxCell, adding one item to it, and replacing the third cell of the first row with the new single-record combobox.

You need to move the cell creation and assignment outside of the while loop.

C#
const string query = "select distinct RTRIM(faheads) from FA_Details";

var c = new DataGridViewComboBoxCell();
dataGridView1.Rows[0].Cells[2] = c;

using (var con = new SqlConnection(cs))
using (var cmd = new SqlCommand(query, con))
{
    con.Open();
    
    using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
    {
        while (rdr.Read())
        {
            c.Items.Add(rdr[0]);
        }
    }
}
 
Share this answer
 
Comments
Member 10899600 1-Jul-15 0:29am    
Yeah it worked.....
Thanks...
Hello ,

suppose you have one datagridview and there one comboboxcolumn named as
cmbproduct and if you want to bind the combobox value from dataset then
<br />
<br />
 cmbproduct.DataSource = dsproduct.Tables[0].Columns[0].Table.DefaultView;<br />
 cmbproduct.ValueMember = "PId"; <br />
 cmbproduct.DisplayMember = "Pname";<br />

put this code where you want to load the combobox .

thanks
Animesh
 
Share this answer
 
Comments
Richard Deeming 1-Jul-15 7:14am    
You can simplify that data source to:
dsproduct.Tables[0].DefaultView

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900