我需要根据所选城市和所选类型名称填充组合框1。
我用以下内容填充城市组合框:
private void FillCityTable()
{
dtCity.Columns.Add("CityID", typeof(int));//ID will be a number
dtCity.Columns.Add("CName");//defaults typof is string
dtCity.Rows.Add(1, "Regina");
dtCity.Rows.Add(2, "Saskatoon");
dtCity.Rows.Add(3, "Winnipeg");
}
private void Form1_Load(object sender, EventArgs e)
{
FillCityTable();
FillTypeTable();
FillData();
cboCity.DataSource = dtCity;
cboCity.DisplayMember = "CName";
}
我根据所选城市填充“类型”组合框
private void FillTypeTable()
{
dtType.Columns.Add("CityID", typeof(int));//ID will be a number
dtType.Columns.Add("TypeName");//defaults typof is string
dtType.Rows.Add(1, "H1");
dtType.Rows.Add(1, "H2");
dtType.Rows.Add(1, "H3");
dtType.Rows.Add(2, "H2");
dtType.Rows.Add(2, "H1");
dtType.Rows.Add(2, "H3");
dtType.Rows.Add(3, "H2");
}
private void cboCity_SelectedIndexChanged(object sender, EventArgs e)
{
dtSpecificType = dtType.Select("CityID =" + dtCity.Rows[cboCity.SelectedIndex]["CityID"]).CopyToDataTable();
cboType.DataSource = dtSpecificType;
cboType.DisplayMember = "TypeName";
}
主要数据表来自csv文件。 我需要根据所选城市和所选类型名称使用用户名填充组合框1。 到目前为止,我只能根据所选城市填充用户名。我怎样才能得到第二个标准?
private void cboType_SelectedIndexChanged(object sender, EventArgs e)
{
dtSpecificUserNames = dtUserName.Select("CityID =" + dtSpecificType.Rows[cboType.SelectedIndex]["CityID"]).CopyToDataTable();
combobox1.DataSource = dtSpecificUserNames;
combobox1.DisplayMember = "Username";
}
CSV:
CityID,TypeName,Username,Password,MachineID,Picture_File
1,H1,User1,password1,MachineID 1,Airplane
1,H1,User3,password3,MachineID 3,Bread
1,H1,User8,password8,MachineID 8,Boat
1,H2,User11,password11,MachineID 11,Boat
1,H3,User2,password2,MachineID 2,Boat
1,H3,User10,password10,MachineID 10,Airplane
2,H1,User6,password6,MachineID 6,Bread
2,H2,User4,password4,MachineID 4,Airplane
2,H2,User7,password7,MachineID 7,Airplane
2,H3,User5,password5,MachineID 5,Boat
3,H2,User9,password9,MachineID 9,Bread
需要类型标准以及城市标准。
制作
DataTable
的副本,将其指定为 cboType 的 DataSource
,然后在副本上设置 DefaultView.RowFilter
。例如:
public static void TestComboBox() {
DataTable table = GetDataTable();
ComboBox combo1 = new ComboBox();
ComboBox combo2 = new ComboBox();
ComboBox combo3 = new ComboBox();
combo1.DisplayMember = "CityId";
combo1.ValueMember = "CityId";
combo1.DataSource = table;
combo2.DisplayMember = "TypeName";
combo2.ValueMember = "TypeName";
combo2.DataSource = table;
DataTable table2 = table.DefaultView.ToTable(); // make a copy
combo3.DisplayMember = "Username";
combo3.DataSource = table2;
EventHandler evt = (o, e) => {
String cityId = (String) combo1.SelectedValue;
String typeName = (String) combo2.SelectedValue;
table2.DefaultView.RowFilter = String.Format("CityId = '{0}' and TypeName = '{1}'", cityId, typeName);
};
combo1.SelectedValueChanged += evt;
combo2.SelectedValueChanged += evt;
FlowLayoutPanel panel = new FlowLayoutPanel();
panel.Controls.AddRange(new [] { combo1, combo2, combo3 });
panel.Dock = DockStyle.Fill;
Form f = new Form();
f.Controls.Add(panel);
Application.Run(f);
}
public static DataTable GetDataTable() {
String data =
@"CityID,TypeName,Username,Password,MachineID,Picture_File
1,H1,User1,password1,MachineID 1,Airplane
1,H1,User3,password3,MachineID 3,Bread
1,H1,User8,password8,MachineID 8,Boat
1,H2,User11,password11,MachineID 11,Boat
1,H3,User2,password2,MachineID 2,Boat
1,H3,User10,password10,MachineID 10,Airplane
2,H1,User6,password6,MachineID 6,Bread
2,H2,User4,password4,MachineID 4,Airplane
2,H2,User7,password7,MachineID 7,Airplane
2,H3,User5,password5,MachineID 5,Boat
3,H2,User9,password9,MachineID 9,Bread";
String[] lines = data.SplitAndTrim('\r', '\n');
String[] header = lines[0].SplitAndTrim(',');
DataTable table = new DataTable();
for (int i = 0; i < header.Length; i++)
table.Columns.Add(header[i]);
for (int i = 1; i < lines.Length; i++) {
table.Rows.Add(lines[i].SplitAndTrim(','));
}
return table;
}