.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Create header dynamically in gridview using ASP.NET

Posted By:Athira Sreejith       Posted Date: February 22, 2009    Points: 5    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Create header dynamically in gridview using ASP.NET if you are using this code means,while binding time,we have to set the property showheader="false" .

protected void GridViewExcel_RowCreated(object sender, GridViewRowEventArgs e)

{

GridView gv = (GridView)sender;

if (e.Row.RowType == DataControlRowType.Header)

{

if (gv.HasControls())

{

GridViewRow tr1 = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

TableHeaderCell tcCheckBox = new TableHeaderCell();

CheckBox chkCheckAll = new CheckBox();

chkCheckAll.ID = "chkCheckAll";

chkCheckAll.Attributes.Add("onclick", "return select_deselectAll(this.checked, this.id)");

chkCheckAll.ToolTip = "select/deselect all";

tcCheckBox.RowSpan = 2;

tcCheckBox.Controls.Add(chkCheckAll);

tcCheckBox.CssClass = "HeadwithBG";

tr1.Cells.Add(tcCheckBox);

TableHeaderCell tcSNo = new TableHeaderCell();

tcSNo.Text = "SNO";

tcSNo.RowSpan = 2;

tcSNo.CssClass = "HeadwithBG";

tr1.Cells.Add(tcSNo);

TableHeaderCell tcUSN = new TableHeaderCell();

tcUSN.Text = "USN";

tcUSN.CssClass = "HeadwithBG";

tcUSN.RowSpan = 2;

tr1.Cells.Add(tcUSN);

TableHeaderCell tcName = new TableHeaderCell();

tcName.Text = "Name";

tcName.Width = 800;

tcName.CssClass = "HeadwithBG";

tcName.RowSpan = 2;

tr1.Cells.Add(tcName);

TableHeaderCell tcSubCode = new TableHeaderCell();

tcSubCode.Text = "Sub code";

tcSubCode.CssClass = "HeadwithBG";

tr1.Cells.Add(tcSubCode);

ArrayList ArrSubcode = new ArrayList();

ArrayList ArrMaxDetail = new ArrayList();

if (Session["SubCode"] != null)

{

ArrSubcode = (ArrayList)Session["SubCode"];

}

if (Session["MaxDetail"] != null)

{

ArrMaxDetail = (ArrayList)Session["MaxDetail"];

}

for (int i = 0; i < ArrSubcode.Count; i++)

{

TableHeaderCell tcSub = new TableHeaderCell();

tcSub.Text = ArrSubcode[i].ToString();

tcSub.CssClass = "HeadwithBG";

string strTest = string.Empty;

strTest = ArrSubcode[i].ToString();

if (!string.IsNullOrEmpty(strTest))

{

// tcSub.ColumnSpan = 2;

tr1.Cells.Add(tcSub);

}

}

((Table)gv.Controls[0]).Rows.AddAt(0, tr1);

GridViewRow tr2 = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

string strType = string.Empty;

if (Session["FileType"] != null)

{

strType = Session["FileType"].ToString();

if (strType.Contains("Attendance"))

{

strType = "Max Attendance";

}

else if (strType.Contains("MarkSheet"))

{

strType = "Max Marks";

}

}

TableHeaderCell tcMaxMarks = new TableHeaderCell();

tcMaxMarks.Text = strType;

tcMaxMarks.CssClass = "HeadwithBG";

tr2.Cells.Add(tcMaxMarks);

for (int i = 0; i < ArrMaxDetail.Count; i++)

{

TableHeaderCell tcAtten = new TableHeaderCell();

tcAtten.Text = ArrMaxDetail[i].ToString();

tcAtten.CssClass = "HeadwithBG";

string strTest = string.Empty;

strTest = ArrMaxDetail[i].ToString();

if (!string.IsNullOrEmpty(strTest))

{

tr2.Cells.Add(tcAtten);

}

}

((Table)gv.Controls[0]).Rows.AddAt(1, tr2);

}

}

}

 



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend