DataGridView visible cells

K

Kevin S Gallagher

I am looking for advice/code to assist in determining which cells are
visible on a DGV. The code below works but wanted to see if there are better
methods or perhaps the code below has issues I have not thought about.

Thanks,
Kevin


Dim x As Integer = 0
While x < dataGridView1.Rows.Count
Dim y As Integer = 0
While y < dataGridView1.Rows(x).Cells.Count
Dim c As DataGridViewCell = dataGridView1.Rows(x).Cells(y)
If c.Displayed Then
If Not c.Value Is DBNull.Value Or Nothing Then
Console.WriteLine("Row={0} Value={1}", x.ToString,
dataGridView1.Rows(x).Cells(0).Value)
End If
End If
System.Math.Min(System.Threading.Interlocked.Increment(y), y -
1)
End While
System.Math.Min(System.Threading.Interlocked.Increment(x), x - 1)
End While
 
K

Kevin S Gallagher

Okay I believe this is best

''' <summary>
''' Used to get displayed data on a DataGridView
''' </summary>
''' <param name="GridView">Grid to get data from</param>
''' <param name="PartialRow">Display partial rows or not</param>
''' <param name="Partialcolumn">Display partial columns or not</param>
''' <remarks></remarks>
Sub GetGridViewDisplayedCells(ByVal GridView As
Windows.Forms.DataGridView, Optional ByVal PartialRow As Boolean = False,
Optional ByVal Partialcolumn As Boolean = False)
Dim intRowIndex As Integer = GridView.FirstDisplayedScrollingRowIndex

While intRowIndex < GridView.DisplayedRowCount(PartialRow)
Dim intColumnIndex As Integer = 0
While intColumnIndex < GridView.DisplayedColumnCount(Partialcolumn)
Dim CurrentCell As DataGridViewCell =
GridView.Rows(intRowIndex).Cells(intColumnIndex)
If Not CurrentCell.Value Is DBNull.Value Or Nothing Then
' do what we came for
End If
System.Math.Min(System.Threading.Interlocked.Increment(intColumnIndex),
intColumnIndex - 1)
End While
System.Math.Min(System.Threading.Interlocked.Increment(intRowIndex),
intRowIndex - 1)
End While
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top