0

我正在为杂货店或商店制作一个c# 项目。在那里我有一个数据网格,我在其中对单元格进行了编程以自动建议并且它工作正常。

只有一个问题。我希望数据网格中的一列(例如条形码列)成为仅具有自动建议功能的文本框,而不是所有列。

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;
    }
 }
4

2 回答 2

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();
            }
于 2013-01-16T19:47:28.820 回答
0

您的代码几乎是正确的。

我要纠正你的代码是用你的 EditingControlShowing Eventhandler 你基本上说,将任何列视为文本框。

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

 // 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();
于 2013-01-16T20:28:43.557 回答