Below code will explain the how to merge two column in the grid view.
Design Code
<asp:GridView ID="dvMergingHeader" runat="server" OnRowCreated="dvMergingHeader_ItemCreated" align="center" AutoGenerateColumns="false" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%"> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> <RowStyle CssClass="ctext" BackColor="LightCyan" Font-Bold="False" VerticalAlign="Top" /> <HeaderStyle BackColor="gray" Font-Bold="True" CssClass="text" ForeColor="white" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" VerticalAlign="Middle" /> <asp:BoundField DataField=" Column1" HeaderText="Column1"> <ItemStyle HorizontalAlign="Left" VerticalAlign="top" /> </asp:BoundField> <asp:BoundField DataField=" Column2" HeaderText="Column2"> <ItemStyle HorizontalAlign="Left" VerticalAlign="top" /> </asp:BoundField> <asp:BoundField DataField=" Column3" HeaderText="Column3"> <ItemStyle HorizontalAlign="Left" VerticalAlign="top" /> </asp:BoundField> <asp:BoundField DataField=" Column4" HeaderText="Column4"> <ItemStyle HorizontalAlign="Left" VerticalAlign="top" /> </asp:BoundField> </asp:GridView>
Code Behind Code
protected void dvMergingHeader_ItemCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { //custom header. GridView gvHeader = (GridView)sender; GridViewRow gvHeaderRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert); TableCell tc1 = new TableCell();
// First Merged column tc1.Text = "Merged Column1"; tc1.ColumnSpan = 2; tc1.BackColor = System.Drawing.Color.Brown; gvHeaderRow.Cells.Add(tc1l);
// Second Merged column tc1 = new TableCell(); tc1.Text = "Merged Column2"; tc1.ColumnSpan = 2; gvHeaderRow.Cells.Add(tc1);
gvHeader.Controls[0].Controls.AddAt(0, gvHeaderRow); } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|