2

構成情報を保持する DataGridView を作成しようとしています。

使用可能な値は、別の列の値に基づいて列内の行ごとに変わる可能性があるため、comboBox 列に単一のデータソースをアタッチすることはできません。例: 車を選択した場合、利用可能な色はそのモデルで利用可能な色に限定する必要があります。

Car                 ColorsAvailable
Camry               {white,black}
CRV                 {white,black}
Pilot               {silver,sage}

dataGridView を検討する理由は、オペレーターが車を追加するために行を追加できるようにするためです。

このタイプの UI を実装するのに適した設計は何ですか?

4

1 に答える 1

10

DataSourceそれぞれに個別に設定できますDataGridViewComboBoxCell

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0) // presuming "car" in first column
    { // presuming "ColorsAvailable" in second column
        var cbCell = dataGridView1.Rows[e.RowIndex].Cells[1] as DataGridViewComboBoxCell;
        string[] colors = { "white", "black" };
        switch (dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString())
        {
            case "Pilot": colors = new string[] { "silver", "sage" }; break;
                // case "other": add other colors
        }

        cbCell.DataSource = colors;
    }
}

あなたの色(そしておそらく車でさえ)が列挙子のような強いタイプであるなら、もちろんあなたは私がスイッチを入れてここに挿入している文字列の代わりにそれらのタイプを使うべきです...

于 2012-02-07T00:56:36.343 に答える