Sometimes up-down keys do not work with DataGridView

Sometimes the up and down keys do not work on DataGridView.

I have no clue why this is especially strange, because for key events DataGridViewthere is no code assigned <...>

SelectionMode - FullRowSelect

Multi Selector False

This dos code doesn't help ...

     private void dataGridView1_PreviewKeyDown(object sender, reviewKeyDownEventArgs e)
            {
                switch (e.KeyCode)
                {
                    case Keys.Down:
                        e.IsInputKey = true;
                        break;
                    case Keys.Up:
                        e.IsInputKey = true;
                        break;
                }
            }

  private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyData == Keys.Down)
            {

                e.Handled = true;
            }
            else if (e.KeyData == Keys.Up)
            {

                e.Handled = true;
            }
        }

Any clue?

PS

It seems that the method SelectionChangeddoes some hard work ... So when I turn it off, eberything is fine.

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    // Some hard work
}

So the question is how to optimize it.

I suppose to use a timer, so when the user stops the selection of the arrow keys 1 second later, the method code SelectionChangedmust be executed.

Any clue on the best way to do this?

+4
2

- SelectionChanged . , - User Controls .

, , !

 bool canDoHardWork = true;
 private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (canDoHardWork)
            {
                int interval = 2000; // Just 2 seconds
                Task.Factory.StartNew(() =>
                {
                    canDoHardWork= false;
                    Thread.Sleep(interval);
                    this.BeginInvoke((Action)(() =>
                    {                         
                        PopulateTabs(); // Very hard work
                        dataGridView1.Focus();
                        canDoHardWork= true;
                    }), null);

                });
            }
        }
+1

, PopulateTabs(), Focus DataGridView. . . (PopulationTabs), , TPL. , , , "DoHardWork" DataGridView . , , , , SelectionChanged , "DoesHardWork" , DoesHardWork false, PopulateTabs(). CellValue/RowValue/Control, PopulateTabs() , PopulateTabs(), IAsyncResult.

- :

bool canDoHardWork = true;

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
   if (canDoHardWork)
   {
    IAsyncResult result;
    Task.Factory.StartNew(() =>
    {
       canDoHardWork = false;
       result = this.BeginInvoke((Func<Button>)(() =>
       {                         
           canDoHardWork = true;
           return PopulateTabs(); // Very hard work
       }), null);
       this.dataGridView1.Controls.Add((Button)this.EndInvoke(result));
       dataGridView1.Focus();
    });
   }
}

, , DataGridView . . , , . DataGridView , .

0

Source: https://habr.com/ru/post/1629370/


All Articles