将textchange事件添加到datagridview单元格 [英] Add textchange event to datagridview cell

查看:80
本文介绍了将textchange事件添加到datagridview单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在datagridview c#中创建一个自动完成的单元格。我希望当文本更改时动态填充autocompletecustomsource。

I want to make an autocomplete cell in datagridview c#. and I want the autocompletecustomsource dynamicly populated when the text change.

我试图按照
。有用。
,但有一段时间它崩溃并显示错误

I've tried to add the textChanged event handler to the cell as suggested in "How to get the text from current cell in datagridview textchanged event?". it works. but some time it crashed and showing error

Vschost32 Error, or memory access violation.

任何想法如何实现?

更新1

我的代码

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        DataGridView dgv = sender as DataGridView;
        string headerText = dgv.Columns[dgv.CurrentCell.ColumnIndex].HeaderText.ToString();
        DataGridViewTextBoxEditingControl tb = e.Control as DataGridViewTextBoxEditingControl;
        removeAutoComplete(tb);
        if (headerText == "Kode Barang" && tb !=null)
        {
            tb.TextChanged += new EventHandler(tb_kodeBarang_TextChanged);
        }
        else if(headerText == "Nama Barang" && tb!=null)
        {
            tb.TextChanged += new EventHandler(tb_namaBarang_TextChanged);
        }  
    }
    private void removeAutoComplete(TextBox tb)
    {
        tb.TextChanged -= tb_kodeBarang_TextChanged;
        tb.TextChanged -= tb_namaBarang_TextChanged;
        tb.AutoCompleteMode = AutoCompleteMode.None;
    }
    private void tb_kodeBarang_TextChanged(object sender,EventArgs e)
    {
        TextBox tb = sender as TextBox;
        if (tb.Text.Length > 0 && tb.Text != "" && tb.Text != null)
        {
            tb.AutoCompleteMode = AutoCompleteMode.Suggest;
            tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
            tb.AutoCompleteCustomSource = getStringCollection(tb.Text,"Kode");                
        }
    }
    private void tb_namaBarang_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = sender as TextBox;
        if (tb.Text.Length > 0 && tb.Text != "" && tb.Text != null)
        {
            tb.AutoCompleteMode = AutoCompleteMode.Suggest;
            tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
            tb.AutoCompleteCustomSource = getStringCollection(tb.Text,"Nama");
        }
    }


推荐答案

此我将更改以下内容:

您的代码:

private void dataGridView1_EditingControlShowing(object sender,
                           DataGridViewEditingControlShowingEventArgs e)
{
    DataGridView dgv = sender as DataGridView;
    string headerText = dgv.Columns[dgv.CurrentCell.ColumnIndex].HeaderText.ToString();
    DataGridViewTextBoxEditingControl tb = e.Control as DataGridViewTextBoxEditingControl;
    removeAutoComplete(tb);
    ...

建议编辑:

 DataGridViewTextBoxEditingControl tb = null;  // keep reference

 private void dataGridView1_EditingControlShowing(object sender,
                           DataGridViewEditingControlShowingEventArgs e)
{
    DataGridView dgv = sender as DataGridView;
    string headerText = dgv.Columns[dgv.CurrentCell.ColumnIndex].HeaderText.ToString();
    if (tb != null) removeAutoComplete(tb);  // check for null and unhook old tb
    tb = e.Control as DataGridViewTextBoxEditingControl;
    ...

在编辑其他单元格/行时,该单元格不会得到相同的结果TextBox控件,因此您不应该依赖于它。

When editing a different cell/row the cell will not get the same TextBox control so you should not rely on it being the same; hence the un-hooking may fail and maybe other things as well.

实际上,需要另一种检查来获取正确类型的编辑控件。毕竟可能有诸如ComboBoxCells或CheckBoxCell等之类的东西。

Actually another check for getting the correct type of editing control is called for; after all there may be stuff like ComboBoxCells or CheckBoxCell etc..

也许像这样检查:

if (tb is TextBox)..

这篇关于将textchange事件添加到datagridview单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆