This code sample explains how to create columns in a GridView control dynamically in ASP.NET. The code is written in C#. This function can be called whenever we need to create new columns in GridView.
public DataTable Getcallcoveragesummaryallheader() { string strDate; DataTable dtCallCoverage = new DataTable();
DataColumn dCol = new DataColumn("Employee Name", typeof(System.String)); dtCallCoverage.Columns.Add(dCol);
dCol = new DataColumn("Call Details", typeof(System.String)); dtCallCoverage.Columns.Add(dCol);
if (drpYear.SelectedValue != "") { for (int ColIndex = 1; ColIndex <= 12; ColIndex++) { string cYear = drpYear.SelectedValue; strDate = Convert.ToString(ColIndex) + "/" + 1 + "/" + cYear; DateTime chkDate = Convert.ToDateTime(strDate); string CMonthName = chkDate.ToString("MMM"); dCol = new DataColumn(CMonthName); dtCallCoverage.Columns.Add(dCol); } } GridDisplay.Visible = true; string sPrevname = string.Empty; string mstrCall = "Technical,Technical Avg,Commercial,Commercial Avg,Total"; string[] strCall = mstrCall.Split(','); foreach (string strCallHeader in strCall) { DataRow drow = dtCallCoverage.NewRow(); drow["Employee Name"] = thomas; } drow["Call Details"] = strCallHeader; sPrevname = "thomas; dtCallCoverage.Rows.Add(drow); } DataRow dRow1 = dtCallCoverage.NewRow(); dRow1["Call Details"] = "Average Per Day"; dtCallCoverage.Rows.Add(dRow1);
return dtCallCoverage; }
The code sample below is an example on how to call the above function.
DataTable dtReport = Getcallcoveragesummaryallheader(); gridview.DataSource = dtReport; gridview.DataBind();
|
No responses found. Be the first to respond and make money from revenue sharing program.
|