Solved

Referring to Columns by Name with Bound dgv

Posted on 2010-09-01
6
1,090 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 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

Technology Partners: 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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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