DataGridView Bug or Design Flaw?

J

jp2msft

I use unbound DataGridView controls in my form (simply because I have to
manually message my data before populating the DataGridView with it).

Say I clear all the info from the DataGridView:

DataGridView1.Columns.Clear()

Now I manually add new columns to fill:

DataGridView1.Columns.Add("Date_Time", "Date_Time")
DataGridView1.Columns.Add("Station_ID", "Station_ID")
DataGridView1.Columns.Add("Operator", "Operator")

Now try to fill these columns using their Column Names used above:

Dim dgRow As New DataGridViewRow()
dgRow.CreateCells(DataGridView1)
dgRow.Cells["Date_Time"].Value = DateTime.Now().ToString()
dgRow.Cells["Station_ID"].Value = "Test Station 1"
dgRow.Cells["Operator"].Value = Form1.EmployeeNameTextBox.Text
DataGridView1.Rows.Add(dgRow);

Here is the error:
ArgumentException was unhandled
Column named Date_Time cannot be found.
Parameter name: columnName

Is there a fix for this?

Microsoft has overloaded this method to accept an integer Index or a string
columnName, but the columnName version does not appear to work. My
DataGridColumns are added Programmatically, so I don't always know what the
Index value of the DataGridColumns are going to be.

Is there a way to get the DataGridColumn's Index using the columnName? Is
this the way to get around this problem?

I have also coded a different version using CellTemplates, but it threw the
same error.
 
J

jp2msft

I don't immediately see how this is going to solve this particular delima.

When using SetValues(ByVal ParamArray values As Object()):

How do I ensure that "06/05/2008" is going to be mapped to the "Date_Time"
column if I do not know the index number of that column?

In the DataGridView, there is the option to reorder the Columns. After they
have been reordered, how would you go about constructing your ParamArray of
values?
 
R

Rich P

The datagridview works best/easiest to manipulate when the datasource is
based on a dataTable. So instead of adding columns to the datagridview
you can add columns to the dataTable and massage your data there:

Dim dt As DataTable
dim col1 As DataColumn
...
dt.oolumns.Add(col1)
...
datagridview1.datasource = dt '--this automatically create all the
columns from the dataTable

For information on creating dataTables from code and adding data there
is good documentation in the VS2005 Help system.

Rich
 
J

jp2msft

Hi Rich,

That's a neat way to do it! And it simplifies life quite a bit.

Your approach solves the programming task, but does not answer the question:
Is there a bug or design flaw with accessing Cells by the column name?
 
R

Rich P

No. There is no design flaw. The Datagridview control is the most
powerful of the standard issue datagrid controls currently around
(standard issue meaning they come stock with VS2005). It takes a little
while to get used to programming the datagridview control. There are
hundreds of datagridview events to work with for manipulating data
within the datagridview. Just remember that when you are manipulating
data from the datagridview - you have to actually manipulate the data in
the underlying dataTable. Think of the datagridview control as a
Subform control in datasheet view - that really is what a datagridview
is. So if you edit a cell in the datagridview - you have to go to an
event under the datagridview - like CellValueChanged or Cell_Leaving
(whatever that event is called) and edit your dataTable in order to make
the edit stick. For these kind of events you have an object named "e"
(for event) and this object has various properties like e.RowIndex,
e.ColumnIndex.

So to edit a dataTable you could do something like this in one of the
events

dt.Rows(e.RowIndex)("somefield") =
datagridview1.Rows(e.rowIndex).Cells(e.ColumnIndex).Value

Rich
 
J

jp2msft

Thanks for the detailed explanation, Rich ...and the easier DataTable
assignment solution! :) I appreciated them both (the later much more after
seeing the detailed explanation below).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top