我有一个由 a 支持的 WPF (.NET 4) ,DataGrid
它的列之一是 a 。一切都对我有用,但我必须创建一个新类来保存 DGCBC 的可能值的简单 [ // whatever I can try] 的内容,当看起来应该不那么复杂时方法。这是我的 XAML:DataView
DataTable
DataGridComboBoxColumn
ArrayList
List<string>
IEnumerable
<Window x:Class="MASTableMaint.TruckloadLimitsEdit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Edit Truckload Limits" Height="600" Width="460" MinWidth="300" MinHeight="300" WindowStartupLocation="CenterOwner" >
<DockPanel LastChildFill="True">
<DataGrid Name="TruckloadGrid" ItemsSource="{Binding}" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserSortColumns="False"
AutoGenerateColumns="False" RowHeaderWidth="20">
<DataGrid.Columns>
<DataGridComboBoxColumn x:Name="companyIdColumn" Header="Company" SelectedValueBinding="{Binding Path=CompanyID}"
DisplayMemberPath="TheCompanyID" SelectedValuePath="TheCompanyID"/>
...
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Window>
而我背后的代码:
namespace MASTableMaint
{
public partial class TruckloadLimitsEdit : Window
{
private DataTable truckloadDT = new DataTable();
private SqlDataAdapter truckloadDA;
private SqlConnection sqlconn;
public TruckloadLimitsEdit(List<string> massCompanies)
{
sqlconn = new SqlConnection(Constants.SqlConnectionStr);
if (!DbUtils.OpenDbConn(sqlconn))
{
sqlconn.Dispose();
return;
}
string query = "SELECT * FROM tTruckload ORDER BY CompanyID";
truckloadDA = new SqlDataAdapter(query, sqlconn);
truckloadDA.Fill(truckloadDT);
SqlCommandBuilder builder = new SqlCommandBuilder(truckloadDA);
InitializeComponent();
DataView truckloadDV = new DataView(truckloadDT, null, null, DataViewRowState.CurrentRows);
TruckloadGrid.ItemsSource = truckloadDV;
ObservableCollection<MassCompany> ocMassCompanies = new ObservableCollection<MassCompany>();
foreach (string company in massCompanies)
{
ocMasCompanies.Add(new MassCompany(company));
}
companyIdColumn.ItemsSource = ocMassCompanies;
}
...
}
public class MassCompany
{
public MassCompany(string company)
{
TheCompanyID = company;
}
public string TheCompanyID { get; set; }
}
}