Creating Custom Rows in Datagrid

Posted on 2006-04-05
Last Modified: 2012-05-05

I am a little stuck when it comes to creating custom rows in a datagrid. Basically I need to bind data to a datagrid but I need to place a row above and below the first record that I bind to the datagrid. These rows should span across the datagrid and should have specific text within them.

Is this possible? Or is the datagrid to rigid for this to happen?

Thanks for any help,

Question by:fevster
    LVL 1

    Expert Comment

    it might only take a header and footer

    when I learned datagrids, this tutorial was a HUGE help

    since then, I primarily use datalists for everything

    or even generate my own html
    LVL 1

    Expert Comment

    Add the rows to the datatable that is being binded to the datagrid.

    //inserted as first row
          myTable.Rows.InsertAt(myTable.NewRow(), 0);
    //inserted as last row

    Then bind datagrid to this datatable.
    LVL 1

    Expert Comment

    Sorry guys...i misread the original comments. if you want it above adn below the first record then:

    //below first record
         myTable.Rows.InsertAt(myTable.NewRow(), 1);
    //above first record
         myTable.Rows.InsertAt(myTable.NewRow(), 0);

    LVL 24

    Accepted Solution

    This should give you an idea of how to accomplish this.

    <%@ Page Language="c#" %>
    <%@ import Namespace="System.Data" %>

    <script language="c#" runat="server">

    private string _department = String.Empty;

    private void Page_Load()
          DataTable dt = new DataTable();
          dt.Columns.Add("Department", typeof(string));
          dt.Columns.Add("EmployeeId", typeof(int));
          dt.Columns.Add("Fname", typeof(string));

          dt.Rows.Add(new object[] {"Architecture", 1, "Ben"});
          dt.Rows.Add(new object[] {"IT", 2, "Sara"});
          dt.Rows.Add(new object[] {"Transportation", 3, "Lea"});
          dt.Rows.Add(new object[] {"Architecture", 4, "UserArchitecture"});
          dt.Rows.Add(new Object[] {"IT", 5, "User6"});

          DataView dv = new DataView(dt, "", "Department, Fname", DataViewRowState.CurrentRows);

          dgAlerts.DataSource= dv;

    private void dgAlerts_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            DataGridItem item = e.Item;

            // if its not an item or alternating item, get out of here
            if (item.ItemType != ListItemType.Item & item.ItemType != ListItemType.AlternatingItem)

            // we need to access the datarow to get the text, so lets get it
            DataRow dataRow = ((DataRowView) item.DataItem).Row;

          // check for the next department and if new, then display header
          if(dataRow["Department"].ToString() != _department)
                _department = dataRow["Department"].ToString();

                  //now we need to create the new row, add the cell and put in sometext.
                  DataGridItem oNewItem = new DataGridItem(item.ItemIndex,item.DataSetIndex,item.ItemType);
                  TableCell oNewCell = new TableCell();
                  oNewItem.Cells.Add( oNewCell);
                  oNewCell.ColumnSpan = item.Cells.Count;
                  Label oFullText = new Label();
                oFullText.Font.Bold = true;
                  oFullText.Text = dataRow["Department"].ToString();

                  // now w need to add it to the table (is always first item indatagrid controls) and we're done!



    <asp:DataGrid id="dgAlerts" OnItemCreated="dgAlerts_ItemCreated" AutoGenerateColumns="false" runat="server">
          <asp:BoundColumn DataField="EmployeeId" HeaderText="EmployeeID" />
          <asp:BoundColumn DataField="Fname" HeaderText="FirstName" />



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now