Merge cells in datagrid (.NET 1.1)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codesid
    New Member
    • Dec 2006
    • 6

    Merge cells in datagrid (.NET 1.1)

    Just to add the info in this community that I finally found a way to merge cells in a datagrid. In many other forums over the Internet we hear many different opinions, usually asking you to move to ASP.NET 2.0. For those still struggling in 1.1, this is a way out:

    When you bind the dataset in the datagrd, it creates an event that you can use to change the way the data will be binded to it. In the InitializeCompo nent(), add the code:

    private void InitializeCompo nent()
    {
    // ... there will be more code here

    this.MyDataGrid .ItemDataBound += new System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.MyDa taGrid_ItemData BoundEventHandl er);
    }

    where the MyDataGrid is my datagrid object.

    Then, this event will be handled at:

    private void MyDataGrid_Item DataBoundEventH andler(object sender, DataGridItemEve ntArgs e)
    {
    switch(e.Item.I temType)
    {
    case ListItemType.It em:
    case ListItemType.Al ternatingItem:
    e.Item.Cells[0].ColumnSpan = 2;
    e.Item.Cells[1].Visible = false;
    break;
    }

    }

    This code above will be executed for every row. For the ones you want to specify the colspan (merge) or not, just handle it with a if else condition statement. It works just fine!

    Happy programming!
  • enreil
    New Member
    • Jan 2007
    • 86

    #2
    Thanks for the tip!

    Originally posted by codesid
    Just to add the info in this community that I finally found a way to merge cells in a datagrid. In many other forums over the Internet we hear many different opinions, usually asking you to move to ASP.NET 2.0. For those still struggling in 1.1, this is a way out:

    When you bind the dataset in the datagrd, it creates an event that you can use to change the way the data will be binded to it. In the InitializeCompo nent(), add the code:

    private void InitializeCompo nent()
    {
    // ... there will be more code here

    this.MyDataGrid .ItemDataBound += new System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.MyDa taGrid_ItemData BoundEventHandl er);
    }

    where the MyDataGrid is my datagrid object.

    Then, this event will be handled at:

    private void MyDataGrid_Item DataBoundEventH andler(object sender, DataGridItemEve ntArgs e)
    {
    switch(e.Item.I temType)
    {
    case ListItemType.It em:
    case ListItemType.Al ternatingItem:
    e.Item.Cells[0].ColumnSpan = 2;
    e.Item.Cells[1].Visible = false;
    break;
    }

    }

    This code above will be executed for every row. For the ones you want to specify the colspan (merge) or not, just handle it with a if else condition statement. It works just fine!

    Happy programming!

    Comment

    Working...