Here is my code
Dim dt As DataTable = New DataTable("Intervenant")
For Each column As DataControlField In grdvIntervenants.Columns
dt.Columns.Add(New DataColumn(column.HeaderText))
Next
Dim dr As DataRow
For Each row As GridViewRow In grdvIntervenants.Rows
dr = dt.NewRow()
dr.Item(0) = row.Cells(0)
dr.Item(1) = row.Cells(1)
dr.Item(2) = row.Cells(2)
dr.Item(3) = row.Cells(3)
dr.Item(4) = row.Cells(4)
dr.Item(5) = row.Cells(5)
dt.Rows.Add(dr)
Next
'add new intervenant
dr = dt.NewRow()
dr.Item(0) = Nom
dr.Item(1) = Prenom
dr.Item(2) = Courriel
dr.Item(3) = False
dr.Item(4) = True
dr.Item(5) = IdIntervenant
dt.Rows.Add(dr)
grdvIntervenants.DataSource = dt
grdvIntervenants.DataBind()
I am developing in ASP.NET using VB.NET as background code. I am on vs2008 with windows 7 enterprise edition (if that info is of any help)
I have just restarted doing VB.Net and my first time using a gridview without a DataBase, so maybe I am missing something. Basically, I am trying to add a DataRow (dr) to a DataTable (dt) and then put said DataTable into a Gridview (grdvIntervenants).
My first step is to take the gridview and put all its data into a DataTable. Then i create a DataRow(dr) that I populate with variables and I add said DataRow to the DataTable. After that, I put the DataTable as the DataSource and bind the GridView.
The problem is that my GridView shows the rows but they are empty.
While debugging, I found out that this error was beside all the DataRow.Item:
“In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user” Seraching for it on the net, I found out it was an issue that sometimes happens to vb users but I could nto find any solution for my problem.
I did not totally understand the error, so i tried many different ways of adding the data into the DataRow but to no avail. I also tried different way of adding the column thinking the index problem might be coming from there, but again, the problem persisted. Now, something I found weird was that the Item property of my DataRow gives the quoted error, but I see the values into the ItemArray of the DataRow.
I also found out that the same error appears beside the columns property into the dataTable.
When I try to access the data from the DataSource in my rowDatabound method using the DataBinder, it gives me null results…
So my question is: what I am missing?
Your issue is most likely that you are using the HeaderText as the column name for the data table, but this is probably not correct.
You should probably be testing to see if each column is a BoundField and, if so, use the BoundField.DataField as the DataTable’s column name.
For example: