Add rows to datagrid

Discussion in 'ASP .Net Datagrid Control' started by Terry Holland, Apr 28, 2006.

  1. I want to create a datagrid with different "row types".
    To illustrate my point I'll describe a simplified version of what I want.

    I have a list of stock with the following fields:
    StockCode, Description, Price, NumberInStock.

    Stock is grouped into stock groups. What I would like to do is display the
    stock in a datagrid so that the stock appears under the stock group that it
    belongs to.

    ie
    Tube Materials
    10001, Steel Tube (1m), £10.00, 1000
    10002, Steel Tube (2m), £19.00, 1500
    Board Materials
    20001, Standard Board (4m), £15, 3000
    20002, Other Board, £10,2000

    etc...


    Is there any way that I can display my grid with the stock group headings in
    their own row in the grid followed by the stock in that group.

    The code that I have supplied is a very simplified version of what I am
    using at the moment (I actually use custom datgrid columns that get created
    at runtime). When this code runs, you will see that the stock group data is
    displayed as an extra column in the grid. I would like it to display as a
    seperate row, formatted differently to stock rows with a single column
    spanning entire grid.

    Id appreciate any examples of how I might achieve this

    Terry Holland


    '=================================================
    'STOCK CLASS
    '=================================================
    Public Class clsStock
    Private m_strStockCode As String
    Private m_strName As String
    Private m_sglPrice As Single
    Private m_intRemaining As Int16
    Private m_strStockGroup As String

    Private Sub New()

    End Sub

    Public Sub New(ByVal strStockCode As String, ByVal strName As String,
    ByVal sglPrice As Single, ByVal intRemaining As Integer, ByVal strStockGroup
    As String)
    StockCode = strStockCode
    Name = strName
    Price = sglPrice
    Remaining = intRemaining
    m_strStockGroup = strStockGroup
    End Sub

    Public Property StockCode() As String
    Get
    Return m_strStockCode
    End Get
    Set(ByVal Value As String)
    m_strStockCode = Value
    End Set
    End Property

    Public Property Name() As String
    Get
    Return m_strName
    End Get
    Set(ByVal Value As String)
    m_strName = Value
    End Set
    End Property

    Public Property Price() As Single
    Get
    Return m_sglPrice
    End Get
    Set(ByVal Value As Single)
    m_sglPrice = Value
    End Set
    End Property

    Public Property Remaining() As Int16
    Get
    Return m_intRemaining
    End Get
    Set(ByVal Value As Int16)
    m_intRemaining = Value
    End Set
    End Property
    Public Property StockGroup() As String
    Get
    Return m_strStockGroup
    End Get
    Set(ByVal Value As String)
    m_strStockCode = Value
    End Set
    End Property



    End Class

    '=================================================
    'STOCKLIST CLASS
    '=================================================
    Public Class clsStockList
    Inherits CollectionBase

    Public Sub New()
    Dim s As clsStock

    s = New clsStock("10001", "Steel Tube (1m)", 10, 1000, "TUBE
    MATERIALS")
    MyBase.List.Add(s)

    s = New clsStock("10002", "Steel Tube (2m)", 19, 2000, "TUBE
    MATERIALS")
    MyBase.List.Add(s)

    s = New clsStock("20001", "Standard Board", 10, 3000, "BOARD
    MATERIALS")
    MyBase.List.Add(s)

    s = New clsStock("20002", "Other Board", 13, 1000, "BOARD
    MATERIALS")
    MyBase.List.Add(s)

    End Sub
    End Class


    '=================================================
    'PAGE CODE
    '=================================================

    Public Class WebForm1
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    GetData()
    End Sub
    Private Sub GetData()
    Dim sl As New clsStockList

    DataGrid1.DataSource = sl
    DataGrid1.DataBind()
    End Sub

    End Class
     
    Terry Holland, Apr 28, 2006
    #1
    1. Advertising

  2. Terry Holland

    Teemu Keiski Guest

    Hi,

    the idea I presented with GridView in my blog post:

    GridView Sort Grouping
    http://aspadvice.com/blogs/joteke/archive/2006/02/11/15130.aspx

    applies equally for DataGrid. Note: when writing this, my blog site is going
    an upgrade to CS 2.0 so it might be unavailable for some time. Just keep
    trying.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    "Terry Holland" <> wrote in message
    news:%...
    >I want to create a datagrid with different "row types".
    > To illustrate my point I'll describe a simplified version of what I want.
    >
    > I have a list of stock with the following fields:
    > StockCode, Description, Price, NumberInStock.
    >
    > Stock is grouped into stock groups. What I would like to do is display
    > the
    > stock in a datagrid so that the stock appears under the stock group that
    > it
    > belongs to.
    >
    > ie
    > Tube Materials
    > 10001, Steel Tube (1m), £10.00, 1000
    > 10002, Steel Tube (2m), £19.00, 1500
    > Board Materials
    > 20001, Standard Board (4m), £15, 3000
    > 20002, Other Board, £10,2000
    >
    > etc...
    >
    >
    > Is there any way that I can display my grid with the stock group headings
    > in
    > their own row in the grid followed by the stock in that group.
    >
    > The code that I have supplied is a very simplified version of what I am
    > using at the moment (I actually use custom datgrid columns that get
    > created
    > at runtime). When this code runs, you will see that the stock group data
    > is
    > displayed as an extra column in the grid. I would like it to display as a
    > seperate row, formatted differently to stock rows with a single column
    > spanning entire grid.
    >
    > Id appreciate any examples of how I might achieve this
    >
    > Terry Holland
    >
    >
    > '=================================================
    > 'STOCK CLASS
    > '=================================================
    > Public Class clsStock
    > Private m_strStockCode As String
    > Private m_strName As String
    > Private m_sglPrice As Single
    > Private m_intRemaining As Int16
    > Private m_strStockGroup As String
    >
    > Private Sub New()
    >
    > End Sub
    >
    > Public Sub New(ByVal strStockCode As String, ByVal strName As String,
    > ByVal sglPrice As Single, ByVal intRemaining As Integer, ByVal
    > strStockGroup
    > As String)
    > StockCode = strStockCode
    > Name = strName
    > Price = sglPrice
    > Remaining = intRemaining
    > m_strStockGroup = strStockGroup
    > End Sub
    >
    > Public Property StockCode() As String
    > Get
    > Return m_strStockCode
    > End Get
    > Set(ByVal Value As String)
    > m_strStockCode = Value
    > End Set
    > End Property
    >
    > Public Property Name() As String
    > Get
    > Return m_strName
    > End Get
    > Set(ByVal Value As String)
    > m_strName = Value
    > End Set
    > End Property
    >
    > Public Property Price() As Single
    > Get
    > Return m_sglPrice
    > End Get
    > Set(ByVal Value As Single)
    > m_sglPrice = Value
    > End Set
    > End Property
    >
    > Public Property Remaining() As Int16
    > Get
    > Return m_intRemaining
    > End Get
    > Set(ByVal Value As Int16)
    > m_intRemaining = Value
    > End Set
    > End Property
    > Public Property StockGroup() As String
    > Get
    > Return m_strStockGroup
    > End Get
    > Set(ByVal Value As String)
    > m_strStockCode = Value
    > End Set
    > End Property
    >
    >
    >
    > End Class
    >
    > '=================================================
    > 'STOCKLIST CLASS
    > '=================================================
    > Public Class clsStockList
    > Inherits CollectionBase
    >
    > Public Sub New()
    > Dim s As clsStock
    >
    > s = New clsStock("10001", "Steel Tube (1m)", 10, 1000, "TUBE
    > MATERIALS")
    > MyBase.List.Add(s)
    >
    > s = New clsStock("10002", "Steel Tube (2m)", 19, 2000, "TUBE
    > MATERIALS")
    > MyBase.List.Add(s)
    >
    > s = New clsStock("20001", "Standard Board", 10, 3000, "BOARD
    > MATERIALS")
    > MyBase.List.Add(s)
    >
    > s = New clsStock("20002", "Other Board", 13, 1000, "BOARD
    > MATERIALS")
    > MyBase.List.Add(s)
    >
    > End Sub
    > End Class
    >
    >
    > '=================================================
    > 'PAGE CODE
    > '=================================================
    >
    > Public Class WebForm1
    > Inherits System.Web.UI.Page
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > InitializeComponent()
    >
    > End Sub
    > Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    >
    > 'NOTE: The following placeholder declaration is required by the Web
    > Form
    > Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web Form Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > GetData()
    > End Sub
    > Private Sub GetData()
    > Dim sl As New clsStockList
    >
    > DataGrid1.DataSource = sl
    > DataGrid1.DataBind()
    > End Sub
    >
    > End Class
    >
    >
    >
     
    Teemu Keiski, Apr 30, 2006
    #2
    1. Advertising

  3. Hi Terry,

    In addition to Teemu's suggesion on customizing the GridView or DataGrid
    control(modifying the internal control structure), you can also consider
    using Nested template databound control with hierarchical data binding.
    For example, you can use a DataList or repeater to display the main Groups,
    and in each Item of the repeater, display a nested sub DataGrid which
    display the detail rows belong to that group item. Here are some msdn
    articles discussing on nested databound controls with hierarchical data
    binding:

    #Nested Grids for Hierarchical Data
    http://msdn.microsoft.com/msdnmag/issues/03/10/cuttingedge/

    #Hierarchical Data Binding in ASP.NET
    http://msdn.microsoft.com/library/en-us/dnaspp/html/aspn-hierdatabinding.asp
    ?frame=true

    Hope this also helps.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], May 1, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?c2VudGhpbA==?=

    RE: add rows to datagrid at client

    =?Utf-8?B?c2VudGhpbA==?=, Aug 12, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    3,642
    =?Utf-8?B?c2VudGhpbA==?=
    Aug 12, 2004
  2. =?Utf-8?B?QW5vbg==?=

    add summary rows to datagrid

    =?Utf-8?B?QW5vbg==?=, Jan 25, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    484
    =?Utf-8?B?QW5vbg==?=
    Jan 25, 2005
  3. Subba Rao via DotNetMonster.com

    script for moving rows up and down and traverse thru rows of HTML table

    Subba Rao via DotNetMonster.com, Mar 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    8,342
    Subba Rao via DotNetMonster.com
    Mar 19, 2005
  4. Arjen Hoekstra
    Replies:
    0
    Views:
    588
    Arjen Hoekstra
    Aug 2, 2005
  5. locitt
    Replies:
    0
    Views:
    151
    locitt
    Feb 2, 2004
Loading...

Share This Page