Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void Button1_Click(object sender, EventArgs e)
{



DataTable dt = new DataTable();
dt.Rows.Add();
dt.Columns.Add(new DataColumn(TextBox1.Text));
GridView1.DataSource = dt;
GridView1.DataBind();

}


Its only showing one column but i want to generate column of every text box value.it should automatically create column by name of text box value.


can somebody help,i am new in .net
Posted
Updated 5-Feb-14 3:17am
v2
Comments
G-code101 5-Feb-14 11:34am    
Ok say you have 5 textboxes and you want 5 columns then do this

protected void Button1_Click(object sender, EventArgs e)
{

DataTable dt = new DataTable();
String col1 = textBox1.Text;
String col2 = textBox2.Text;
String col3 = textBox3.Text;
String col4 = textBox4.Text;
String col5 = textBox5.Text;

dt.Rows.Add();
dt.Columns.Add(new DataColumn(col1));
dt.Columns.Add(new DataColumn(col2));
dt.Columns.Add(new DataColumn(col3));
dt.Columns.Add(new DataColumn(col4));
dt.Columns.Add(new DataColumn(col5));

dataGridView1.DataSource = dt;

// Do bindings after columns have been added
}

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