开发者

How to set the pointer to the current row in the Datagridview in C#

开发者 https://www.devze.com 2022-12-24 03:51 出处:网络
I have a datagridview on th开发者_高级运维e Windows Form in c#. I am updating and fillingthe table adapter on the cell event. The problem is that When I go to lower half of the datagrid which is not v

I have a datagridview on th开发者_高级运维e Windows Form in c#. I am updating and filling the table adapter on the cell event. The problem is that When I go to lower half of the datagrid which is not visible until I scroll down, as I click any cell, the table adapter is filled and updated and the pointerpoints to the very first row. Any suggestion on how to fix it. I have a ideas to record the top row of the datagridview that is visible and set the pointer to that row. But how to do it?


Before the fill happens, save the current cell:

 DataGridViewCell selectedCell = dgv.CurrentCell;

Then after the fill, set it back:

 dgv.CurrentCell = selectedCell;

If you don't care which cell was selected but just want to scroll the view back to where it was before the fill, you can use the FirstDisplayedCell property instead.

I believe these will work but I haven't used them in your exact scenario (which sounds kinda funky).

Incidentally, there a lot of events on the DataGridView, which gives great flexibility, but you have to read the docs carefully to understand all your options.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号