469,913 Members | 2,129 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

DataGridView Bug or Design Flaw?

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.
Jul 11 '08 #1
4 2212
dgdRow.SetValues seems to work...

This is where looking at the DataGridView code could be helpfull. I assume
there is some method that triggers the actual creation.

Is this fix enough or do you need a closer look ?

--
Patrice

"jp2msft" <jp*****@discussions.microsoft.coma écrit dans le message de
groupe de discussion : CE**********************************@microsoft.com...
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.

Jul 11 '08 #2
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?

"Patrice" wrote:
dgdRow.SetValues seems to work...

This is where looking at the DataGridView code could be helpfull. I assume
there is some method that triggers the actual creation.

Is this fix enough or do you need a closer look ?

--
Patrice
Jul 11 '08 #3
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

*** Sent via Developersdex http://www.developersdex.com ***
Jul 11 '08 #4
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?

"Rich P" wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***
Jul 11 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by ma740988 | last post: by
4 posts views Thread by Aaron Smith | last post: by
2 posts views Thread by Marc Solé | last post: by
3 posts views Thread by Johnny E. Jensen | last post: by
7 posts views Thread by =?Utf-8?B?TG9zdEluTUQ=?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.