Solved

Referring to Columns by Name with Bound dgv

Posted on 2010-09-01
6
1,068 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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 …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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