DataGridViewComboBoxColumn忽略DataSource字符串数组 [英] DataGridViewComboBoxColumn ignores DataSource string array

查看:74
本文介绍了DataGridViewComboBoxColumn忽略DataSource字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VS2012,创建了带有六个DataGridViewTextBoxColumn的DataGridView,全部只读,一切正常。

With VS2012, created DataGridView with six DataGridViewTextBoxColumn, all read only, all work fine.

在VS2012设计器中添加第七列为DataGridViewComboBoxColumn,不是只读。

Added seventh column in VS2012 designer as DataGridViewComboBoxColumn, not read only.

在C#表单构造函数中设置DataSource,如下所示:

Set DataSource in C# form constructor as follows:

int row = dataGridView_ActiveJob.Rows.Add(view.viewName, view.cameraName, view.imageSize, view.frameRate.ToString(), mainForm.MakeExposureTime(view.exposureTime), "0%");
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)(dataGridView_ActiveJob.Rows[row].Cells["Rotate"]);
cell.DataSource = new string[] { "0", "90", "180", "270" };

表格显示正确,显示将列旋转为组合框,但第一次点击组合框没有反应,第二次点击有下拉一行空白,上面的字符串数组中没有四个项目。

Form displays properly, showing Rotate column as combo box, but first click of combo box has no reaction, second click has drop down of one row blank, none of the four items in the above string array.

什么阻止组合框丢弃并显示四个字符串数组选项?

What is preventing combo box from dropping and showing the four string array options?

推荐答案

嗨弗兰克,

>> 但首次点击组合框没有反应

第一个效果单击是让单元格获得焦点,您可以通过以下步骤来避免它。

The effect of the first click is to make the cell get the focus, you can avoid it by the following steps.

首先,设置属性" EditMode "将DataGridView控件更改为" EditOnEnter "。

First, set the property "EditMode" of the DataGridView control to "EditOnEnter".

其次,添加"CellClick"事件到DataGridView。具体代码如下:

Second, add the "CellClick" event to the DataGridView. The specific code is as follows.

    private void dataGridView_ActiveJob_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex >= 0 && e.RowIndex >= 0 && dataGridView_ActiveJob[e.ColumnIndex, e.RowIndex] != null && !dataGridView_ActiveJob[e.ColumnIndex, e.RowIndex].ReadOnly)
        {
            DataGridViewComboBoxColumn comboBoxColumn = dataGridView_ActiveJob.Columns[e.ColumnIndex] as DataGridViewComboBoxColumn;
            if (comboBoxColumn != null)
            {
                dataGridView_ActiveJob.BeginEdit(true);
                DataGridViewComboBoxEditingControl comboBoxEditingControl = dataGridView_ActiveJob.EditingControl as DataGridViewComboBoxEditingControl;
                if (comboBoxEditingControl != null)
                {
                    comboBoxEditingControl.DroppedDown = true;
                }
            }
        }
    }

>> 第二次点击有一行空白下拉列表,上面的字符串数组中没有四项。

请确认是否可以访问代码段。

Please confirm whether the code segment can be accessed.

我可以在测试中显示记录:

I can display the records in my test:

    public Form1()
    {
        InitializeComponent();
        view.viewName = "A";
        view.cameraName = "B";
        view.imageSize = "10";
        view.frameRate = 12;
        view.exposureTime = 34;
    }
    struct View
    {
        public string viewName;
        public string cameraName;
        public string imageSize;
        public double frameRate;
        public double exposureTime;
    }
    View view;
    static string MakeExposureTime(double time)
    {
        return time.ToString();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        int row = dataGridView_ActiveJob.Rows.Add(view.viewName, view.cameraName, view.imageSize, view.frameRate.ToString(), Form1.MakeExposureTime(view.exposureTime), "0%");
        DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)(dataGridView_ActiveJob.Rows[row].Cells["Rotate"]);
        cell.DataSource = new string[] { "0", "90", "180", "270" };
        dataGridView_ActiveJob.AllowUserToAddRows = false;
    }

问候,

Stanly


这篇关于DataGridViewComboBoxColumn忽略DataSource字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆