1

我要做的是将 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;
    }
}
4

1 回答 1

0

如果我正确理解了您的代码,那么您正在尝试为每一行使用不同的数据源,这是数据网格视图中组合框列的非标准用法。试试这个示例:

公共类 Form1 : Form { public Form1() { var grid = new DataGridView { Dock = DockStyle.Fill, AutoGenerateColumns = false };

        var column = new DataGridViewComboBoxColumn();
        column.DataPropertyName = "Selected";
        column.DisplayMember = "Text";
        column.ValueMember = "Value";
        grid.Columns.Add(column);
        grid.DataSource = new[]
        {
            new Item
            {
                Selected = 1 ,
                Elements = new[]
                {
                    new Element { Text = "first", Value = 1 },
                    new Element { Text = "third", Value = 3 }
                }
            },
            new Item
            {
                Selected = 5 ,
                Elements = new[]
                {
                    new Element { Text = "second", Value = 2 },
                    new Element { Text = "fifth", Value = 5 },
                    new Element { Text = "sixth", Value = 6 }
                }
            }
        };
        Controls.Add(grid);
        grid.RowsAdded += new DataGridViewRowsAddedEventHandler(grid_RowsAdded);
    }

    private void grid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        var grid = (DataGridView)sender;
        for (int i = 0; i < e.RowCount; i++)
        {
            var item = (Item)grid.Rows[e.RowIndex + i].DataBoundItem;

            var cell = (DataGridViewComboBoxCell)grid.Rows[e.RowIndex + i].Cells[0];

            cell.DataSource = item.Elements;
            Debug.WriteLine(cell.Items.Count);
        }
    }
}

internal class Item
{
    public int Selected { get; set; }

    public IEnumerable<Element> Elements { get; set; }
}

internal class Element
{
    public string Text { get; set; }

    public int Value { get; set; }
}

但我不太确定你Selected在行类中的属性是什么。您当然知道此列的目的是选择许多可用项目之一,而不是显示项目列表。

于 2012-07-23T08:48:48.687 回答