943,587 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 19764
  • VB.NET RSS
Mar 1st, 2006
0

Pivot table in DataGrid

Expand Post »
Hi,

I need help solving this problem.
I have a dataset with four columns col1,col2,col3,col4 and the data could be
col1 col2 col3 col4
=====================================
t1 abc a 1
t1 xyz a 2
t1 mnp b 3
t2 abc 1
t2 kkl 2

I want to create a datagrid which will have, column headings from the col1 data (t1 and t2 as columns) starting from 2nd column in data grid
1st column heading should be blank (like in excel spreadsheet)
and under 1st column in the datagrid, col2 (from source dataset) values should appear as rows
something like this

t1 t2
===========================
abc
xyz
mnp
kkl

that's not it, now remaining two columns col3 and col4 from the source dataset are the attributes of rows, so somehow I need to split each cell under column t1 and t2 and show values of col3 and col4

so far I could get the column headings t1 and t2
by writing separate query for col2 I can even get the first column in datagrid populated but I am clueless how to get two attributes in one cell

any idea ?

thanks in advance
VB.NET Syntax (Toggle Plain Text)
  1. Dim MySQLString As String
  2.  
  3. Dim objParam1 As SqlParameter
  4. Dim MyConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("Conn"))
  5. Dim dest As DataTable
  6.  
  7.  
  8. Try
  9. Session("Current_PackageId") = PackageNameDDL.SelectedItem.Value
  10.  
  11. 'MySQLString = "SELECT DISTINCT Component.Description,Song.SongTitle, "
  12. 'MySQLString = MySQLString + " XREFCompSong.ComponentSeq, XREFCompSong.ComponentSide "
  13. 'MySQLString = MySQLString + " FROM XREFCompSong INNER JOIN"
  14. 'MySQLString = MySQLString + " Song ON XREFCompSong.SongID = Song.PXSongID INNER JOIN"
  15. 'MySQLString = MySQLString + " Component ON XREFCompSong.ComponentID = Component.ComponentID "
  16. 'MySQLString = MySQLString + " WHERE (Component.PackageID = @vPackageId)"
  17.  
  18. MySQLString = "SELECT DISTINCT Component.Description"
  19. MySQLString = MySQLString + " FROM Component "
  20. MySQLString = MySQLString + " WHERE (Component.PackageID = @vPackageId)"
  21.  
  22. Dim MySqldataAdapter As SqlDataAdapter = New SqlDataAdapter(MySQLString, MyConn)
  23. Dim MyDataSet As DataSet = New DataSet
  24.  
  25. objParam1 = MySqldataAdapter.SelectCommand.Parameters.Add("@vPackageId", SqlDbType.Int)
  26. objParam1.Value = PackageNameDDL.SelectedItem.Value
  27.  
  28. If MyConn.State = ConnectionState.Closed Then
  29. MyConn.Open()
  30. End If
  31.  
  32. MySqldataAdapter.Fill(MyDataSet)
  33.  
  34. dest = New DataTable("PivotTable")
  35. dest.Columns.Add(" ")
  36.  
  37. Dim r As DataRow
  38.  
  39. For Each r In MyDataSet.Tables(0).Rows
  40. dest.Columns.Add(r(0))
  41. Next
  42.  
  43. DataGrid1.DataSource = dest
  44. DataGrid1.DataBind()
  45.  
  46. MyConn.Dispose()
  47. MyConn.Close()
  48. Catch ex As Exception
  49. System.Diagnostics.Trace.WriteLine("[ShowBtnClick] Exception " & ex.Message)
  50. Finally
  51. MyConn.Close()
  52. End Try
Notice that I've commented out first query, because with that query I get error while creating columns that "column t1 is already in the dataset", since t1 repeates in the query result.

Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Texpert is offline Offline
14 posts
since Jan 2006
Feb 26th, 2008
0

Re: Pivot table in DataGrid

Hi there did you get any reply from anywhere for this problem of yours because i am also stuck with almost same kind of thing.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
luciano_sunny is offline Offline
4 posts
since Feb 2008
Jul 19th, 2008
0

Re: Pivot table in DataGrid

Sound like you need a real pivot table gird with some customization capabilities. There are a few of them on the market but usually quite pricy. You can try the VIBlend SuperGridView. http://www.viblend.com. Not too expensive and does a great job.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JakeDB is offline Offline
1 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: VB Error's I don't know
Next Thread in VB.NET Forum Timeline: Problem in uploading VB project on the server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC