Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Grid in my windows application project .. In Grid , i have DROPDOWN in one column , in that column how to display data fetched from SQLSERVER .
Please see below .. please edit below program and make me to solve problem .. THANKS IN ADVANCE..

connstring();
conn.Open();
adap = new SqlDataAdapter("gvretrieve_ip", conn);


ds = new System.Data.DataSet();
adap.Fill(ds, "dt");

foreach (DataColumn DC in ds.Tables[0].Columns)
{

}

dataGridView2.DataSource = ds.Tables[0];


int index = 5;

DataGridViewComboBoxColumn newCol = new DataGridViewComboBoxColumn();
dataGridView2.Columns.Insert(index, newCol);
dataGridView2.Columns[index].HeaderText = dataGridView2.Columns[index + 1].HeaderText;
newCol.MaxDropDownItems = 4;



dataGridView2.Columns.RemoveAt(index + 1);
Posted
Updated 7-Dec-14 21:32pm
v3

Try this code

string strcon = @"Data Source=kp;Initial Catalog=Name;Integrated Security=True;Pooling=False";
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataTable dt;
        DataGridViewComboBoxColumn dgvCmb;
        public Form2()
        {
            InitializeComponent();
            grdcmd();
        }
        public void grdcmd()
        {
            con = new SqlConnection(strcon);
            con.Open();
            string qry = "Select * from Dbname";
            da = new SqlDataAdapter(qry, strcon);
            dt = new DataTable();
            da.Fill(dt);
            dgvCmb = new DataGridViewComboBoxColumn();
            foreach (DataRow row in dt.Rows)
            {
                dgvCmb.Items.Add(row["Fname"].ToString());
            }
            dataGridView1.Columns.Add(dgvCmb);
        }
 
Share this answer
 
Comments
Bns Vigneshwaran 8-Dec-14 3:24am    
Thank you very much .. BUT please see my coding below , i want to edit in this to get that answer .. Please help me genius

connstring();
conn.Open();
adap = new SqlDataAdapter("gvretrieve_ip", conn);

ds = new System.Data.DataSet();
adap.Fill(ds, "dt");

foreach (DataColumn DC in ds.Tables[0].Columns)
{

}

dataGridView2.DataSource = ds.Tables[0];


int index = 5;

DataGridViewComboBoxColumn newCol = new DataGridViewComboBoxColumn();
dataGridView2.Columns.Insert(index, newCol);
dataGridView2.Columns[index].HeaderText = dataGridView2.Columns[index + 1].HeaderText;
newCol.MaxDropDownItems = 4;



dataGridView2.Columns.RemoveAt(index + 1);
Check my Article hope this will help you.
you need to create a Cobobox template column and you can set the combobox datasource as your sql query.

C#
 dgvcombo.DataSource = dtsource;
 dgvcombo.DisplayMember = DisplayMember;
dgvcombo.ValueMember = ValueMember;


DataGridView Helper Class[^]
 
Share this answer
 
Comments
Bns Vigneshwaran 8-Dec-14 22:41pm    
Hi syed ..Hope you doing good.. I saw ur article .. But i have one doubt .. you are using window controls in your demo or telerik controls ?
syed shanu 8-Dec-14 22:52pm    
Hi,
if you have read my article you wont ask this question as you can see DatagridView helper class which means this is not telerik controls this is .net winform datagridView Grid Control.
Bns Vigneshwaran 9-Dec-14 2:19am    
Sorry, Am New to .net . please answer

In your article , you are just designing , you are not fetching values from sql server to combox , right ?
syed shanu 9-Dec-14 2:22am    
its demo program i pass the datatable to bind the result to combo box same like that you can pass your datatable to bind to combobox.Your datatable will be as your database result.
// ComboBox Column creation
ShanuDGVHelper.Templatecolumn(shanuDGV, ShanuControlTypes.ComboBox, "DGVComboColumn", "ComboCol", "Combo Column", true, 160, DataGridViewTriState.True, DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.MiddleRight, Color.Transparent, dtName, "Name", "Value", Color.Black);
here you can see dtName - is Datatable
"Name", "Value" are combobox displaymember and value member

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