我要做的是将 DataGridView 绑定到我的模型,并在每行中有两个具有不同值的无界列;这些列包含组合框。
这是一种工作:有界值正确显示,并且 ComboBoxes 填充了项目和 item.ToString() 给出了我怀疑在 ComboBoxes 中显示的值以供选择......但它们没有显示。我怀疑我错误地使用了 DisplayMember,但我不明白为什么......
这就是我填充每个 ComboBox 的方式:
DataGridViewComboBoxCell theCell = (DataGridViewComboBoxCell)dataGridView1.Rows[tableRow.Index].Cells["SendSets"];
BindingSource theSource = new BindingSource(customerRow, "SendSets");
theCell.ValueMember = "Value";
theCell.DisplayMember = "Text";
theCell.DataSource = theSource;
同样的问题是当我使用简单的 BindingList 时。填充组合框的类:
public class ComboBoxItem
{
public String Text { get; set; }
public object Value { get; set; }
public ComboBoxItem(String text, object value)
{
this.Text = text;
this.Value = value;
}
public override string ToString()
{
return Text;
}
}
[编辑] 的输出
private void Customers_Load(object sender, EventArgs e)
{
RefreshTable();
foreach (DataGridViewRow tableRow in dataGridView1.Rows)
{
DataGridViewComboBoxCell theCell = (DataGridViewComboBoxCell)tableRow.Cells["SendSets"];
Debug.Write(theCell.Items.Count);
}
foreach (DataGridViewRow tableRow in dataGridView1.Rows)
{
DataGridViewComboBoxCell theCell = (DataGridViewComboBoxCell)tableRow.Cells["ReceivedSets"];
Debug.Write(theCell.Items.Count);
}
}
是
200100
所以我应该有一个带有两个选项的 ComboBox 和一个带有一个选项的 ComboBox。[/编辑]
整个人口过程是这样的:
public void RefreshTable()
{
trader trader = EntityManager.GetTraderById(parent.GetTrader().id);
BindingList<Row> rows = new BindingList<Row>();
foreach (customer customer in trader.customers.OrderBy(c => c.id))
{
rows.Add(new Row(customer));
}
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = rows;
foreach (DataGridViewRow tableRow in dataGridView1.Rows)
{
customer customer = EntityManager.GetCustomerByCompanyName((String)tableRow.Cells["companyDataGridViewTextBoxColumn"].Value);
Row customerRow = rows.Single(r => r.GetCustomerId().Equals(customer.id));
if (customerRow.SendSets.Count > 0)
{
DataGridViewComboBoxCell theCell = (DataGridViewComboBoxCell)dataGridView1.Rows[tableRow.Index].Cells["SendSets"];
BindingSource theSource = new BindingSource(customerRow, "SendSets");
theCell.ValueMember = "Value";
theCell.DisplayMember = "Text";
theCell.DataSource = theSource;
}
if (customerRow.ReceivedSets.Count > 0)
{
DataGridViewComboBoxCell theCell = (DataGridViewComboBoxCell)dataGridView1.Rows[tableRow.Index].Cells["ReceivedSets"];
BindingSource theSource = new BindingSource(customerRow, "ReceivedSets");
theCell.DataSource = theSource;
theCell.ValueMember = "Value";
theCell.DisplayMember = "Text";
}
}
}
填充 DataGrid 的类:
public class Row
{
private customer customer;
public String Town { get { return customer.town; } set { customer.town = value; } }
public String Names
{
get
{
return customer.last_name + ", " + customer.first_name;
}
set
{
string s = value;
int a = s.LastIndexOf(',');
int b = s.LastIndexOf(' ');
string l = a < 0 ? s : s.Substring(0, a),
r = a < 0 ? "" : s.Substring(b + 1);
customer.last_name = l;
customer.first_name = r;
}
}
public String Company { get { return customer.company_name; } set { customer.company_name = value; } }
public String EMail { get { return customer.e_mail; } set { customer.e_mail = value; } }
public BindingList<ComboBoxItem> SendSets { get; set; }
public BindingList<ComboBoxItem> ReceivedSets { get; set; }
public int GetCustomerId()
{
return customer.id;
}
public Row(customer customer)
{
this.customer = customer;
this.Town = customer.town;
this.Names = customer.last_name + ", " + customer.first_name;
this.Company = customer.company_name;
IEnumerable<customers_question_set> sendQuestionSets = customer.customers_question_sets.Where<customers_question_set>(
x => x.send != null);
BindingList<ComboBoxItem> toSendSets = new BindingList<ComboBoxItem>();
foreach (customers_question_set customerQuestionSet in sendQuestionSets)
{
toSendSets.Add(new ComboBoxItem(customerQuestionSet.send.ToString() + " (" + customerQuestionSet.question_set.name + ")", customerQuestionSet));
}
this.SendSets = toSendSets;
IEnumerable<customers_question_set> receivedQuestionSets = customer.customers_question_sets.Where<customers_question_set>(
x => x.received != null);
BindingList<ComboBoxItem> toReceivedSets = new BindingList<ComboBoxItem>();
foreach (customers_question_set customerQuestionSet in receivedQuestionSets)
{
toReceivedSets.Add(new ComboBoxItem(customerQuestionSet.send.ToString() + " (" + customerQuestionSet.question_set.name + ")", customerQuestionSet));
}
this.ReceivedSets = toReceivedSets;
this.EMail = customer.e_mail;
}
}