【问题标题】:Autocomplete for cells in dataGridView from dataset using C#使用 C# 自动完成数据集中 dataGridView 中的单元格
【发布时间】:2018-06-04 08:01:13
【问题描述】:

我有一个从数据集中填充的 dataGridView:

  dataGridView1.DataSource = aSH_ORDER_DBDataSet.ASH_PROD_ORDERS;

向 datagridview 添加新条目时,是否可以让单元格自动完成或根据该列中其他单元格的内容提出建议?

我已经搜索过这个,但我找到的所有内容都是文本框,对我来说没有意义。

【问题讨论】:

    标签: c# winforms datagridview autocomplete


    【解决方案1】:

    好的,我想通了。

    这部分是从其他地方抓取的,并由我编辑用于我的特定用途:

    此代码从您正在编辑的当前列中获取数据,并会根据该列建议自动完成答案。

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
                TextBox autoText = e.Control as TextBox;
                if (autoText != null)
                {
                    autoText.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    autoText.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
                    addItems(DataCollection);
                    autoText.AutoCompleteCustomSource = DataCollection;
                }
        }
    
        public void addItems(AutoCompleteStringCollection col)
        {
            var selectedColumn = dataGridView1.CurrentCell.ColumnIndex;
    
            List<string> headerList = new List<string>();
            foreach (DataRow row in aSH_ORDER_DBDataSet.ASH_PROD_ORDERS.Rows)
            {
                headerList.Add(row[selectedColumn].ToString());
            }
    
            List<string> cleanHeaderList = headerList.Distinct().ToList();
            foreach (var item in cleanHeaderList)
            {
                col.Add(item);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 2014-08-06
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      相关资源
      最近更新 更多