【问题标题】:Adding items to a datagrid [advice needed]将项目添加到数据网格 [需要建议]
【发布时间】:2014-01-17 16:55:07
【问题描述】:

我习惯于在 vb.Net 中使用 listview 来添加您只是用作示例的项目:

ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr("Box start").ToString())

但是,我找不到通过智能感知将项目或子项目添加到数据网格的方法。我所做的所有谷歌搜索都会抛出 DataGridView,它是较新的控件,但我只对 DataGrid 感兴趣。如果有人可以提供任何建议或tuts,我将不胜感激。非常感谢

【问题讨论】:

  • 请添加标签以表明这是桌面应用程序还是网络应用程序。这将帮助您获得可以提供帮助的人的关注。
  • 对不起,这是winforms。谢谢

标签: vb.net winforms visual-studio-2010 visual-studio datagrid


【解决方案1】:

您不一定要向DataGridView 添加项目,您想向其基础数据源添加(或编辑、删除、以某种方式更改)项目DataGridView绑定

例如,如果您有一个 Person 类并将 DataGridView 绑定到 List(Of Person),它可能看起来像这样:

Dim personList As New List(Of Person)()

' elsewhere...
personList = GetPeople()
dataGridView1.DataSource = personList

此时,您管理的是personList 中的元素,而不是DataGridView 本身。所以要添加一个元素:

Dim newPerson As New Person()
' set some values on newPerson

personList.Add(newPerson)

您可能需要“刷新”DataGridView 以反映更改:

dataGridView1.Refresh()

或:

dataGridView1.DataSource = Nothing
dataGridView1.DataSource = personList

【讨论】:

  • 大卫,数据网格的控件是否相同?谢谢
  • @user1532468:成员在文档中看起来非常相似,是的:msdn.microsoft.com/en-us/library/…
  • david,据您所知,是否有任何 tuts 或综合说明作为使用 datagrid 的示例,因为我是 vb.net 的新用户。谢谢
  • @user1532468:不是我知道的副手。在这方面,您的 Google 搜索将与我的一样有效。好久没做WinForms的开发了,所以没必要去查这些教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多