Tip

Create a DataGrid using a DataView as the source

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
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
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.