Add rows to datagrid

T

Terry Holland

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
 
S

Steven Cheng[MSFT]

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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top