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
Larry BiedermanSoftware EngineerAsked:
Who is Participating?
 
Larry BiedermanSoftware EngineerAuthor Commented:
OK, will send today - thanks.
0
 
Fernando SotoRetiredCommented:
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
0
 
Larry BiedermanSoftware EngineerAuthor Commented:
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
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Fernando SotoRetiredCommented:
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

0
 
Fernando SotoRetiredCommented:
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.
0
 
Larry BiedermanSoftware EngineerAuthor Commented:
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
0
 
Fernando SotoRetiredCommented:
Ok, not a problem.
0
 
Larry BiedermanSoftware EngineerAuthor Commented:
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
0
 
Fernando SotoRetiredCommented:
In my profile in the Bio section you will see my email. You can use that to sent the files.
0
 
Larry BiedermanSoftware EngineerAuthor Commented:
He was helpful.  I just wasnt allowed to send the source code..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.