2

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?

error1692
  • 23
  • 1
  • 6

1 Answers1

1

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"
Nianios
  • 1,391
  • 3
  • 20
  • 45
  • thanks habouf! your code works. So I really have to add .cellTemplate for every column that I will add. Thanks again! – error1692 Nov 27 '12 at 15:07
  • You can also create do "Dim lname as New DataGridViewTextBoxColumn" which takes care of that for you. – WozzeC Nov 27 '12 at 15:14
  • WozzeC- sure I'll try that one as well thanks! one question is there any alternative for itrow=datagridview1.rows(datagridview1.rows.add())? – error1692 Nov 27 '12 at 15:22
  • Sure , it depends what you are trying to achieve. – Nianios Nov 27 '12 at 15:46