Datagrid - 数据集/数据表绑定不起作用

时间:2016-02-11 11:42:31

标签: c# wpf datagrid datatable dataset

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

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

enter image description here

--- --- ADD 以下是我如何形成数据集:

    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);
        }
      }

1 个答案:

答案 0 :(得分:1)

WPF中的DataGrid只显示一个表,并告诉她绑定到多个表的DataSet,因为有两个选项:
1-将一个表绑定到ItemsSource,如下所示:

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

2-绑定所有dataSet,然后告诉datagrid绑定你的特定表,如下所示:

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