Avatar of Larry Biederman
Larry Biederman
Flag for United States of America asked on

vb.net datagridview will not identify its column names

Dear Experts,

I have a datagridview in a  2013 vb.net winforms app.
I can get the value of a cell when identify it by column number but not by column name.

This works:
MyValue = DataGridView1.SelectedRows(0).Cells(18).Value

The following two samples do NOT work:

example one:
        For Each col In DataGridView1.Columns
            Debug.Print(col.Name)
        Next

example two:
ecell = DataGridView1.CurrentRow.Index
 DataGridView1.CurrentCell = DataGridView1.Item("d200", ecell)
 MessageBox.Show(DataGridView1.CurrentCell.Value)


In first example it goes to 'next', doesn't loop, and never prints a column name.

In second example, it produces the error message:  "column named d200 cannot be found"
I've tried this on several of its columns and get same error.  The column name in database and column caption are identical.

Its as if it does not know its own column names!
Suggestions appreciated.
Thanks, Larry
.NET Programming

Avatar of undefined
Last Comment
Larry Biederman

8/22/2022 - Mon
Fernando Soto

Hi Larry;

Can you post the code which shows how you get the data from the database and how you populate the DataGridView.

Thanks;
Fernando
Larry Biederman

ASKER
Hi Fernando,

I have attached a zip file of the form files where the datagridview issue occurs.
The code lines where I can get the cell value by column #, but not by column name are lines 133-139

Thanks much!!

-Larry
DatagridviewIssue-code.zip
Fernando Soto

Hi Larry;

I was not able to reproduce your issues. I have used your code with some minor changes for example database and it worked as expected. If you would like and if you are able to zip your complete project with a sample database so that I would not need to change anything I will try and see if I can find your issue. If you can do this try using a web site line Microsoft OneDrive, it's a free service, because EE will have issues if you try loading a complete project.

Dim DataPath As String = "C:\Working Directory\MSDN Forum\WindowsApplication21\WindowsApplication21\"
Dim sql As String
Dim connectionString As String = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = " & DataPath & "Northwind 2007.accdb"
sql = "SELECT * FROM Employees"
Dim connection As New OleDbConnection(connectionString)
Dim dataadapter As New OleDbDataAdapter(Sql, connection)
Dim ds As New DataSet()
connection.Open()

dataadapter.Fill(ds, "Table_1A")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Table_1A"

connection.Close()
ds.Dispose()
dataadapter.Dispose()


For Each col In DataGridView1.Columns
    Debug.Print(col.Name)
Next

Dim ecell As Integer = DataGridView1.CurrentRow.Index
Dim cell As DataGridViewCell = DataGridView1.Item("First Name", ecell)
MessageBox.Show(cell.Value)

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Fernando Soto

Hi Larry;

Before you do what I asked for in my last post have you tried cleaning the solution by doing the following. From the main menu of VS click on Build --> Clean Solution and then recompile and try it then.
Larry Biederman

ASKER
Hi Fernando,
I tried cleaning and no help.
I cannot upload entire project, as it belongs to client and is confidential.

Checking on feasibility of removing splitting off portion of program.
Give me a day or two.

Thanks, Larry
Fernando Soto

Ok, not a problem.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Larry Biederman

ASKER
Fernando,
Can you either connect to my computer and have a look, or can I email the files to you privately?
The app is big (3 years in the making) and my cient doesn't want competitors to see it.

Any suggestions?
Thanks, Larry
Fernando Soto

In my profile in the Bio section you will see my email. You can use that to sent the files.
ASKER CERTIFIED SOLUTION
Larry Biederman

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Larry Biederman

ASKER
He was helpful.  I just wasnt allowed to send the source code..
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy