Answered by:
Add Heading Title In Gridview Second Row

Question
-
User-807418713 posted
Hello
This is my code
if (e.Row.RowType == DataControlRowType.Header) { GridView HeaderGrid = (GridView)sender; GridViewRow HeaderGridRow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert); TableCell HeaderCell = new TableCell(); HeaderCell.HorizontalAlign = HorizontalAlign.Center; Label Second_Label = new Label(); Second_Label.Text = " Customer Despatch Report"; Second_Label.Font.Size = 16; Second_Label.ForeColor = System.Drawing.Color.Red; Label FromDate = new Label(); FromDate.Text = " From " + TextBox1.Text; FromDate.Font.Size = 12; FromDate.ForeColor = System.Drawing.Color.Blue; Label ToDate = new Label(); ToDate.Text = " To " + TextBox2.Text; ToDate.Font.Size = 12; ToDate.ForeColor = System.Drawing.Color.Blue; HeaderCell.Controls.Add(Second_Label); HeaderCell.Controls.Add(FromDate); HeaderCell.Controls.Add(ToDate); HeaderCell.ColumnSpan = 14; HeaderGridRow.Cells.Add(HeaderCell); HeaderGridRow.Cells.Add(HeaderCell); GridView1.Controls[0].Controls.AddAt(0, HeaderGridRow); }
I want to show this message in second row of gridview coz first row will be my data heading second row should be Title
How to do so
Tuesday, September 4, 2018 3:34 PM
Answers
-
User839733648 posted
Hi Gopi.MCA,
According to your description, I suggest that you could add the header in the function RowDataBound.
And you may use the function ((Table)GridView1.Controls[0]).Rows.AddAt(,) to add the header line to the position you want.
I've made a sample on my side, and for more details, you could refer to my code below.
.aspx
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" HeaderStyle-BackColor="#9AD6ED" HeaderStyle-ForeColor="#636363" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" > <Columns> <asp:BoundField DataField="ColumnName1" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="C1Descroption" HeaderText="Description" ItemStyle-Width="150" /> <asp:BoundField DataField="ColumnName2" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="C2Descroption" HeaderText="Description" ItemStyle-Width="150" /> <asp:BoundField DataField="ColumnName3" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="C3Descroption" HeaderText="Description" ItemStyle-Width="150" /> </Columns> </asp:GridView> </div> </form> </body> </html>
code behind.
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[6] { new DataColumn("ColumnName1"), new DataColumn("C1Descroption"), new DataColumn("ColumnName2"), new DataColumn("C2Descroption"), new DataColumn("ColumnName3"), new DataColumn("C3Descroption") }); dt.Rows.Add("AAAA", "AAA", "BBBB", "BBB","CCCC", "CCC"); dt.Rows.Add("DDDD", "DDD", "EEEE", "EEE", "FFFF", "FFF"); dt.Rows.Add("GGGG", "GGG", "HHHH", "HHH", "IIII", "III"); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { GridView gvw = (GridView)sender; GridViewRow HeaderRow = new GridViewRow(1, 1, DataControlRowType.Header, DataControlRowState.Insert); HeaderRow.HorizontalAlign = HorizontalAlign.Center; TableCell HeaderCell = new TableCell(); Label Second_Label = new Label(); Second_Label.Text = " Customer Despatch Report"; Second_Label.Font.Size = 16; Second_Label.ForeColor = System.Drawing.Color.Red; Label FromDate = new Label(); FromDate.Text = " From "; FromDate.Font.Size = 12; FromDate.ForeColor = System.Drawing.Color.Blue; Label ToDate = new Label(); ToDate.Text = " To "; ToDate.Font.Size = 12; ToDate.ForeColor = System.Drawing.Color.Blue; HeaderCell.ColumnSpan = 14; HeaderCell.Controls.Add(Second_Label); HeaderCell.Controls.Add(FromDate); HeaderCell.Controls.Add(ToDate); HeaderRow.Cells.Add(HeaderCell); HeaderRow.Cells.Add(HeaderCell); ((Table)GridView1.Controls[0]).Rows.AddAt(1, HeaderRow); } }
result:
Best Regards,
Jenifer- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 6, 2018 10:40 AM
All replies
-
User839733648 posted
Hi Gopi.MCA ,
According to your description and code, I’m sorry that I could not fully understand what the effect you want to.
first row will be my data heading second row should be TitleWhat do data heading and Title correspond to in your code.
I want to show this messageAnd what “this message” is.
If possible, please provide more details such as the page codes so that it will be better for us to help with you.
Best Regards,
JeniferWednesday, September 5, 2018 7:47 AM -
User-807418713 posted
Hello
Thanks For Your Reply
The Above Code If you apply To Gridview On RowCreated It Will Give You Top Row
Instead Of Top Row I want to show in second Row
how to do so..
Thanking You
Wednesday, September 5, 2018 5:46 PM -
User839733648 posted
Hi Gopi.MCA,
According to your description, I suggest that you could add the header in the function RowDataBound.
And you may use the function ((Table)GridView1.Controls[0]).Rows.AddAt(,) to add the header line to the position you want.
I've made a sample on my side, and for more details, you could refer to my code below.
.aspx
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" HeaderStyle-BackColor="#9AD6ED" HeaderStyle-ForeColor="#636363" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" > <Columns> <asp:BoundField DataField="ColumnName1" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="C1Descroption" HeaderText="Description" ItemStyle-Width="150" /> <asp:BoundField DataField="ColumnName2" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="C2Descroption" HeaderText="Description" ItemStyle-Width="150" /> <asp:BoundField DataField="ColumnName3" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="C3Descroption" HeaderText="Description" ItemStyle-Width="150" /> </Columns> </asp:GridView> </div> </form> </body> </html>
code behind.
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[6] { new DataColumn("ColumnName1"), new DataColumn("C1Descroption"), new DataColumn("ColumnName2"), new DataColumn("C2Descroption"), new DataColumn("ColumnName3"), new DataColumn("C3Descroption") }); dt.Rows.Add("AAAA", "AAA", "BBBB", "BBB","CCCC", "CCC"); dt.Rows.Add("DDDD", "DDD", "EEEE", "EEE", "FFFF", "FFF"); dt.Rows.Add("GGGG", "GGG", "HHHH", "HHH", "IIII", "III"); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { GridView gvw = (GridView)sender; GridViewRow HeaderRow = new GridViewRow(1, 1, DataControlRowType.Header, DataControlRowState.Insert); HeaderRow.HorizontalAlign = HorizontalAlign.Center; TableCell HeaderCell = new TableCell(); Label Second_Label = new Label(); Second_Label.Text = " Customer Despatch Report"; Second_Label.Font.Size = 16; Second_Label.ForeColor = System.Drawing.Color.Red; Label FromDate = new Label(); FromDate.Text = " From "; FromDate.Font.Size = 12; FromDate.ForeColor = System.Drawing.Color.Blue; Label ToDate = new Label(); ToDate.Text = " To "; ToDate.Font.Size = 12; ToDate.ForeColor = System.Drawing.Color.Blue; HeaderCell.ColumnSpan = 14; HeaderCell.Controls.Add(Second_Label); HeaderCell.Controls.Add(FromDate); HeaderCell.Controls.Add(ToDate); HeaderRow.Cells.Add(HeaderCell); HeaderRow.Cells.Add(HeaderCell); ((Table)GridView1.Controls[0]).Rows.AddAt(1, HeaderRow); } }
result:
Best Regards,
Jenifer- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 6, 2018 10:40 AM