DataView to DataGridView binding problem

G

Guest

How do I bind my DataView to my DataGridView control? Here's the scenario:
because I want to make my first column a check box, I have defined a
collection for my data grid in the designer. I also had to specify the
columns for my dataTable (see code below). After I did that, there were 8
columns displayed in the grid (the first 4 from my collection (they were
empty) and the last 4 populated with the DataView data). I set the grid's
AutoGenerateColumns property to False so that I see only 4 columns (those
defined in the collection) instead of 8, but how do I get them populated
witht the DataView data? dgvTables.DataSource = myDataView doesn't do the
trick, I don't know what I'm doing wrong.

dgvTables.AutoGenerateColumns = False
Dim dt As New DataTable
dt.Columns.AddRange(New DataColumn(3) {New DataColumn("Copy"), New
DataColumn("Source Table"), New DataColumn("Destination Table"), New
DataColumn("Columns")})
....
Dim dr1 As DataRow = dt.NewRow
....
dr1.ItemArray = New Object(2) {checked, table.Name, table.Name} ' NOTE:
checked can be either "T" or "F"
....
dt.Rows.Add(dr1)
....
Dim myDataView As DataView
myDataView = New DataView(dt)
myDataView.Sort = ""
dgvTables.DataSource = myDataView
dgvTables.Refresh()
 
B

Bart Mermuys

Hi,

Eve said:
How do I bind my DataView to my DataGridView control? Here's the scenario:
because I want to make my first column a check box, I have defined a
collection for my data grid in the designer. I also had to specify the
columns for my dataTable (see code below). After I did that, there were 8
columns displayed in the grid (the first 4 from my collection (they were
empty) and the last 4 populated with the DataView data). I set the grid's
AutoGenerateColumns property to False so that I see only 4 columns (those
defined in the collection) instead of 8, but how do I get them populated
witht the DataView data? dgvTables.DataSource = myDataView doesn't do the
trick, I don't know what I'm doing wrong.

dgvTables.AutoGenerateColumns = False
Dim dt As New DataTable
dt.Columns.AddRange(New DataColumn(3) {New DataColumn("Copy"), New
DataColumn("Source Table"), New DataColumn("Destination Table"), New
DataColumn("Columns")})
...
Dim dr1 As DataRow = dt.NewRow
...
dr1.ItemArray = New Object(2) {checked, table.Name, table.Name} ' NOTE:
checked can be either "T" or "F"
...
dt.Rows.Add(dr1)
...
Dim myDataView As DataView
myDataView = New DataView(dt)
myDataView.Sort = ""
dgvTables.DataSource = myDataView
dgvTables.Refresh()

If i understand it correctly, then you need to set a DataPropertyName on
each DataGridViewColumn (using column editor or from code), which maps to
each column name in the DataTable/DataView.

HTH,
Greetings
 
G

Guest

It worked. Thank you very much!!!

Bart Mermuys said:
Hi,



If i understand it correctly, then you need to set a DataPropertyName on
each DataGridViewColumn (using column editor or from code), which maps to
each column name in the DataTable/DataView.

HTH,
Greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top