简体   繁体   English

如何在datagridview textchanged 事件中从当前单元格获取文本?

[英]How to get the text from current cell in datagridview textchanged event?

i am making a windows form application in which i used a datagridview.我正在制作一个 Windows 窗体应用程序,其中使用了 datagridview。 i want that when i write something in textbox in datagridview,than a messagebox appears containing the string i wrote.. ican't get my text in textchanged event..我希望当我在 datagridview 的文本框中写一些东西时,会出现一个包含我写的字符串的消息框..我无法在 textchanged 事件中获取我的文本..

all thing must be fired in textchanged event.. here is my code:-所有的事情都必须在 textchanged 事件中触发..这是我的代码:-

 void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == 1)
            {
                TextBox tb = (TextBox)e.Control;
                tb.TextChanged += new EventHandler(tb_TextChanged);
            }
        }
        void tb_TextChanged(object sender, EventArgs e)
        {
            //listBox1.Visible = true;
            //string firstChar = "";
            //this.listBox1.Items.Clear();
            //if (dataGridView1.CurrentCell.ColumnIndex == 1)
            {
                string str = dataGridView1.CurrentRow.Cells["Column2"].Value.ToString();
                if (str != "")
                {

                    MessageBox.Show(str);
                }
            }
void tb_TextChanged(object sender, EventArgs e)
{
    var enteredText = (sender as TextBox).Text
    ...
}

Showing MessageBox in TextChanged will be very annoying.TextChanged显示MessageBox会很烦人。

Instead you could try it in DataGridView.CellValidated event which is fired after validation of the cell is completed.相反,您可以在完成单元格验证后触发的DataGridView.CellValidated事件中尝试它。

Sample code:示例代码:

dataGridView1.CellValidated += new DataGridViewCellEventHandler(dataGridView1_CellValidated);

void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
    {
        MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM