Google
WWW Yariv Hammer's Code Site

Wednesday, October 26, 2005

How-To Show Different Columns in the DataGrid of the same DataTable

Introduction
In my previous post I showed how to select which rows to show in the DataGrid. Now I will take this one step further, and show how to add buttons in the form which when clicked change which columns are shown in the DataGrid.
This can be very useful on large tables with many columns. We want to have different "views" of the same DataTable, instead of showing all the data at once.

Example
In this example I have a form with a DataGrid and two Buttons. On Form_Load I create a DataTable with 100 columns (which might simulate your problem). I create a DataGridColumnStyle for each column, and store them in a Hashtable, mapping each column index with the appropriate ColumnStyle:
------------------------------------------------

   Private ts As New DataGridTableStyle
   Private hash As New Hashtable
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      'Create the table
      Dim dt As DataTable = New DataTable("table")
      For i As Integer = 1 To 100
         dt.Columns.Add(New DataColumn("col" & i))
      Next

      Create a TableStyle
      ts.MappingName = "table"
      'Create the ColumnStyles and map them to the columns
      For i As Integer = 1 To 100
         Dim cs As New DataGridTextBoxColumn
         cs.MappingName = "col" & i
         cs.HeaderText = "col" & i
         hash(i) = cs
         'Only the first 10 columns is shown!!!!!
         If (i <= 10) Then _
            ts.GridColumnStyles.Add(cs)
      Next
      DataGrid1.TableStyles.Add(ts)
      DataGrid1.DataSource = dt
   End Sub

-------------------------------------------------
As you can see, all the ColumnStyles which are added to the TableStyle is shown to the user in the order of insertion. So in our case, we will see only the first 10. But I created 100 ColumnStyles.
On the first button click I will show the first 10 columns. On the second button click I will show the next 10 (11-21).
-------------------------------------------------

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   ts.GridColumnStyles.Clear()
   For i As Integer = 1 To 10
      ts.GridColumnStyles.Add(hash(i))
   Next
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   ts.GridColumnStyles.Clear()
   For i As Integer = 11 To 20
      ts.GridColumnStyles.Add(hash(i))
   Next
End Sub


-------------------------------------------------
You can, for example, create 10 radio buttons (shaped as buttons), each of them creates a different view, by selecting other columns to map.

0 Comments:

Post a Comment

<< Home

Feel free to use everything here. Add links to my site if you wish.

Do not copy anything to other sites without adding link to here.

All the contents of the site belong to Yariv Hammer.