Problem solve Get help with specific problems with your technologies, process and projects.

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()
 Me.dgCollections.DataMember = ""
 Me.dgCollections.HeaderForeColor = 
 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})
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

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

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

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

 TableAlreadyAdded = True
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.


 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.


 . . .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 last published in April 2003

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.