Solved

Referring to Columns by Name with Bound dgv

Posted on 2010-09-01
6
1,058 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now