error1692
error1692

Reputation: 23

Datagridviewrow using column name instead of index

Here is the code I'm using below

Dim lname As New DataGridViewColumn

lname.Name = "LastName"
lname.DataPropertyName = "LastName"
DataGridView1.Columns.Add(lname)

Dim itrow = New DataGridViewRow    
itrow.CreateCells(DataGridView1)                
itrow.Cells(0).Value = empcoll.Item(i).LastName <<works
itrow.Cells("LastName").Value = empcoll.Item(i).LastName <<error column name can't be found

I'm having trouble with using datagridviewrow.cells("column name"). Can anyone enlighten or help me?

Upvotes: 2

Views: 6199

Answers (1)

Nianios
Nianios

Reputation: 1426

Try this (I slightly change something to try the code if it works):

  Dim lname As New DataGridViewColumn
  lname.Name = "LastName"
  lname.DataPropertyName = "LastName"
  lname.CellTemplate = New DataGridViewTextBoxCell 'I had to add this to my code

  DataGridView1.Columns.Add(lname)

  Dim itrow As New DataGridViewRow
  itrow = DataGridView1.Rows(DataGridView1.Rows.Add())
  itrow.Cells("LastName").Value = "Give here your Value"

Upvotes: 1

Related Questions