Datagridviewrow使用列名而不是索引

时间:2012-11-27 11:52:35

标签: vb.net datagridview

以下是我正在使用的代码

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

我在使用datagridviewrow.cells("column name")时遇到问题。任何人都可以启发或帮助我吗?

1 个答案:

答案 0 :(得分:0)

试试这个(如果有效的话,我会稍微改变一下来尝试代码):

  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"