开发者

Persist user selection in DataGridViewComboBoxColumn in Winform

开发者 https://www.devze.com 2023-04-02 20:21 出处:网络
I have problem in persisting a user\'s selection in a DataGridViewComboBoxColumn inside a DataGridView in Winform. Once I leave the ComboBox, the selection disappears.

I have problem in persisting a user's selection in a DataGridViewComboBoxColumn inside a DataGridView in Winform. Once I leave the ComboBox, the selection disappears.

I'd found some answers to the problem such as setting the SelectedIndex to -1, but it didn't work. Kindly please point to me the right direction.

Thanks in advance.

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 ComboBoxColum开发者_Go百科n.
        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"; } }
    }
}


This initial cause of the problem is that you are not specifying any values for the Number property on your IHuman objects.

If you change your line of code where you create the List to something like:

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

Or put default values into the Number property of each object the implements IHuman as you have for the Category property then you combobox should work correctly.


In addition you can make your code a little bit easier to work with.

The first thing you can do is add a column to your datatable to support databinding on your combobox column, allowing you to just look at the datatable to know what was selected. The code to do this is below:

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;

After that change I would strongly recommend using lists of custom objects for your datasource rather that using a datatable - I've always found this to be much more flexible.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号