Solved

Referring to Columns by Name with Bound dgv

Posted on 2010-09-01
6
1,079 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get size of each directory on each mapped drive 5 36
Disable extension 8 48
Expression Evaluater 3 38
VB.Net Determine if a mapped network drive exists 2 30
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…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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