创建自定义datagridview的列类型

时间:2018-01-05 17:31:19

标签: c# winforms datagridview

我只是创建一个自定义控件,我希望这个控件实现到datagridview的列。

但是怎么样?有可能吗?

enter image description here

例如,我想添加我的列类型,如" DataGridViewCustomTextBoxColumn"

这是我目前的代码。

 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {    
        if (dataGridView1.CurrentCell.ColumnIndex == 0)
        {

            Connection.ConnectionClose();
            Connection.ConnectionOpen();

            var source = new List<string>();
            string queryItem = "SELECT * FROM ITEM ";              

            Connection.command = new OleDbCommand(queryItem, Connection.conn);
            Connection.command.CommandType = CommandType.Text;
            AutoCompleteStringCollection kode = new AutoCompleteStringCollection();

            reader = Connection.command.ExecuteReader();

            if (reader.HasRows == true)
            {
                while (reader.Read())
                {
                    //kode.Add(reader["code"].ToString());
                    source.Add(reader["code"].ToString());
                }
            }
            else
            {
                MessageBox.Show("Data not Found");
            }
            reader.Close();
            //ComboBox txtBusID = e.Control as ComboBox;
            TextBox kodeTxt = e.Control as TextBox;
            if (kodeTxt != null)
            {
                kodeTxt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                kodeTxt.AutoCompleteCustomSource = kode;
                kodeTxt.AutoCompleteSource = AutoCompleteSource.CustomSource;
            }

        }
  }

我试过的......就像这样。

 AutoCompleteTextBoxSample.AutoCompleteTextbox kodeTxt = e.Control as AutoCompleteTextBoxSample.AutoCompleteTextbox;
                if (kodeTxt != null)
                {
                    kodeTxt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    kodeTxt.AutoCompleteCustomSource = source;
                    kodeTxt.AutoCompleteSource = AutoCompleteSource.CustomSource;
                }

然后我的自动完成工作不再有效了

我想要的就是这样。

enter image description here

1 个答案:

答案 0 :(得分:0)

我会使用模板字段,如下所述:https://developer.mozilla.org/de/docs/Web/API/Window/frames

其中一个模板字段将是您的自定义控件。

这将涉及编写代码,而不是使用GUI。