You must Sign In to post a response.
  • Category: ASP.NET

    How remove empty row of header from Datagrid

    I have create a datagrid. In the datagrid have merge column for header.
    so i have use Itemcreated events from datagrid. But problem is that,
    it is created another/extra rows which I don't need. So how remove this rows

    e.g.Heading of datagrid
    -----------------------------------------------------
    toll Plaza | total collection |
    -------------------------------------------
    | amount A | Amount B| total amount|
    -----------------------------------------------------
    | | | |
    -----------------------------------------------------

    code :
    protected void dgTollData_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    try
    {
    if (e.Item.ItemType == ListItemType.Header)
    {
    DataGridItem dgItem = default(DataGridItem);
    TableCell dgCell = new TableCell();
    dgItem = new DataGridItem(0, 0, ListItemType.Header);

    dgCell = new TableCell();
    dgCell.RowSpan = 2;
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgCell.VerticalAlign = VerticalAlign.Middle;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Toll Plaza";

    dgCell = new TableCell();
    dgCell.RowSpan = 2;
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgCell.VerticalAlign = VerticalAlign.Middle;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "From Date";

    dgCell = new TableCell();
    dgCell.RowSpan = 2;
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgCell.VerticalAlign = VerticalAlign.Middle;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Total Traffic";

    dgCell = new TableCell();
    dgCell.RowSpan = 2;
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgCell.VerticalAlign = VerticalAlign.Middle;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Total Exempted";

    dgCell = new TableCell();
    dgCell.ColumnSpan = 6;
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgCell.VerticalAlign = VerticalAlign.Middle;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Total Non-Exempted";

    this.dgTollData.Controls[0].Controls.AddAt(0, dgItem);

    dgItem = new DataGridItem(0, 1, ListItemType.Header);

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "LMV (Car,Jeep)";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "LCV (Mini Bus/Tempo)";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Bus/Truck";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "3 Axle Vehicle";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Oversie Vehicles";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Total";

    this.dgTollData.Controls[0].Controls.AddAt(1, dgItem);
    }

    //for group of items
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    if (createsub)
    {
    DataGrid dg = (DataGrid)sender;
    TableCell tc = new TableCell();
    tc.Controls.Add(new LiteralControl(subheading));
    tc.ColumnSpan = 11;
    tc.Attributes.Add("align", "left");
    tc.Font.Bold = true;
    tc.BackColor = ColorTranslator.FromHtml("#F08080");
    tc.ForeColor = Color.White;
    DataGridItem di = new DataGridItem(e.Item.ItemIndex + 1, 0, ListItemType.Item);
    di.Cells.Add(tc);
    Table t = (Table)dg.Controls[0];
    t.Rows.Add(di);
    }
    }
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }
  • #769148
    can you post your aspx design so that we can help you more in order to resolve your query.
    have you use 'TemplateField ' for your header or what ? Please elaborate

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #769150
    <div class="container" style="margin-top: -0.5%;">
    <div class="row" id="dTollAnalysisReport" runat="server" visible="true">
    <div class="col-md-11">
    <div style="margin-left: 90%;">
    <asp:Button ID="bPrint" runat="server" Text="Print" Height="30px" Width="75px" BackColor="#293339"
    ForeColor="White" />
    </div>
    <br />
    <div id="no-more-tables">
    <div class="row" style="margin-left: 0.7%;">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
    <asp:DataGrid ID="dgTollData" runat="server" AutoGenerateColumns="false" GridLines="None"
    CssClass="table table-hover table-bordered table-striped" OnItemCreated="dgTollData_ItemCreated"
    OnItemDataBound="dgTollData_ItemDataBound" Font-Bold="true" Font-Size="12px">
    <HeaderStyle CssClass="pageheading" Width="80%" HorizontalAlign="Center" BackColor="#DC143C"
    Font-Bold="True" ForeColor="White" />
    <AlternatingItemStyle CssClass="pageheading" BackColor="White" ForeColor="#284775" />
    <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <Columns>
    <asp:BoundColumn DataField="PROJECT_NAME" HeaderText="Project Name"></asp:BoundColumn>
    <asp:BoundColumn DataField="TOLL_REGIMENT_NAME" ItemStyle-Font-Size="12px" ItemStyle-Width="15%">
    </asp:BoundColumn>
    <asp:TemplateColumn ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
    <asp:Label ID="lbDateTime" runat="server" Text='<%#Eval("FROMDATE")%>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
    <asp:Label ID="lbDateTime" runat="server" Text='<%#Eval("TODATE")%>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="TOTAL_TRAFFIC" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
    <asp:BoundColumn DataField="TOTAL_EXEMPTED" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
    <asp:BoundColumn DataField="LMV" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
    <asp:BoundColumn DataField="LCV" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
    <asp:BoundColumn DataField="HVM" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
    <asp:BoundColumn DataField="HCM" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
    <asp:BoundColumn DataField="MULTIAXEL" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
    <asp:BoundColumn DataField="TOTAL_NON_EXEMPTED" ItemStyle-HorizontalAlign="Center">
    </asp:BoundColumn>
    </Columns>
    </asp:DataGrid>
    <asp:Timer ID="tTimer" runat="server" OnTick="tTimer_Tick" Interval="10500" Enabled="true">
    </asp:Timer>
    </ContentTemplate>
    </asp:UpdatePanel>
    </div>
    </div>
    </div>
    </div>
    </div>
    <asp:UpdateProgress ID="upProgress" runat="server" DisplayAfter="0" AssociatedUpdatePanelID="UpdatePanel1"
    DynamicLayout="false">
    <ProgressTemplate>
    <div id="dProcessing" class="Box-Half">
    </div>
    </ProgressTemplate>
    </asp:UpdateProgress>

    code;
    ======
    protected void dgTollData_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    try
    {
    if (e.Item.ItemType == ListItemType.Header)
    {
    DataGridItem dgItem = default(DataGridItem);
    TableCell dgCell = new TableCell();
    dgItem = new DataGridItem(0, 0, ListItemType.Header);

    dgCell = new TableCell();
    dgCell.RowSpan = 2;
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgCell.VerticalAlign = VerticalAlign.Middle;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Toll Plaza";

    dgCell = new TableCell();
    dgCell.ColumnSpan = 2;
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgCell.VerticalAlign = VerticalAlign.Middle;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Date";

    dgCell = new TableCell();
    dgCell.RowSpan = 2;
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgCell.VerticalAlign = VerticalAlign.Middle;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Total Traffic";

    dgCell = new TableCell();
    dgCell.RowSpan = 2;
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgCell.VerticalAlign = VerticalAlign.Middle;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Total Exempted";

    dgCell = new TableCell();
    dgCell.ColumnSpan = 6;
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgCell.VerticalAlign = VerticalAlign.Middle;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Total Non-Exempted";

    this.dgTollData.Controls[0].Controls.AddAt(0, dgItem);

    dgItem = new DataGridItem(0, 1, ListItemType.Header);

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "From";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "To";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "LMV (Car,Jeep)";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "LCV (Mini Bus/Tempo)";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Bus/Truck";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "3 Axle Vehicle";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Oversie Vehicles";

    dgCell = new TableCell();
    dgCell.HorizontalAlign = HorizontalAlign.Center;
    dgItem.Cells.Add(dgCell);
    dgCell.Text = "Total";

    this.dgTollData.Controls[0].Controls.AddAt(1, dgItem);
    }

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    if (createsub)
    {
    DataGrid dg = (DataGrid)sender;
    TableCell tc = new TableCell();
    tc.Controls.Add(new LiteralControl(subheading));
    tc.ColumnSpan = 11;
    tc.Attributes.Add("align", "left");
    tc.Font.Bold = true;
    tc.BackColor = ColorTranslator.FromHtml("#F08080");
    tc.ForeColor = Color.White;
    DataGridItem di = new DataGridItem(e.Item.ItemIndex + 1, 0, ListItemType.Item);
    di.Cells.Add(tc);
    Table t = (Table)dg.Controls[0];
    t.Rows.Add(di);
    }
    }
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }

    protected void dgTollData_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    HtmlAnchor headerName;

    try
    {
    e.Item.Cells[0].Visible = false;

    switch (e.Item.ItemType)
    {
    case ListItemType.Header:
    case ListItemType.AlternatingItem:
    case ListItemType.Item:
    DataTable dt = (DataTable)((DataGrid)sender).DataSource;
    // header or Subheading column value will change in next row
    if ((e.Item.ItemIndex == -1) ||
    (e.Item.ItemIndex + 1 < dt.Rows.Count &&
    dt.Rows[e.Item.ItemIndex + 1]["PROJECT_NAME"].ToString() !=
    dt.Rows[e.Item.ItemIndex]["PROJECT_NAME"].ToString() && dt.Rows[e.Item.ItemIndex + 1]["PROJECT_NAME"].ToString() != string.Empty))
    {
    // get next subheading value
    subheading = "<b>Project Name : " + dt.Rows[e.Item.ItemIndex + 1]["PROJECT_NAME"].ToString() + "</b>";
    createsub = true;
    }
    else
    createsub = false;
    break;
    }
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }


Sign In to post your comments