locked
Common Heading in Gridview RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I am trying below code it is working but i want to start from 5th column. 

    protected void OnDataBound(object sender, EventArgs e)
    {
    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
    TableHeaderCell cell = new TableHeaderCell();
    cell.Text = "ChkAll";
    cell.ColumnSpan = 1;
    row.Controls.Add(cell);

    cell = new TableHeaderCell();
    cell.ColumnSpan = 1;
    cell.Text = "Sr.";
    row.Controls.Add(cell);

    cell = new TableHeaderCell();
    cell.ColumnSpan = 1;
    cell.Text = "Type";
    row.Controls.Add(cell);

    cell = new TableHeaderCell();
    cell.ColumnSpan = 1;
    cell.Text = "Name";
    row.Controls.Add(cell);

    cell = new TableHeaderCell();
    cell.ColumnSpan = 1;
    cell.Text = "Department";
    row.Controls.Add(cell);

    cell = new TableHeaderCell();
    cell.ColumnSpan = 6;
    cell.Text = "NS";
    row.Controls.Add(cell);

    gvw.HeaderRow.Parent.Controls.AddAt(0, row);
    }

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
    
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                            <div class="table-responsive">
    
                                <asp:GridView ID="gvw" runat="server" AutoGenerateColumns="false" Font-Size="9pt"
                                    BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="5px" CellPadding="3" ForeColor="Black"
                                    
                                    CssClass="table table-striped" GridLines="None" BorderStyle="None" EmptyDataText="No Records Found" EmptyDataRowStyle-ForeColor="Red">
                                    <AlternatingRowStyle BackColor="PaleGoldenrod" />
                                    <Columns>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                <asp:CheckBox ID = "chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText = "Sr.">
                                            <ItemTemplate>
                                                <asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        
                                        <asp:BoundField DataField="StoreCategory" HeaderText = "Type" ReadOnly="True" SortExpression="StoreCategory"/>
                                        <asp:BoundField DataField="Name"  HeaderText = "Name" ReadOnly="True" SortExpression="Name" />
                                        <asp:BoundField DataField="Department"  HeaderText = "Department" ReadOnly="True" SortExpression="Department" />
                                        <asp:TemplateField HeaderText = "Amount">
                                            <ItemTemplate>
                                                <asp:Label ID="lblAmount" runat="server" Text='<%# Eval("Amount") %>' CssClass="mytxtWidth75"></asp:Label>
                                                <asp:TextBox ID="txtAmount" Text='<%# Eval("Amount") %>'  CssClass="mytxtWidth75" runat="server" Visible = "false"/>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText = "Flat %">
                                            <ItemTemplate>
                                                <asp:Label ID="lblFixedPercent" runat="server" Text='<%# Eval("FixedPercentage") %>' CssClass="mytxtWidth30"></asp:Label>
                                                <asp:TextBox ID="txtFixedPercent" Text='<%# Eval("FixedPercentage") %>' CssClass="mytxtWidth30" runat="server" Visible = "false"/>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:Label ID="lblNMSDL1" runat="server" Text='<%# Eval("NMS-DL1") %>'></asp:Label>
                                                <asp:TextBox ID="txtNMSDL1" Text='<%# Eval("NMS-DL1") %>' CssClass="mytxtWidth30" runat="server" Visible = "false"/>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:Label ID="lblNMSBFB1" runat="server" Text='<%# Eval("NMS-BFB1") %>'></asp:Label>
                                                <asp:TextBox ID="txtNMSBFB1" Text='<%# Eval("NMS-BFB1") %>' CssClass="mytxtWidth30" runat="server" Visible = "false"/>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:Label ID="lblNMSDL2" runat="server" Text='<%# Eval("NMS-DL2") %>'></asp:Label>
                                                <asp:TextBox ID="txtNMSDL2" Text='<%# Eval("NMS-DL2") %>' CssClass="mytxtWidth30" runat="server" Visible = "false"/>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:Label ID="lblNMSBFB2" runat="server" Text='<%# Eval("NMS-BFB2") %>'></asp:Label>
                                                <asp:TextBox ID="txtNMSBFB2" Text='<%# Eval("NMS-BFB2") %>' CssClass="mytxtWidth30" runat="server" Visible = "false"/>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:Label ID="lblNMSDL3" runat="server" Text='<%# Eval("NMS-DL3") %>'></asp:Label>
                                                <asp:TextBox ID="txtNMSDL3" Text='<%# Eval("NMS-DL3") %>' CssClass="mytxtWidth30" runat="server" Visible = "false"/>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:Label ID="lblNMSBFB3" runat="server" Text='<%# Eval("NMS-BFB3") %>'></asp:Label>
                                                <asp:TextBox ID="txtNMSBFB3" Text='<%# Eval("NMS-BFB3") %>' CssClass="mytxtWidth30" runat="server" Visible = "false"/>
                                            </ItemTemplate>
                                        </asp:TemplateField>
    
                                    </Columns>
                                </asp:GridView>
    
                            </div>
                        </div>
            </div>
        </div>
    </asp:content>

    Thanks

    Thanks

    Monday, August 6, 2018 4:06 AM

Answers

All replies

  • User-893317190 posted

    Hi JagjitSingh,

    You could  add a new cell to your common  heading row  before add  the cell with your wanted content.

    However ,I find your code doesn’t in the page_load event . 

    I suggest you could add your head in pageload event , because  onDataBound event will not fire when you postback(viewstate has save the state, so it needn’t bind data), which  will cause  an empty second row when you click the checkbox.

    So I add the common header in page_load event  and   remove the empty second row  every time   the page posts back.

    Below is my code.

    protected void Page_Load(object sender, EventArgs e)
            {
    
                GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
                TableHeaderCell cell = new TableHeaderCell();
                cell.ColumnSpan = 4;
                row.Controls.Add(cell);
                cell = new TableHeaderCell();
                cell.Text = "ChkAll";
                cell.ColumnSpan = 1;
                row.Controls.Add(cell);
    
                cell = new TableHeaderCell();
                cell.ColumnSpan = 1;
                cell.Text = "Sr.";
                row.Controls.Add(cell);
    
                cell = new TableHeaderCell();
                cell.ColumnSpan = 1;
                cell.Text = "Type";
                row.Controls.Add(cell);
    
                cell = new TableHeaderCell();
                cell.ColumnSpan = 1;
                cell.Text = "Name";
                row.Controls.Add(cell);
    
                cell = new TableHeaderCell();
                cell.ColumnSpan = 1;
                cell.Text = "Department";
                row.Controls.Add(cell);
    
                cell = new TableHeaderCell();
                cell.ColumnSpan = 6;
                cell.Text = "NS";
                row.Controls.Add(cell);
    
    
                if (IsPostBack)
                {
                    gvw.HeaderRow.Parent.Controls.RemoveAt(1);
                }
    
    
    
                gvw.HeaderRow.Parent.Controls.AddAt(0, row);
    
            }
    

    Best regards ,

    Ackerly Xu

    Tuesday, August 7, 2018 7:29 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 7, 2018 12:51 PM