• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 908
  • Last Modified:

Creating Custom Rows in Datagrid


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,

  • 2
1 Solution
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);

DBAduck - Ben MillerPrincipal ConsultantCommented:
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" />


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now