1

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"; } }
    }
}
4

1 に答える 1

0

この問題の最初の原因は、IHumanオブジェクトのNumberプロパティに値を指定していないことです。

リストを作成するコード行を次のように変更した場合:

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

または、Categoryプロパティの場合と同じようにIHumanを実装する各オブジェクトのNumberプロパティにデフォルト値を設定すると、コンボボックスが正しく機能するはずです。


さらに、コードの操作を少し簡単にすることができます。

最初にできることは、コンボボックス列でのデータバインディングをサポートするためにデータテーブルに列を追加することです。これにより、データテーブルを見るだけで、何が選択されたかを知ることができます。これを行うためのコードは次のとおりです。

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;

その変更後は、データテーブルを使用するのではなく、データソースにカスタムオブジェクトのリストを使用することを強くお勧めします。これは常にはるかに柔軟であることがわかりました。

于 2011-09-14T13:24:55.233 に答える