DataGridView RowCount vs Rows.Count

StewartBW 745 Reputation points
2024-05-24T14:28:36.5866667+00:00

Hello,

RowCount: Gets or sets the number of rows displayed in the DataGridView

Rows.Count: Returns the number of rows

What does it mean, when they can have different values?

The only condition I can imagine is if a row is invisible, then RowCount does not count it, anything else that can cause different values?

Thanks.

  • I need to check if all rows are selected, so don't know which one is a better way to go?
DataGridView.SelectedRows.Count = DataGridView.Rows.Count
vs
DataGridView.SelectedRows.Count = DataGridView.RowCount
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,573 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,642 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Dewayne Basnett 1,361 Reputation points
    2024-05-24T16:08:32.02+00:00

    I constructed this to find out what happens if RowCount is changed. See remarks here.

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    
        'sample data
    
        DataGridView1.ColumnCount = 4
    
        DataGridView1.ColumnHeadersVisible = True
    
        ' Set the column header style.
    
        Dim columnHeaderStyle As New DataGridViewCellStyle()
    
        columnHeaderStyle.BackColor = Color.Beige
    
        columnHeaderStyle.Font = New Font("Verdana", 10, FontStyle.Bold)
    
        DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle
    
        ' Set the column header names.
    
        DataGridView1.Columns(0).Name = "Recipe"
    
        DataGridView1.Columns(1).Name = "Category"
    
        DataGridView1.Columns(2).Name = "Main Ingredients"
    
        DataGridView1.Columns(3).Name = "Rating"
    
        ' Populate the rows.
    
        Dim row1() As String = {"Meatloaf", "Main Dish", "ground beef", "**"}
    
        Dim row2() As String = {"Key Lime Pie", "Dessert", "lime juice, evaporated milk", "****"}
    
        Dim row3() As String = {"Orange-Salsa Pork Chops", "Main Dish", "pork chops, salsa, orange juice", "****"}
    
        Dim row4() As String = {"Black Bean and Rice Salad", "Salad", "black beans, brown rice", "****"}
    
        Dim row5() As String = {"Chocolate Cheesecake", "Dessert", "cream cheese", "***"}
    
        Dim row6() As String = {"Black Bean Dip", "Appetizer", "black beans, sour cream", "***"}
    
        Dim rows() As Object = {row1, row2, row3, row4, row5, row6}
    
        Dim rowArray As String()
    
        For Each rowArray In rows
    
            DataGridView1.Rows.Add(rowArray)
    
        Next rowArray
    
    End Sub
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Debug.WriteLine(DataGridView1.Rows.Count)
    
        DataGridView1.RowCount = DataGridView1.Rows.Count - 2
    
        Debug.WriteLine(DataGridView1.Rows.Count)
    
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
        Debug.WriteLine(DataGridView1.Rows.Count)
    
        DataGridView1.RowCount = DataGridView1.Rows.Count + 3
    
        Debug.WriteLine(DataGridView1.Rows.Count)
    
    End Sub
    

  2. KOZ6.0 6,300 Reputation points
    2024-05-24T17:15:47.0333333+00:00

    The content of the question has changed. If there are hidden rows, if all rows are selected it should look like this:

    Public Shared Function IsFullRowSelected(view As DataGridView) As Boolean
        For Each row As DataGridViewRow In view.Rows
            If row.Visible AndAlso Not row.Selected Then
                Return False
            End If
        Next
        Return True
    End Function
    
    0 comments No comments

  3. Karen Payne MVP 35,291 Reputation points
    2024-05-24T20:59:10.5833333+00:00

    The best approach to working with a DataGridView is to have a DataSource such as a DataTable or a List that is set to the DataSource of a BindingSource which then becomes the DataSource to the DataGridView. Doing this assist in other things besides what you are after, mainly going this way means you can work with data in 99 percent of the time by asking a question e.g. if the data source is a DataTable to get at the current row case Current property of the current row to a DataRowView then to a DataRow.

    For your question, I have a DataGridView named ProductsDataGridView and a BindingSource named _mainBindingSource. To see if all rows are selected use.

    if (ProductsDataGridView.SelectedRows.Count == _mainBindingSource.Count)
    {
        // do something
    }
    

    Or

    If ProductsDataGridView.SelectedRows.Count = _mainBindingSource.Count Then
    	' do something
    End If
    
    
    0 comments No comments