?
Solved

Creating Custom Rows in Datagrid

Posted on 2006-04-05
4
Medium Priority
?
905 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:fevster
  • 2
4 Comments
 
LVL 1

Expert Comment

by:bonfire89
ID: 16384421
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
0
 
LVL 1

Expert Comment

by:ghostReckon
ID: 16386934
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.
0
 
LVL 1

Expert Comment

by:ghostReckon
ID: 16386953
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
0
 
LVL 25

Accepted Solution

by:
DBAduck - Ben Miller earned 500 total points
ID: 16389164
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month16 days, 1 hour left to enroll

850 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