如果DataGridView不是活动控件,则会滚动到顶部

时间:2015-07-13 11:10:01

标签: c# winforms datagridview scrollbar

我的Form顶部有一些TextBoxesfilter我的DataGridView位于底部。一切都很好,除了一个行为困扰我。 如果TextBoxFocus而我向下滚动DataGridView,然后ClickCell滚动,DataGridView滚动回到Cell最佳。但它选择了“正确的”Cell。因此,如果我向下滚动从顶部点击第三个Cell,它会向上滚动,然后选择第三个 AlertForm alert; private void btnImport_Click(object sender, EventArgs e) { if (backgroundWorker1.IsBusy != true) { // create a new instance of the alert form alert = new AlertForm(); // event handler for the Cancel button in AlertForm alert.Canceled += new EventHandler<EventArgs>(buttonCancel_Click); alert.Show(); backgroundWorker1.RunWorkerAsync(); StartConversion(txtPath.Text.Trim(), Path.GetFileName(txtPath.Text.Trim())); } } #region Progress bar EVENTS private void buttonStart_Click(object sender, EventArgs e) { if (backgroundWorker1.IsBusy != true) { // create a new instance of the alert form alert = new AlertForm(); // event handler for the Cancel button in AlertForm alert.Canceled += new EventHandler<EventArgs>(buttonCancel_Click); alert.Show(); // Start the asynchronous operation. backgroundWorker1.RunWorkerAsync(); } } // This event handler cancels the backgroundworker, fired from Cancel button in AlertForm. private void buttonCancel_Click(object sender, EventArgs e) { if (backgroundWorker1.WorkerSupportsCancellation == true) { // Cancel the asynchronous operation. backgroundWorker1.CancelAsync(); // Close the AlertForm alert.Close(); } } // This event handler is where the time-consuming work is done. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; for (int i = 1; i <= 10; i++) { if (worker.CancellationPending == true) { e.Cancel = true; break; } else { // Perform a time consuming operation and report progress. worker.ReportProgress(i * 10); System.Threading.Thread.Sleep(500); } } } // This event handler updates the progress. private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { // Show the progress in main form (GUI) labelResult.Text = (e.ProgressPercentage.ToString() + "%"); // Pass the progress to AlertForm label and progressbar alert.Message = "In progress, please wait... " + e.ProgressPercentage.ToString() + "%"; alert.ProgressValue = e.ProgressPercentage; } // This event handler deals with the results of the background operation. private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled == true) { labelResult.Text = "Canceled!"; } else if (e.Error != null) { labelResult.Text = "Error: " + e.Error.Message; } else { labelResult.Text = "Done!"; } // Close the AlertForm alert.Close(); } #endregion 形式。如何禁用此行为?

2 个答案:

答案 0 :(得分:0)

尝试在用户点击时手动设置第一个显示的行:

dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;

MSDN

答案 1 :(得分:0)

我解决了问题。似乎问题出现了,因为DataGridView不是active Control。所以我只选择DataGridView - MouseEnter中的Event

private void dgvTaetigkeiten_MouseEnter(object sender, EventArgs e)
    {
        this.dgvTaetigkeiten.Select();
    }