Creating Custom Rows in Datagrid

Hi,

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,

Kevin
fevsterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bonfire89Commented:
it might only take a header and footer

when I learned datagrids, this tutorial was a HUGE help

http://aspnet.4guysfromrolla.com/articles/040502-1.aspx

since then, I primarily use datalists for everything

or even generate my own html
ghostReckonCommented:
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
      myTable.Rows.Add(myTable.NewRow());

Then bind datagrid to this datatable.
ghostReckonCommented:
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);

Cheers
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;
      dgAlerts.DataBind();
}

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

        // 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();
              oNewCell.Controls.Add(oFullText);

              // now w need to add it to the table (is always first item indatagrid controls) and we're done!
              dgAlerts.Controls[0].Controls.Add(oNewItem);
      }
}

</script>


<html>
<head><title>DataGrid</title>
<body>

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

</body>
</html>

Ben.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.