datagridview组合框一次查看所有内容,下拉gridview

时间:2015-10-29 21:13:45

标签: c# datagridview combobox

我有4列17行的数据网格视图,第1列是数字1-16,第2-4列是组合框,它们都具有相同的值。这些组合框中最多可列出100个项目。我想以网格模式显示这些项目,可能是10x10或者其他东西,因此用户可以一次看到所有项目并选择一个而不是一个长列表来滚动。

这是构建组合框的一个列,因为你可以看到它有很多项目。

DataGridViewComboBoxColumn DCSfreq = new DataGridViewComboBoxColumn();
DCSfreq.DataPropertyName = "DCS (Hz)";
DCSfreq.HeaderText = "DCS (Hz)";
String[] DCS = { "", "23", "25", "26", "31", "32", "43", "47", "51", "54", "65", "71", "72", "73", "74", "114", "115", "116", "125", "131", "132", "134", "143", "152", "155", "156", "162", "165",
"172", "174", "205", "223", "226", "243", "244", "245", "251", "261", "263", "265", "271", "306", "311", "315", "331", "343", "346", "351", "364", 
"365", "371", "411", "412", "413", "423", "431", "432", "445", "464", "465", "466", "503", "506", "516", "532", "546", "565", "606", "612", "624",
"627", "631", "632", "654", "662", "664", "703", "712", "723", "731", "732", "734", "743", "754" };
DCSfreq.Items.AddRange(DCS);
dataGridView1.Columns.Add(DCSfreq);

1 个答案:

答案 0 :(得分:0)

您可能希望使用嵌套的DataGridView代替ComboBox,如下所示:

private DataGridView childDgv = new DataGridView();

private void Form1_Load(object sender, EventArgs e)
{
    parentDgv.CellClick += parentDgv_CellClick;
    parentDgv.Columns["DCS (Hz)"].ReadOnly = true; // uneditable

    Set10x10Data(childDgv); // set 10 x 10 data to the child DataGridView
    childDgv.ColumnHeadersVisible = false;
    childDgv.RowHeadersVisible = false;

    childDgv.CellClick += childDgv_CellClick;
    childDgv.Visible = false;

    parentDgv.Controls.Add(childDgv);
}

void parentDgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].HeaderText == "DCS (Hz)")
    {
        // Show child DataGridView under the clicked cell
        Rectangle dgvRectangle = parentDgv.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
        childDgv.Location = new Point(dgvRectangle.X, dgvRectangle.Y + 20);
        childDgv.CurrentCell = childDgv[0, 0];
        childDgv.Visible = true;
    }
}

void childDgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
    parentDgv.CurrentCell.Value = childDgv[e.ColumnIndex, e.RowIndex].Value;
    childDgv.Visible = false;
}