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.C olumns.Clear()
Now I manually add new columns to fill:
DataGridView1.C olumns.Add("Dat e_Time", "Date_Time" )
DataGridView1.C olumns.Add("Sta tion_ID", "Station_ID ")
DataGridView1.C olumns.Add("Ope rator", "Operator")
Now try to fill these columns using their Column Names used above:
Dim dgRow As New DataGridViewRow ()
dgRow.CreateCel ls(DataGridView 1)
dgRow.Cells["Date_Time"].Value = DateTime.Now(). ToString()
dgRow.Cells["Station_ID "].Value = "Test Station 1"
dgRow.Cells["Operator"].Value = Form1.EmployeeN ameTextBox.Text
DataGridView1.R ows.Add(dgRow);
Here is the error:
ArgumentExcepti on 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 Programmaticall y, 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.
manually message my data before populating the DataGridView with it).
Say I clear all the info from the DataGridView:
DataGridView1.C olumns.Clear()
Now I manually add new columns to fill:
DataGridView1.C olumns.Add("Dat e_Time", "Date_Time" )
DataGridView1.C olumns.Add("Sta tion_ID", "Station_ID ")
DataGridView1.C olumns.Add("Ope rator", "Operator")
Now try to fill these columns using their Column Names used above:
Dim dgRow As New DataGridViewRow ()
dgRow.CreateCel ls(DataGridView 1)
dgRow.Cells["Date_Time"].Value = DateTime.Now(). ToString()
dgRow.Cells["Station_ID "].Value = "Test Station 1"
dgRow.Cells["Operator"].Value = Form1.EmployeeN ameTextBox.Text
DataGridView1.R ows.Add(dgRow);
Here is the error:
ArgumentExcepti on 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 Programmaticall y, 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.
Comment