在Winform中的DataGridViewComboBoxColumn中保留用户选择

时间:2011-09-07 06:50:04

标签: datagridview combobox persist

我在Winform中的DataGridView内的DataGridViewComboBoxColumn中持久保存用户的选择时遇到问题。一旦我离开ComboBox,选择就会消失。

我找到了一些问题的答案,例如将SelectedIndex设置为-1,但它没有用。请指出正确的方向。

提前致谢。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // Create DataTable.
        DataColumn classIdColumn = new DataColumn("Class", typeof(string));
        _schoolTable = new DataTable("School");
        _schoolTable.Columns.AddRange(new[] { classIdColumn });
        DataRow row = _schoolTable.NewRow();
        row["Class"] = "yr 5";
        _schoolTable.Rows.Add(row);

        // Bind DataGridView to DataTable, and add ComboBoxColumn.
        dataGridView1.DataSource = _schoolTable;
        DataGridViewComboBoxColumn listCol = new DataGridViewComboBoxColumn();
        listCol.DisplayIndex = 1;
        listCol.DataSource = GetChoices();
        listCol.DisplayMember = "Category";
        listCol.ValueMember = "Number";
        listCol.DefaultCellStyle.NullValue = "None";
        dataGridView1.Columns.Add(listCol);
    }

    private DataTable _schoolTable;

    private static List<IHuman> GetChoices()
    {
        return Choices;
    }

    private static readonly List<IHuman> Choices = new List<IHuman>(){ new Student(), new Teacher() };

    private interface IHuman
    {
        int Number { get; set; }
        string Category { get; }
    }

    private class Student : IHuman
    {
        public int Number { get; set; }
        public string Category { get { return "student"; } }
    }

    private class Teacher : IHuman
    {
        public int Number { get; set; }
        public string Category { get { return "teacher"; } }
    }
}

1 个答案:

答案 0 :(得分:0)

问题的最初原因是您未在IHuman对象上为Number属性指定任何值。

如果您将创建List的代码行更改为:

private static readonly List<IHuman> Choices = new List<IHuman>() { new Student() {Number = 0}, new Teacher() {Number = 1} };

或者将默认值放入实现IHuman的每个对象的Number属性中,就像您对Category属性一样,然后组合框应该可以正常工作。


此外,您可以使代码更容易使用。

您可以做的第一件事是在数据表中添加一列以支持组合框列上的数据绑定,这样您就可以只查看数据表以了解所选内容。执行此操作的代码如下:

DataColumn classIdColumn = new DataColumn("Class", typeof(string));
_schoolTable = new DataTable("School");

//Create a new column in the data table of type int
DataColumn humanIdColumn = new DataColumn("HumanId", typeof(int));

_schoolTable.Columns.AddRange(new[] { classIdColumn });
_schoolTable.Columns.AddRange(new[] { humanIdColumn });

DataRow row = _schoolTable.NewRow();
row["Class"] = "yr 5";
_schoolTable.Rows.Add(row);

// Bind DataGridView to DataTable, and add ComboBoxColumn.
dataGridView1.DataSource = _schoolTable;
DataGridViewComboBoxColumn listCol = new DataGridViewComboBoxColumn();
listCol.DisplayIndex = 1;
listCol.DataSource = GetChoices();
listCol.DisplayMember = "Category";
listCol.ValueMember = "Number";
//Set the DataPropertyName on the comboboxcolumn to enable databinding
listCol.DataPropertyName = "HumanId";
listCol.DefaultCellStyle.NullValue = "None";
dataGridView1.Columns.Add(listCol);

//Hide the autogenerated column HumanId - we only have it for the databinding
dataGridView1.Columns["HumanId"].Visible = false;

在更改之后,我强烈建议您使用数据源的自定义对象列表,而不是使用数据表 - 我总是发现这更灵活。