Datagrid - Dataset/Datatable 绑定不起作用

Datagrid - Dataset/Datatable binding not working

我有一个由 4 个数据表组成的数据集,想将它绑定到我的数据网格,但是当我查看数据网格时它是空的。 所以我做的是:

 DataSet dtsResults = myPcd.GetDimensionsDataSet();
 dtgResults.DataContext = dtsResults.Tables;

---添加--- 这是我形成数据集的方式:

    obcPcdlrnDimensionCommands.Clear();
    int maxNumCommands = (pcdPartProgram.Commands.Count < MAX_NUM_COMMANDS) ? pcdPartProgram.Commands.Count : MAX_NUM_COMMANDS;
    PCDLRN.Commands pcdCommands = pcdPartProgram.Commands;
    //2. ciclo su tutti i comandi
    int numTables = dtsDimensions.Tables.Count + 1;
    dtsDimensions.Tables.Add("DATA_TABLE_" + numTables);
    dtsDimensions.Tables[0].Columns.Add();
    dtsDimensions.Tables[0].Columns.Add();
    dtsDimensions.Tables[0].Columns.Add();
    dtsDimensions.Tables[0].Columns.Add();
    String CurName = string.Empty;
    for (int iCommand = 1; iCommand < maxNumCommands; iCommand++)
    {
      PCDLRN.Command pcdCommand = pcdCommands.Item(iCommand);
      if (pcdCommand.IsDimension && pcdCommand.Marked)
      {
        if (pcdCommand.Type == PCDLRN.OBTYPE.DIMENSION_START_LOCATION || pcdCommand.Type == PCDLRN.OBTYPE.DIMENSION_TRUE_START_POSITION)
        {
          CurName = pcdCommand.ID;
        }
        else
        {
          if (pcdCommand.ID != string.Empty)
            CurName = pcdCommand.ID;

          double approxMeasured = Math.Round(pcdCommand.DimensionCommand.Measured, 9);
          dtsDimensions.Tables[0].Rows.Add(CurName, delenda_PART_NAME, pcdCommand.DimensionCommand.AxisLetter, approxMeasured);
        }
      }

WPF 中的 DataGrid 仅显示一个 table,您告诉她绑定到多个 table 的 DataSet,因此您有两个选择:
1- 像这样将一个 table 绑定到 ItemsSource:

yourDatagrid.ItemsSource = yourDataSet.Tables[0]; \or yourDataSet.Tables["TableName"]

2- 绑定所有数据集,然后告诉您数据网格绑定您的特定 table,如下所示:

<DataGrid ItemsSource="{Binding TableName}"/>