当DataGridView正在更新时,滚动每次都会上升

时间:2018-11-13 09:21:35

标签: c# winforms datagridview scrollbar

我在Windows窗体应用程序中有一个gridView。当我们向数据库添加新行时,我必须更新它的内容。我使用这种方式来添加新行并对其进行更新。但是,当我有无法容纳在页面中的内容时,可以看到滚动。当我滑动页面的滚动底部并同时更新时,滚动到达页面的顶部,我该如何解决该问题。

private void fillhistoryOfUpdateProcessDataGridView()
{
    dt = db.getHistoryOfUpdatedProcess();    

    dgwHistoryofProcess.Invoke(new Action(() => dgwHistoryofProcess.DataSource = dt));

    dgwHistoryofProcess.Invoke(new Action(() => dgwHistoryofProcess.Columns["islemAdi"].HeaderText = "İşlem Adı"));

    dgwHistoryofProcess.Invoke(new Action(() => dgwHistoryofProcess.Columns["islemBaslangicTarihi"].HeaderText = "İşlem Başlangıç Tarihi"));
    dgwHistoryofProcess.Invoke(new Action(() => dgwHistoryofProcess.Columns["islemBitisTarihi"].HeaderText = "İşlem Tamamlanma Tarihi"));
    dgwHistoryofProcess.Invoke(new Action(() => dgwHistoryofProcess.Columns["islemBitisTarihi"].ValueType = typeof(DateTime)));               
    dgwHistoryofProcess.Sort(dgwHistoryofProcess.Columns["islemBitisTarihi"], ListSortDirection.Descending);
    dgwHistoryofProcess.Invoke(new Action(() => dgwHistoryofProcess.Columns["islemDurumu"].HeaderText = "İşlem Durumu"));
    dgwHistoryofProcess.Invoke(new Action(() => dgwHistoryofProcess.Columns["hataLog"].HeaderText = "Hata Sebebi"));

    dgwHistoryofProcess.Invoke(new Action(() => dgwHistoryofProcess.Update()));

    dt.Dispose();
}

private void updateHistoryofUpdatedProcessDataGridView()
{
    dt = db.GuncellemeIslemGecmisiGetir();

    dgwIslemGecmisi.Invoke(new Action(() => dgwIslemGecmisi.DataSource = dt));

    dgwIslemGecmisi.Invoke(new Action(() => dgwIslemGecmisi.Update()));
    dt.Dispose();
}

1 个答案:

答案 0 :(得分:0)

我建议在更新之前,先找到DataGridView的当前行。然后在更新后将位置再次设置为该记录。如果您同时使用bindingSource,则会更容易。伪代码是这样的:

private void updateHistoryofUpdatedProcessDataGridView()
{
    var current = bindingSource.Current as typeOfData

    // do the update


    int index = bindingSource.Find("ID", current.ID);
    bindingSource.Position = index;

}

但是我想应该有更好的方法,所以我关注这个问题以寻求其他答案。