Create a DataGrid using a DataView as the source

This tip explains how to use DataView with a DataGrid and access individual fields using text field, checkboxes, etc., on the form outside the DataGrid.

This tip was submitted to the VS.NET Info Center by member Melvin Lervick. Please let other users know how useful it is by rating it below. Do you have an interesting .NET time-saver, troubleshooting technique, shortcut or code snippet of your own you'd like to share? Submit it here.


This tip explains how to use DataView with a DataGrid and access individual fields using text field, checkboxes, etc., on the form outside the DataGrid.

Here is the sample that creates a DataGrid using a DataView as the source for the grid. The initial sample is VB.NET. Some of the internal code snippets are C# .NET. I did this on purpose to emphasize that language should not be the driving factor for a project or example. If you can't use or at least read both, you need to get out the books.

…
 Private dvCollections As DataView
…
 Friend WithEvents dgCollections As System.Windows.Forms.DataGrid
…
 Me.dgCollections = New System.Windows.Forms.DataGrid()
…
 CType(Me.dgCollections, 
System.ComponentModel.ISupportInitialize).BeginInit()
…
 '
 'dgCollections
 '
 Me.dgCollections.DataMember = ""
 Me.dgCollections.HeaderForeColor = 
System.Drawing.SystemColors.ControlText
 Me.dgCollections.Location = New System.Drawing.Point(12, 12)
 Me.dgCollections.Name = "dgCollections"
 Me.dgCollections.PreferredColumnWidth = 200
 Me.dgCollections.ReadOnly = True
 Me.dgCollections.Size = New System.Drawing.Size(768, 324)
 Me.dgCollections.TabIndex = 4
…
 Me.Controls.AddRange(New System.Windows.Forms.Control() 
{Me.txtStartingTable, Me.lblStartingTable, Me.btnOpenFileDialog, 
Me.txtLastUpdated, Me.lblLastUpdated, Me.txtLocation, Me.lblLocation, 
Me.txtName, Me.lblName, Me.dgCollections})
…
 CType(Me.dgCollections, 
System.ComponentModel.ISupportInitialize).EndInit()
…
Private Sub ConfigureDataGrid()
 Dim ts As New DataGridTableStyle()
 ts.MappingName = "Collections"
 ' Set other properties.
 ts.AlternatingBackColor = Color.LightGray
 ' Add first column style.
 Dim TextCol As New DataGridTextBoxColumn()
 TextCol.MappingName = "Name"
 TextCol.HeaderText = "Collection Name"
 TextCol.Width = 130
 ts.GridColumnStyles.Add(TextCol)

 ' Add second column style.
 Dim TextCol2 As New DataGridTextBoxColumn()
 TextCol2.MappingName = "Path"
 TextCol2.HeaderText = "Location"
 TextCol2.Width = 275
 ts.GridColumnStyles.Add(TextCol2)

 ' Add third column style.
 Dim TextCol3 As New DataGridTextBoxColumn()
 TextCol3.MappingName = "StartingTable"
 TextCol3.HeaderText = "Start Table Name"
 TextCol3.Width = 135
 ts.GridColumnStyles.Add(TextCol3)

 ' Add fourth column style.
 Dim Updated As New DataGridTextBoxColumn()
 Updated.MappingName = "LastUpdated"
 Updated.HeaderText = "Last Updated"
 Updated.Width = 175
 ts.GridColumnStyles.Add(Updated)

 TableAlreadyAdded = True
 dgCollections.TableStyles.Add(ts)
End Sub

…
 dvCollections = New DataView(dd.ds.Tables("Collections"))
 dgCollections.DataSource = dvCollections
…

Everything to this point is pretty straightforward and reasonably well documented. After a row is selected in the DataGrid, the objective is to populate standard fields on the form for editing purposes. (Note: The fields in the GRID were set to ReadOnly.) How you access the individual row and its fields is NOT well documented. The .NET environment defines DATAVIEWS, DATAROWVIEWS, CURRENCYMANAGER and a number of other objects that can be used, but there are very few examples. After a tedious search of the online help, I decided that the CurrencyManager could be used to define DataRowView.

<C#>

 DataRowView drv;
CurrencyManager cm;
 cm = (CurrencyManager)Me.BindingContext[dvCollections];
 drv = (DataRowView)cm.Current;

Individual fields can now be accessed using the following:

. . .drv.Row["Assigned"]. . .;

What isn't obvious is the most straightforward way to access the Row and Field.

<C#>

 . . .dvCollections[dgCollections.CurrentRowIndex]["Assigned"
] . . . ;

It is really too bad that there are no good examples of how to define and use DataGrids. The DataGrid could be used by a lot more people if there were examples that described the HOW TO. I've found that once you know how to use DataViews and DataGrids, it is pretty easy. The LEARNING CURVE is very long though. I'm sure many of us give up before we ever really get proficient using the DataGrid. There are also a lot of things that I still do not know how to do, so if you have readers who have learned HOW TO, you could create an entire book on the information that they have acquired.

This was first published in April 2003

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close