【问题标题】:DataGrid the method DataSource not foundDataGrid找不到方法DataSource
【发布时间】:2012-02-12 17:47:46
【问题描述】:

我正在尝试构建一个 DataGrid - 不是 DataGridView,因为它似乎在 Visual Studio 2010 上不可用。

我想动态添加行(我想我可以这样做)但为了显示结果我需要将数据分配给 DataGrid,所有示例都说我应该使用 DataSource 但 Visual Studio 坚持认为这是不可用的.要编码,我发现其他地方是这样的。

private void BindToDataView(System.Windows.Controls.DataGrid myGrid)
{
    // Create a DataView using the DataTable.
    DataTable myTable = new DataTable("Suppliers");
    // Insert code to create and populate columns.
    DataView myDataView = new DataView(myTable);
    myGrid.DataSource = myDataView;
}

我做错了什么?

【问题讨论】:

  • 您发布了有效的代码(在其他地方),然后希望我们回答您未发布的代码中有什么问题?
  • 代码无法在我使用的 Visual Studio 2010 上编译。它给出了一个错误,说 DataGrid 不包含定义 DataSource。
  • 从指定 GUI 开始:WinForms.、WPF 或 ASP.NET?
  • 但您的代码主体似乎针对 WinForms。 DataGrid 有一个 ItemsSsource 属性。

标签: c# wpf datagrid datasource


【解决方案1】:

您传入 WPF DataGrid (System.Windows.Controls.DataGrid) 并使用适用于 WinForms DataGrid (System.Windows.Forms.DataGrid) 的代码。

WPF 数据网格使用ItemsSource
WinForms DataGrids 使用DataSource

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
  • 2010-10-09
相关资源
最近更新 更多