In my C# (2010) application I have a DataGridView in Virtual Mode which holds several thousand rows. Is it possible to find out which cells are onscreen at the moment?
Just set the CurrentCell property, the DGV will scroll to make it visible.
If you populate the DataGridView with a DataSet table you can use the . GetChanges method to find out which rows, if any, have changed.
The DataGridViewRow class represents a row in a DataGridView control. You can retrieve rows through the Rows and SelectedRows collections of the control. Unlike a DataGridViewColumn, a DataGridViewRow physically contains a collection of all of the cells in that row.
public void GetVisibleCells(DataGridView dgv)
{
var visibleRowsCount = dgv.DisplayedRowCount(true);
var firstDisplayedRowIndex = dgv.FirstDisplayedCell.RowIndex;
var lastvisibleRowIndex = (firstDisplayedRowIndex + visibleRowsCount) - 1;
for (int rowIndex = firstDisplayedRowIndex; rowIndex <= lastvisibleRowIndex; rowIndex++)
{
var cells = dgv.Rows[rowIndex].Cells;
foreach (DataGridViewCell cell in cells)
{
if (cell.Displayed)
{
// This cell is visible...
// Your code goes here...
}
}
}
}
Updated: It now finds visible cells.
private bool RowIsVisible(DataGridViewRow row)
{
DataGridView dgv = row.DataGridView;
int firstVisibleRowIndex = dgv.FirstDisplayedCell.RowIndex;
int lastVisibleRowIndex = firstVisibleRowIndex + dgv.DisplayedRowCount(false) - 1;
return row.Index >= firstVisibleRowIndex && row.Index <= lastVisibleRowIndex;
}
IMHO Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With