私がやろうとしているのは、DataGridView をモデルにバインドし、各行に異なる値を持つ 2 つのバインドされていない列を作成することです。これらの列には ComboBoxes が含まれています。
それは一種の作業です:境界値が適切に表示され、コンボボックスにアイテムが入力され、 item.ToString() は、選択するコンボボックスに表示されると思われる値を提供します...しかし、それらは表示されません。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 を使用している場合です。ComboBoxes を生成するクラス:
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
したがって、2 つのオプションを持つ 1 つの ComboBox と 1 つのオプションを持つ 1 つの 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;
}
}