question

Andreasss-5315 avatar image
0 Votes"
Andreasss-5315 asked Viorel-1 edited

Trying to change cell.Value in dataGridView on each KeyUp event

Hello!

I have a dataGridView1. I have added one row with the string: "This is a string".

Now what I try to do is to change the string by typing something new like: "This is a string2"

What I try to do is that the string MUST change in the cell.Value after each KeyUp event, which means Instantly after each new letter is pressed while writing in the textBox cell.

However the _KeyUp event is putting the original "This is a string" value back each _KeyUp event.

How can we achieve this functionality?

Thank you!

                 private void Form1_Load(object sender, EventArgs e)
                 {
                     dataGridView1.Rows.Add(); int index = dataGridView1.Rows.Count - 2;
                     dataGridView1.Rows[index].Cells[0].Value = "This is a string";
                 }
        
                 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                 {
                     if (e.Control is DataGridViewTextBoxEditingControl tb)
                     {
                         tb.KeyUp -= dataGridView1_KeyUp;
                         tb.KeyUp += dataGridView1_KeyUp;
                     }
                 }
                 //then in the keyup event handler, execute your code
                 private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
                 {
                     if (dataGridView1.EditingControl is TextBox)
                     {
                         //Trying to change the cell.Value to current text in the cell. But it sets back the orignal string in the CELL ?
                         dataGridView1.EditingControl.Text = dataGridView1.CurrentCell.Value.ToString();
                     }
                 }


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Andreasss-5315 avatar image
0 Votes"
Andreasss-5315 answered Viorel-1 edited

I think I found a solution like this which seems to work. Thank you!

         private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
         {
             if (e.Control is DataGridViewTextBoxEditingControl)
             {
                 DataGridViewTextBoxEditingControl iDataGridViewEditingControl = e.Control as DataGridViewTextBoxEditingControl;
                 iDataGridViewEditingControl.KeyUp -= SlotTimesDGV_EditingControlShowing_KeyUp;
                 iDataGridViewEditingControl.KeyUp += SlotTimesDGV_EditingControlShowing_KeyUp;
             }
         }
         private void SlotTimesDGV_EditingControlShowing_KeyUp(object sender, KeyEventArgs e)
         {
             TextBox txtbox = sender as TextBox;
             String text = txtbox.Text.Trim();
             if (dataGridView1.CurrentCell.RowIndex != -1 && dataGridView1.CurrentCell.ColumnIndex != -1)
             {
                 dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[dataGridView1.CurrentCell.ColumnIndex].Value = text;
             }
         }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


How should it work if you change the text with mouse?

0 Votes 0 ·