We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Creating Custom Rows in Datagrid

fevster asked
Medium Priority
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,

Watch Question

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
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.
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);

SQL Server Architect
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" />



Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.