只使用autosuggest创建一个dataGridColumn

时间:2013-01-16 19:32:23

标签: c# sql datagridview

我正在为Grocery或商店制作 c#项目。在那里,我有一个 datagrid ,我在其中将单元格编程为自动建议,并且工作正常。

只有一个问题。我希望数据网格中的一列(例如BARCODE列)只是一个带有autosuggest的文本框,而不是所有列。

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    var source = new AutoCompleteStringCollection();
    string cnn = (@"Data Source=TaLy-PC;Initial Catalog=dbMarketi;Integrated Security=True;Pooling=False");
    SqlConnection connection = new SqlConnection(cnn.ToString());
    SqlCommand command = new SqlCommand("Select Barcode FROM tblDepo", connection);

    try
    {
        connection.Open();
        {
            SqlDataReader drd = command.ExecuteReader();
            while (drd.Read())
            {
                // this.comboBoxEx1.Items.Add(drd.GetString(0).ToString());
                source.AddRange(new string[] { drd.GetString(0).ToString() });
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
    connection.Close();

    TextBox dgvEditBox = e.Control as TextBox;
    if (dgvEditBox != null)
    {
        dgvEditBox.AutoCompleteMode = AutoCompleteMode.Suggest;
        dgvEditBox.AutoCompleteCustomSource = source;
        dgvEditBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }
 }

2 个答案:

答案 0 :(得分:0)

这是一个建议,请尝试将您的AutoCompleteStringCollection和条形码Products的查询分开。

private AutoCompleteStringCollection source  = new AutoCompleteStringCollection();

            private void loadAutoCompleteStringCollection();
            {
                string cnn = (@"Data Source=TaLy-PC;Initial Catalog=dbMarketi;Integrated Security=True;Pooling=False");
                source.Clear();
                SqlConnection connection = new SqlConnection(cnn.ToString());
                SqlCommand command = new SqlCommand("Select Barcode FROM tblDepo", connection);

                try
                {
                    connection.Open();
                    {
                        SqlDataReader drd = command.ExecuteReader();
                        while (drd.Read())
                        {
                            // this.comboBoxEx1.Items.Add(drd.GetString(0).ToString());
                            source.AddRange(new string[] { drd.GetString(0).ToString() });
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                finally
                {
                    connection.Close();
                }
            }

            private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                TextBox dgvEditBox = e.Control as TextBox;
                if (dataGridView1.CurrentCell.ColumnIndex == 1)
                {
                    if (dgvEditBox != null)
                    {
                        dgvEditBox.AutoCompleteMode = AutoCompleteMode.Suggest;
                        dgvEditBox.AutoCompleteCustomSource = source;
                        dgvEditBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    }
                }
            }

            //If you want to reload the AutoCompleteStringCollection
            private void button1_Click(object sender, System.EventArgs e)
            {
                loadAutoCompleteStringCollection();
            }

答案 1 :(得分:0)

您的代码几乎是正确的。

我要更正你的代码是用你的EditingControlShowing事件处理程序,你基本上说,将任何列视为文本框。

如果您只想将一个特定列视为文本框,则运行检查,如下所示: -

 // EditingCOntrol Showing

    If(datagridview.CurrentCell.ColumnIndex == 1)
    {
        TextBox dgvEditBox = e.Control as TextBox;
        if (dgvEditBox != null)
        {
            dgvEditBox.AutoCompleteMode = AutoCompleteMode.Suggest;
            dgvEditBox.AutoCompleteCustomSource = source;
            dgvEditBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
        }
    }


// public Object 

 public AutoCompleteStringCollection source = new AutoCompleteStringCollection();