C#在Datagridview上获取Custom Combobox的值

时间:2017-02-10 05:55:34

标签: c# winforms datagridview combobox

我在datagridview上需要一个组合框,允许使用通配符搜索。使用datagridviewcomboboxcolumn,它只允许从搜索开始。所以,我利用执行通配符和正则表达式搜索的EasyCompletionComboBox。由于必须在datagridviewcolumn中使用它,我必须创建一个自定义的comboBox列。

我在实现它方面相当成功,但我无法获得组合框的Selected值(值成员)。我正在获取数据成员。即使我使用所需的值填充组合框数据表。也许ECComboBoxEditingControl类出了问题。

非常感谢任何帮助!

class ECComboBoxColumn : DataGridViewColumn
{
    public ECComboBoxColumn() : base(new ECComboBoxCell())
    {
    }
    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            // Ensure that the cell used for the template is a CalendarCell.
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(ECComboBoxCell)))
            {
                throw new InvalidCastException("Must be a ECComboBoxCell");
            }
            base.CellTemplate = value;
        }
    }
}

class ECComboBoxCell : DataGridViewTextBoxCell
{
    public ECComboBoxCell()
    : base()
{
        // Use the short date format.
        //this.Style.Format = "d";
    }

    public override void InitializeEditingControl(int rowIndex, object
        initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        // Set the value of the editing control to the current cell value.
        base.InitializeEditingControl(rowIndex, initialFormattedValue,
            dataGridViewCellStyle);
        ECComboBoxEditingControl ctl =
            DataGridView.EditingControl as ECComboBoxEditingControl;
        // Use the default row value when Value property is null.
        if (this.Value == null)
        {
            ctl.SelectedIndex = -1;//(String)this.DefaultNewRowValue;
        }
        else
        {
            ctl.SelectedItem = (String)this.Value;
        }
    }

    public override Type EditType
    {
        get
        {
            // Return the type of the editing control that CalendarCell uses.
            return typeof(ECComboBoxEditingControl);
        }
    }

    public override Type ValueType
    {
        get
        {
            // Return the type of the value that CalendarCell contains.

            return typeof(String);
        }
    }

    public override object DefaultNewRowValue
    {
        get
        {
            // Use the current date and time as the default value.
            return "";
        }
    }
}

class ECComboBoxEditingControl : EasyCompletionComboBox, IDataGridViewEditingControl
{
    DataGridView dataGridView;
    private bool valueChanged = false;
    int rowIndex;

    public ECComboBoxEditingControl()
    {
        // this.Format = DateTimePickerFormat.Short;
        this.DropDownStyle = ComboBoxStyle.DropDown;
    }

    // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
    // property.
    public object EditingControlFormattedValue
    {
        get
        {
            return this.SelectedValue;
        }
        set
        {
            if (value is Decimal)
            {

                    this.SelectedItem = value;
            }
        }
    }

    // Implements the 
    // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
    public object GetEditingControlFormattedValue(
        DataGridViewDataErrorContexts context)
    {
        return this.GetItemText(this.SelectedItem);
    }

    // Implements the 
    // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
    public void ApplyCellStyleToEditingControl(
        DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
        this.ForeColor = dataGridViewCellStyle.ForeColor;
        this.BackColor = dataGridViewCellStyle.BackColor;
    }

    // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
    // property.
    public int EditingControlRowIndex
    {
        get
        {
            return rowIndex;
        }
        set
        {
            rowIndex = value;
        }
    }

    // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
    // method.
    public bool EditingControlWantsInputKey(
        Keys key, bool dataGridViewWantsInputKey)
    {
        // Let the DateTimePicker handle the keys listed.
        switch (key & Keys.KeyCode)
        {
            case Keys.Left:
            case Keys.Up:
            case Keys.Down:
            case Keys.Right:
            case Keys.Home:
            case Keys.End:
            case Keys.PageDown:
            case Keys.PageUp:
                return true;
            default:
                return !dataGridViewWantsInputKey;
        }
    }

    // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
    // method.
    public void PrepareEditingControlForEdit(bool selectAll)
    {
        // No preparation needs to be done.
    }

    // Implements the IDataGridViewEditingControl
    // .RepositionEditingControlOnValueChange property.
    public bool RepositionEditingControlOnValueChange
    {
        get
        {
            return false;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingControlDataGridView property.
    public DataGridView EditingControlDataGridView
    {
        get
        {
            return dataGridView;
        }
        set
        {
            dataGridView = value;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingControlValueChanged property.
    public bool EditingControlValueChanged
    {
        get
        {
            return valueChanged;
        }
        set
        {
            valueChanged = value;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingPanelCursor property.
    public Cursor EditingPanelCursor
    {
        get
        {
            return base.Cursor;
        }
    }

    protected override void OnSelectedIndexChanged(EventArgs eventargs)
    {
        // Notify the DataGridView that the contents of the cell
        // have changed.
        valueChanged = true;
        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        base.OnSelectedIndexChanged(eventargs);
    }   
}


public partial class Form1 : Form
{
    EasyCompletionComboBox cmbProject;
    DataTable dtProjects;
    int projectComboBoxPopulated = 0;
    public Form1()
    {
        InitializeComponent();
        dtProjects = new DataTable();
        dtProjects.Columns.Add("ProjectID", typeof(string));
        dtProjects.Columns.Add("ProjectName", typeof(string));

        dtProjects.Rows.Add(0, "--Select--");
        dtProjects.Rows.Add(1, "Project 1");
        dtProjects.Rows.Add(2, "Project 2");
        dtProjects.Rows.Add(3, "Project 3");
        dtProjects.Rows.Add(4, "Project 4");
        dtProjects.Rows.Add(5, "Project 5");


        cmbProject = new EasyCompletionComboBox();

        dataGridView1.ColumnCount = 3;
        dataGridView1.Columns[0].Name = "Product ID";
        dataGridView1.Columns[1].Name = "Product Name";
        dataGridView1.Columns[2].Name = "Product Price";

        string[] row = new string[] { "1", "Product 1", "1000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "2", "Product 2", "2000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "3", "Product 3", "3000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "4", "Product 4", "4000" };
        dataGridView1.Rows.Add(row);

        ECComboBoxColumn cmb = new ECComboBoxColumn();
        //DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
        cmb.HeaderText = "Select Data";
        cmb.Name = "cmb";

        dataGridView1.Columns.Add(cmb);

    }
    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (projectComboBoxPopulated == 0)
        {
            cmbProject = e.Control as EasyCompletionComboBox;
            cmbProject.MatchingMethod = StringMatchingMethod.UseWildcards;
            cmbProject.DisplayMember = "ProjectName";
            cmbProject.ValueMember = "ProjectID";
            cmbProject.DataSource = dtProjects;
            projectComboBoxPopulated = 1;
        }

    }
    private void button1_Click(object sender, System.EventArgs e)
    {
        for (int i = dataGridView1.Rows.Count - 1; i >= 0; i--)
        {
            Debug.WriteLine(dataGridView1.Rows[i].Cells[3].Value); // HERE IS THE PROBLEM
        }
    }
}

0 个答案:

没有答案