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?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
CrishPosted Apr 13, 2011, 7:56 AM
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();
}