将textchange事件添加到datagridview单元格

时间:2016-06-17 08:31:39

标签: c# datagridview

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

我已尝试将textChanged事件处理程序添加到单元格中,如中所示 " How to get the text from current cell in datagridview textchanged event?&#34 ;.有用。 但有一段时间它崩溃并显示错误

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");
        }
    }

2 个答案:

答案 0 :(得分:0)

这就是我要改变的地方:

您的代码:

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控件,因此您不应该依赖它是相同的;因此,不挂钩可能会失败,也可能是其他事情。

实际上需要另外检查以获得正确类型的编辑控件;毕竟可能有ComboBoxCells或CheckBoxCell等东西。

也许像这样检查:

if (tb is TextBox)..

答案 1 :(得分:-1)

这是最适合您的答案示例。

 private void dgvEntry_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
                double iTotal,iQty,iPrice;

            try
            {
                var itmColumn = dgvEntry[e.ColumnIndex, e.RowIndex];
                if(itmColumn.OwningColumn.Index>0)
                {
                    iTotal = 0; iQty = 0; iPrice = 0;
                    foreach(DataGridViewRow row in dgvEntry.Rows)
                    {
            iQty = Convert.ToDouble(row.Cells[dgvDOentry.Columns["itQty"].Index].Value);
          iPrice = Convert.ToDouble(row.Cells[dgvDOentry.Columns["itRate"].Index].Value);
         row.Cells[dgvEntry.Columns["itTotal"].Index].Value = iQty * iPrice;
                    }
                }

            }