Datagridview
how to insert new column to datagridview from textbox and placed after the selected column name from combo box in winforms using C#.net?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
CrishRank #1,487 · 1.1K reputationPosted Apr 13, 2011, 3:56 AM EDT
SqlConnection sqlcon = new SqlConnection(""); SqlCommand sqlcmd = new SqlCommand(); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { sqlcon.Open(); sqlcmd = new SqlCommand("select * from emp", sqlcon); da = new SqlDataAdapter(sqlcmd); dt.Clear(); da.Fill(dt); if (dt.Rows.Count > 0) { string s; s=comboBox1.Text; dt.Columns.Add(comboBox1.Text); DataRow dr; dr = dt.NewRow(); dr[s] = textBox1.Text; dt.Rows.Add(dr); dataGridView1.DataSource = dt; } sqlcon.Close(); }