yash sharma
yash sharma

Reputation: 45

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. 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..

all thing must be fired in textchanged event.. here is my code:-

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

Upvotes: 3

Views: 10901

Answers (2)

Junaith
Junaith

Reputation: 3388

Showing MessageBox in TextChanged will be very annoying.

Instead you could try it in DataGridView.CellValidated event which is fired after validation of the cell is completed.

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

Upvotes: 0

Sinatr
Sinatr

Reputation: 21969

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

Upvotes: 5

Related Questions