我有一个代码生成的数据表,通过代码添加到DataGrid:
// out GMBD); <<-- get results from api
DataTable table = new DataTable();
if (y != 0)
{
string err = apicall.GetErrorText(y);
MessageBox.Show(err);
}
else
{
// Get max/ Add columns and columns name
for (int column = 0; column < GMBD.ColumnCount; column++)
{
table.Columns.Add(GMBD.ColumnName[column],GMBD[0,column].ValueAsObject.GetType());
}
// Add rows.
for (int row = 0; row < GMBD.RowCount; row++)
{
table.Rows.Add(GMBD.GetRowAsList(row).Select(x => x.ValueAsObject).ToArray());
}
tabla.DataContext = table.DefaultView;
apicall.Dispose();
}
private void setnew_Click(object sender, RoutedEventArgs e)
{
}
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
//if ((string)e.Column.Header== "STATE")
//{
// DataGridComboBoxColumn cboclm = new DataGridComboBoxColumn();
// cboclm.Header = "zzzz"; ///"STATE";
// cboclm.ItemsSource = Enum.GetValues(typeof(STATEvariables));
// //cboclm.SetValue(cboclm,e.Column.));
// // cboclm.SelectedValueBinding = new Binding("STATE");
// e.Column = cboclm;
//}
}
列类型是在另一个类中预定义的。 XAML中的DataGrid:
<DataGrid x:Name="tabla"
HorizontalAlignment="Left" Margin="10,46,0,0"
VerticalAlignment="Top" Height="543" Width="1162"
EnableRowVirtualization="false" EnableColumnVirtualization="false"
CanUserAddRows="False" CanUserReorderColumns="False"
AutoGenerateColumns="True" IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding Path=id}"
AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"
ItemsSource="{Binding}" Grid.ColumnSpan="3"/>
尝试了AutoGeneratingColumn,但它不好,因为它在获得结果之前被调用。结果列是未知的,因为我不希望显示所有20多列,只有输入所请求的列。 问题是ComboboxColumn组合框不显示任何结果值,但仍包含预定义的值。
是否有方法在生成的组合框列中显示结果值?