我创建了一个wpf应用程序,我正在调用一个窗口来显示数据库表字段名。我目前正在使用数据网格来显示字段名称。我希望datagrid的第二列是一个组合框,其中包含" Include"和"排除"。我知道如何做datagrid并显示字段名称。我无法弄清楚如何做到这一点。我不知道从哪里开始。
到目前为止,我的XML看起来像这样:
<DataGrid HeadersVisibility="Column" IsReadOnly="True" ItemsSource="{Binding}" Name="dtGrid" Loaded="GridLoaded" Width="283" HorizontalAlignment="Left" VerticalAlignment="Top" Height="365" Margin="54,74,0,0" BorderThickness="1" BorderBrush="Black">
<DataGrid.Columns>
<DataGridTextColumn x:Name="FieldName" Header="Field Name" Width="180" />
<DataGridComboBoxColumn x:Name="ComboBoxColumn" Width="83" Header="Include Field" SelectedItemBinding="{Binding strFieldInclude}" />
</DataGrid.Columns>
</DataGrid>
我的代码尝试如下:
private void DisplayFields(string[] stringFieldNames)
{
int intDisplayCnt, intTotalRowSize = 0;
string strData, strFieldSize = "";
DataTable dt = new DataTable();
dt.Columns.Add("FieldName", typeof(string));
//DataTable dt = (DataTable)dtGrid.ItemsSource;
for (intDisplayCnt = 0; intDisplayCnt < stringFieldNames.Length; intDisplayCnt++)
{
dt.Rows.Add(stringFieldNames[intDisplayCnt]);
}
dtGrid.ItemsSource = dt.DefaultView;
strFieldIncludeList = new ObservableCollection<string>() { "Include", "Exclude" };
ComboBoxColumn.ItemsSource = strFieldIncludeList;
}