Suppose you have a gridview and you want to create multiple columns dynamically in asp.net c#. In this case first you have take a datatable and insert the name of header dynamically. After this procees assign the columns to gridview control. Now you can see the result. Please follow following example.
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
TemplateField xtemplt = new TemplateField();
BoundField xbound = new BoundField();
grdview2.AutoGenerateColumns = false;
Xbound.HeaderText = "PersonName";
Xbound.DataField = "PersonName";
grdview2.Columns.Add(bfield);
MySqlDataAdapter da;
xquery = "SELECT city_name FROM city ";
da = new MySqlDataAdapter(xquery, dbConn);
da.Fill(dt1);
for (int i = 0; i < dt1.Rows.Count; i++)
{
dt.Columns.Add("myrow", typeof(string));
dt.Columns[i + 1].ColumnName = dt1.Rows[i].ItemArray[0].ToString();
(OR)
dt.Columns["myrow"].ColumnName = "NewDelhi";
xtemplt = new TemplateField();
xtemplt.HeaderText = dt1.Rows[i].ItemArray[0].ToString();
grdview2.Columns.Add(xtemplt);
}
dt.Columns.Add("myrow", typeof(string));
dt.Columns["myrow"].ColumnName = "TOTAL";
bfield = new BoundField();
bfield.HeaderText = "TOTAL";
bfield.DataField = "TOTAL";
grdview2.Columns.Add(bfield);
grdview2.DataSource = dt;
grdview2.DataBind();
after calling this function RowdataBound method will call . When program will reach to gridview2 .databind() then rowdatabound will called. In this below code you can change the column behaviour like assigning the textbox for adding values, linkview button, dropdown, listbox etc.
protected void grdview2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 1; i < grdview2.Columns.Count ; i++)
{
TextBox txtcells = new TextBox();
txtcells.ID = "txtcells";
string xcolnam = grdview2.Columns[i].HeaderText.ToString();
txtcells.Text = (e.Row.DataItem as DataRowView).Row[xcolnam].ToString();
e.Row.Cells[i].Controls.Add(txtcells);
}
/* You can also change the particular column in Linkview Mode using Specified Index */
LinkButton lnkview = new LinkButton();
lnkview.ID = "lnkview";
lnkview.Click += new EventHandler(lnkview_Click);
lnkview.Text = (e.Row.DataItem as DataRowView).Row[0].ToString();
lnkview.CommandArgument = (e.Row.DataItem as DataRowView).Row[0].ToString();
e.Row.Cells[0].Controls.Add(lnkview);
}
}
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
TemplateField xtemplt = new TemplateField();
BoundField xbound = new BoundField();
grdview2.AutoGenerateColumns = false;
Xbound.HeaderText = "PersonName";
Xbound.DataField = "PersonName";
grdview2.Columns.Add(bfield);
MySqlDataAdapter da;
xquery = "SELECT city_name FROM city ";
da = new MySqlDataAdapter(xquery, dbConn);
da.Fill(dt1);
for (int i = 0; i < dt1.Rows.Count; i++)
{
dt.Columns.Add("myrow", typeof(string));
dt.Columns[i + 1].ColumnName = dt1.Rows[i].ItemArray[0].ToString();
(OR)
dt.Columns["myrow"].ColumnName = "NewDelhi";
xtemplt = new TemplateField();
xtemplt.HeaderText = dt1.Rows[i].ItemArray[0].ToString();
grdview2.Columns.Add(xtemplt);
}
dt.Columns.Add("myrow", typeof(string));
dt.Columns["myrow"].ColumnName = "TOTAL";
bfield = new BoundField();
bfield.HeaderText = "TOTAL";
bfield.DataField = "TOTAL";
grdview2.Columns.Add(bfield);
grdview2.DataSource = dt;
grdview2.DataBind();
after calling this function RowdataBound method will call . When program will reach to gridview2 .databind() then rowdatabound will called. In this below code you can change the column behaviour like assigning the textbox for adding values, linkview button, dropdown, listbox etc.
protected void grdview2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 1; i < grdview2.Columns.Count ; i++)
{
TextBox txtcells = new TextBox();
txtcells.ID = "txtcells";
string xcolnam = grdview2.Columns[i].HeaderText.ToString();
txtcells.Text = (e.Row.DataItem as DataRowView).Row[xcolnam].ToString();
e.Row.Cells[i].Controls.Add(txtcells);
}
/* You can also change the particular column in Linkview Mode using Specified Index */
LinkButton lnkview = new LinkButton();
lnkview.ID = "lnkview";
lnkview.Click += new EventHandler(lnkview_Click);
lnkview.Text = (e.Row.DataItem as DataRowView).Row[0].ToString();
lnkview.CommandArgument = (e.Row.DataItem as DataRowView).Row[0].ToString();
e.Row.Cells[0].Controls.Add(lnkview);
}
}