Put multiple data in one cell ASP.NET, VB.NET
How can i put multiple data in one datagrid cell? For example, a user can be a member of different organisations.
But i dont want the table to create a new datagrid row for the same user that开发者_JAVA技巧 is a member of multiple organisations.
This is how the code looks like right now:
Dim dSourceMemberOf As New SqlDataAdapter("QUERY", dbConn)
This fills the datagrid, but for some users there are more then 5 rows. So i'd like to put for one particular column all data in one cell.
How can this be done?
Edit:
This is how it woud look like right now:
username memberof
user1 dft
user1 kbo
user2 test
And this is how i want it to looke like:
username memberof
user1 dft
kbo
user2 test
I think the best solution is to group the data using a Linq query, then bind the grouped data to a gridview:
So the GridView would look like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="UserName" />
<asp:TemplateField>
<ItemTemplate>
<asp:ListBox ID="lst" runat="server" DataSource="<%# Container.DataItem.MemberOfGrouped %>" DataTextField="MemberOf"></asp:ListBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and here is some example code to populate it:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim UngroupedData As List(Of YourData) = New List(Of YourData)()
Dim item1 As YourData = New YourData()
item1.MemberOf = "dft"
item1.UserName = "user1"
UngroupedData.Add(item1)
Dim item2 As YourData = New YourData()
item2.MemberOf = "kbo"
item2.UserName = "user1"
UngroupedData.Add(item2)
Dim grouped = From u In UngroupedData Group u By Key = u.UserName Into Group _
Select UserName = Key, MemberOfGrouped = Group.ToList()
GridView1.DataSource = grouped
GridView1.DataBind()
End Sub
Public Class YourData
Private _userName As String
Public Property UserName() As String
Get
Return _userName
End Get
Set(ByVal value As String)
_userName = value
End Set
End Property
Private _memberOf As String
Public Property MemberOf() As String
Get
Return _memberOf
End Get
Set(ByVal value As String)
_memberOf = value
End Set
End Property
End Class
Sorry, I think on paint was wrong, concept was right though - looks like you need to do it on the databind event event... more here (first three links will show you how to do what you require I think) - you could look at the row you are on, get the previous row, then decide what to do with a cell in your current row based on the prev row
You could use a table inside the GridView-Cell (or a Listbox like Ross Scott suggested).
<ItemTemplate>
<asp:table id="TblUserGroups" runat="server" Height="100%" Width="100%" CellSpacing="0" CellPadding="0">
</asp:table>
</ItemTemplate>
and in RowDataBound of gridview(ItemDataBound of Datagrid works similar):
Protected Sub Gridview1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.DataRow
Dim drUser As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
Dim TblUserGroups As Table = DirectCast(e.Row.FindControl("TblUserGroups"), Table)
Dim userID as int32 = Ctype(drUser(0), Int32)
Dim tblGroups as DataTable = getTblUserGroups(userID) 'write a function that returns the Groups of a given User f.e. as DataTable'
If tblGroups.Rows.Count = 0 Then
Dim tr As New TableRow
Dim td As New TableCell
Dim LblNoGroup As New Label
LblNoGroup .Text = "no user-group"
td.CssClass = "UserWithoutGroup"
td.Controls.Add(LblNoGroup)
tr.Cells.Add(td)
TblUserGroups.Rows.Add(tr)
Else
For Each groupRow As DataRow In tblGroups.Rows
Dim tr As New TableRow
Dim td As New TableCell
Dim LblGroup As New Label
LblGroup.Text = groupRow("GroupName").ToString 'The groups name column'
td.Controls.Add(LblGroup)
tr.Cells.Add(td)
TblUserGroups.Rows.Add(tr)
Next
End If
End Select
End Sub
I think this is the most flexible way.
精彩评论