我有一個WPF程序,允許用戶在指定零件號的情況下編輯部分詳細信息的SQL數據表。用戶輸入零件號碼,然後使用表格適配器,使用行過濾器將零件號碼詳細信息顯示到數據網格。我希望能夠獲取ObservableCollection的列表並將其綁定到我的數據網格中的某個列(即具有部分類型列的部分類型的選擇列表下拉列表)。這是我的.cs:將ComboBox添加到從SQL DataTable中提取的WPF DataGrid
public MainWindow()
{
createDropDowns();
context = new RefreshAppContext();
InitializeComponent();
PartTypeComboBox.ItemsSource = partTypesList;
#if DEBUG
#endif
}
public void findButton_Click(object sender, RoutedEventArgs e)
{
var partNumber = inputBox.Text;
// Searches for part number in db.
var foundPart = context.RefreshPartTypes.Where(x => x.PartNumber == partNumber).ToList();
if (!foundPart.Any() == true)
{
MessageBox.Show("Part not found. Please try another number.");
}
else
{
// Adapter used to fill DT with info from DB based on query(ies).
adapter_PT.Fill(table_PT);
DataView dv = table_PT.DefaultView;
dv.RowFilter = "PartNumber='" + partNumber + "'";
// Fills data grid in UI.
dg.DataContext = dv;
PartTypeComboBox.ItemsSource = table_LV.DefaultView;
}
}
public void createDropDowns()
{
// Populate part styles from lookup table.
partTypesList = new ObservableCollection<string>();
adapter_LV.Fill(table_LV);
List<string> temp = table_LV.AsEnumerable().Select(x => x[2].ToString()).ToList();
foreach (var partType in temp)
{
partTypesList.Add(partType);
}
partTypesList.Add(""); // Add a blank option in case user needs to input blank cell.
}
這裏是我的.xaml:
<DataGrid x:Name="dg" ItemsSource="{Binding}" AutoGenerateColumns="True" AutoGeneratingColumn="m_grid_AutoGeneratingColumn" HorizontalAlignment="Left" Margin="28,-145,-779,0" VerticalAlignment="Top" Height="165" Width="1222" AlternatingRowBackground="LightGray" AlternationCount="2">
<DataGrid.Columns>
<DataGridComboBoxColumn x:Name="PartTypeComboBox"
Header="PartTypeTest"
DisplayMemberPath="Key"
SelectedValuePath="Id"
SelectedValueBinding="{Binding PartType}"/>
</DataGrid.Columns>
</DataGrid>
我可以得到它創建的信息正確的列表新的ComboBox列,但我不能得到那個組合框顯示(或綁定?)到從我的table_PT數據表填充的我的PartType列上。方向?謝謝!
隨着該編輯我不能再看到我已經附着在組合框中的項目列表。它只是給我一個空的下拉框。還有什麼建議?謝謝! –
@KaylaNinh是否顯示這些值? – Sajeetharan
不幸的是,它不顯示任何值 –