datagridviewtextBoxColumn中的AutocompleteSource

时间:2016-10-04 13:58:29

标签: c#

我正在尝试在datagridViewtextBoxColumn中添加AutoCompleteSource。我正在尝试两种方法。在第一种方法中,我直接将AutoCompleteSource添加到datagridViewColumn。在第二个我在所需的单元格上创建了​​一个textBox并添加了AutocompleteCustome Source。但这一切都没有例外。

 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
 {
 DataGridViewCell cel = dataGridView1.CurrentCell;
 DataGridViewRow row = dataGridView1.CurrentRow;
   if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
        {
            if (cel == row.Cells[1])
            {

                DataGridViewTextBoxEditingControl t = e.Control as DataGridViewTextBoxEditingControl;
                AutoCompleteStringCollection ccl = new AutoCompleteStringCollection();
                foreach (DataRow rw in bowoniDataSet17.item.Rows)
                {

                    ccl.Add(rw.ToString());
                }
                t.AutoCompleteSource = AutoCompleteSource.CustomSource;
                t.AutoCompleteCustomSource = ccl;
                t.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            }

        }
    }
 TextBox tb = new TextBox();

 private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {

        DataGridViewRow row = dataGridView1.CurrentRow;
        DataGridViewCell cel = dataGridView1.CurrentCell;
        Rectangle rect=dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex,dataGridView1.CurrentCell.RowIndex,true);
        tb.Size = new Size(rect.Width, rect.Height);
        tb.Location = new Point(rect.X, rect.Y);
        tb.TextAlignChanged += new EventHandler(tbtx_OnTextChanged);
        if (cel == row.Cells[1])
        {


            AutoCompleteStringCollection ccl = new AutoCompleteStringCollection();
            foreach (DataRow rw in bowoniDataSet17.item.Rows)
            {

                ccl.Add(rw.ToString());
            }
            tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
            tb.AutoCompleteCustomSource = ccl;
            tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

        }
    }

 private void tbtx_OnTextChanged(object sender, EventArgs e)
    {
        dataGridView1.CurrentCell.Value = tb.Text;
    }

1 个答案:

答案 0 :(得分:0)

现在我将其更改为DataGridViewComboxColumn。正常工作。