?
Solved

Referring to Columns by Name with Bound dgv

Posted on 2010-09-01
6
Medium Priority
?
1,104 Views
Last Modified: 2012-05-10
If I bind a table to a dgv using the following ("manual") process, I can reference each column by its' assigned name in the table (dgv.rows(i).cells("Address").value).

                str_Select = "SELECT * " & _
                             "FROM tbl_SE_TS_Groups"

                Dim con_SE As New OleDbConnection(str_OleDB_Connection_Provider_MDB)

                con_SE.Open()

                cmd = New OleDb.OleDbCommand(str_Select, con_SE)

                da_Groups = New OleDbDataAdapter(cmd)

                con_SE.Close()

                da_Groups.Fill(dt_Groups)
                dgv_Groups.DataSource = da_Groups
                dgv_Groups_Filter.Table = dt_Groups

Howver, if I bind a table to a dgv using a typed Dataset (using "Data Sources" in Visual Studio and dragging a table onto a form), I cannot reference each column by its' assigned name - I get "Value cannot be null. Parameter name: dataGridViewColumn".  If I reference the column by it's position (...cells(1).value), everybody's happy.

Why can't I reference a column by its' name when I bind with typed Dataset, but I can when I "manually" bind?

Thank you in advance for your help!
0
Comment
Question by:CIW_George
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33580758
By default, if binding with a dataset, the column names are a combination of your table column names and "DataGridViewTextBoxColumn", for example, if your table column name is "Company", your can reference the cell value in datagridview by the column name of "CompanyDataGridViewTextBoxColumn".

0
 

Author Comment

by:CIW_George
ID: 33581510
I have a column named "Path_ID" in my table.  If I try:

    dgv_SETS_Groups.Rows(i).Cells("Path_IDDataGridViewTextBoxColumn").Value

I get the following error:

    Column named Path_IDDataGridViewTextBoxColumn cannot be found. Parameter name: columnName

Thoughts?
0
 

Author Comment

by:CIW_George
ID: 33581579
If I do:

        For Each col In dgv_SETS_Groups.Columns
            Console.WriteLine(col.ToString)
        Next

I get the following output:

DataGridViewTextBoxColumn { Name=DataGridViewTextBoxColumn1, Index=0 }
DataGridViewTextBoxColumn { Name=DataGridViewTextBoxColumn2, Index=1 }
DataGridViewTextBoxColumn { Name=DataGridViewTextBoxColumn3, Index=2 }

So, part of the name is "DataGridViewTextBox", but the suffix is just incrementing the column number, without any reference to the column name in the table.

Just thought I'd throw that into the information pool.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Accepted Solution

by:
Zhaolai earned 2000 total points
ID: 33583508
Unfortunately, that is the case. Or maybe it's a bug in VB.NET design. But you can still fix it in design time. After dragging the table to the form, click on the little triangle located on the upper right corner of the datagridview control, then click on Edit Columns..., you can change the column names there, see screen shot below:



Screenshot.PNG
0
 

Author Comment

by:CIW_George
ID: 33583681
Perhaps not so much a "bug" as a shortcoming or oversight.  Either way, it requires a secondary operation before columns can be referenced by their names.

Thank you for your input.  At this point, I consider this subject closed - unless someone else has cracked the code in providing named access to columns without manual intervention.
0
 

Author Closing Comment

by:CIW_George
ID: 33583684
Don't know if the solution was accurate, in so much as there weren't any other suggestions, but it makes sense.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question