Results 1 to 21 of 21

Thread: [RESOLVED] [2005] Label didn't count when selected dgv rows

  1. #1

    Thread Starter
    Banned
    Join Date
    May 2008
    Posts
    461

    Resolved [RESOLVED] [2005] Label didn't count when selected dgv rows

    Hi guys

    I would like to know how I can make a single click and if i select more than one row, label should be counting the value how many rows i have selected.



    Here it the code:

    Code:
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            If Me.DataGridView1.SelectedCells.Count >= 1 Then
                Label19.Text = DataGridView1.SelectedRows.Count
            End If
        End Sub
    
        Private Sub DataGridView1_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
            DataGridView1.ClearSelection()
    
            If DataGridView1.SelectedCells.Count <= 0 Then
                Label19.Text = "0"
            End If
        End Sub


    When I select the row and when I select more than one row, label didn't counting the rows i have selected. Please let me know how i can resolve it??



    Thanks,
    Mark

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Label didn't count when selected dgv rows

    What is your DGV's SelectionMode property set to?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Banned
    Join Date
    May 2008
    Posts
    461

    Re: [2005] Label didn't count when selected dgv rows

    My DGV SelectionMode property are set to FullRowSelect. Any idea how the label can start to count when I click more than one row??



    Thanks,
    Mark

  4. #4
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Label didn't count when selected dgv rows

    Hi,

    Here's the code to count rows in a datagridview;

    vb Code:
    1. Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    2.         If Me.DataGridView1.SelectedCells.Count >= 1 Then
    3.             Label19.Text = DataGridView1.SelectedRows.Count +1
    4.         End If
    5.     End Sub

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  5. #5

    Thread Starter
    Banned
    Join Date
    May 2008
    Posts
    461

    Re: [2005] Label didn't count when selected dgv rows

    Hi

    Thanks for your reply, i have paste the code on my form and I have debug the project to see if there is a change but unfortunately, when I click the cell with a single click and I select more than one cell like 10 cells, label did not count up.



    Any idea how to get this situation resolve??

    Thanks,
    Mark

    Quote Originally Posted by sparrow1
    Hi,

    Here's the code to count rows in a datagridview;

    vb Code:
    1. Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    2.         If Me.DataGridView1.SelectedCells.Count >= 1 Then
    3.             Label19.Text = DataGridView1.SelectedRows.Count +1
    4.         End If
    5.     End Sub

    Wkr,

    sparrow1

  6. #6
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Label didn't count when selected dgv rows

    Hi,

    The code I've gave you, works for me!
    What does the label gives you when you debug and click a row?
    The label in my test app gives me directly 2

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  7. #7

    Thread Starter
    Banned
    Join Date
    May 2008
    Posts
    461

    Re: [2005] Label didn't count when selected dgv rows

    It didn't work for me when I click the cell and select more than one cell.
    The label stays 1 when i click the cell and select more than one.



    Any idea how to fix??


    Thanks,
    Mark

  8. #8
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: [2005] Label didn't count when selected dgv rows

    Are you clicking a cell in the same row as one of the other ones? If you do not have fullrowselect on and it only gives you a single cell then that is the problem. the SelectedRows Property will only count the rows that have selected items in the datagridview.

    If you are counting individual cells try (without fullrowselect on):
    Code:
     DataGridView1.SelectedCells.Count.ToString
    You will know if you have fullrowselect on by clicking on a cell, if the entire row highlights then you have fullrowselect on.

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  9. #9
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Label didn't count when selected dgv rows

    Hi,

    What you can try is:

    Make a new private sub Datagridview1_CellClick and try again.
    Your very sure that the selectionmode is set to FullRowSelectand you didn't change anything else.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  10. #10

    Thread Starter
    Banned
    Join Date
    May 2008
    Posts
    461

    Re: [2005] Label didn't count when selected dgv rows

    Thanks for your reply dminder, do i have to input the code the one you post, do i have to put on cell click event??



    Thanks,
    Mark


    Quote Originally Posted by dminder
    Are you clicking a cell in the same row as one of the other ones? If you do not have fullrowselect on and it only gives you a single cell then that is the problem. the SelectedRows Property will only count the rows that have selected items in the datagridview.

    If you are counting individual cells try (without fullrowselect on):
    Code:
     DataGridView1.SelectedCells.Count.ToString
    You will know if you have fullrowselect on by clicking on a cell, if the entire row highlights then you have fullrowselect on.

    D

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Label didn't count when selected dgv rows

    Quote Originally Posted by Mark107
    My DGV SelectionMode property are set to FullRowSelect. Any idea how the label can start to count when I click more than one row??



    Thanks,
    Mark
    If that is the case then all you need to do is to get the count of the SelectedRows. There's no need to count the selectedcells.
    Code:
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            Label19.Text = DataGridView1.SelectedRows.Count.ToString()
            Label19.Refresh()
        End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  12. #12

    Thread Starter
    Banned
    Join Date
    May 2008
    Posts
    461

    Re: [2005] Label didn't count when selected dgv rows

    Thanks for your reply, but it is still the same where it was when I click the cell and select more than one row that the label are still showing 1 cell that have been selected. I have selected more than 10 cells but it didn't show up any update.



    I don't know how we can situation this and get it into resolve. Any idea??


    Thanks,
    Mark

  13. #13
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: [2005] Label didn't count when selected dgv rows

    Ok, I just tested this. If you use the CellClick Event and capture the SelectedCells.Count Property you will get an accurate count of the number of cells selected even if they are in the same row. Here is my exact code:
    Code:
    Private Sub dgvDetails_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDetails.CellClick
            lblCellsSelected.Text = dgvDetails.SelectedCells.Count.ToString & " Cells Selected"
        End Sub
    The datagridview's (dgvDetails) properties for selection are Multiselect = True and SelectionMode is CellSelect.

    I think some of the confusion in this post has come in as to whether you want just the selected cell count (how many cells are actually selected total regardless of how many rows they are in) or the selected rowcount (how many rows have cells selected in them) which is where the whole fullrowselect bit came in. My understanding is that you want a count of however many cells are currently selected not how many rows have selected items in them. Hopefully I am accurate in this otherwise I am the fool and need to sit in a corner with a dunce hat on for the day....

    Hope this helps some!

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  14. #14
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Label didn't count when selected dgv rows

    Quote Originally Posted by Mark107
    Thanks for your reply, but it is still the same where it was when I click the cell and select more than one row that the label are still showing 1 cell that have been selected. I have selected more than 10 cells but it didn't show up any update.



    I don't know how we can situation this and get it into resolve. Any idea??


    Thanks,
    Mark
    Are you counting the selected cells or are you counting the selected rows of the DGV? In the 1st post you wrote: "if i select more than one row, label should be counting the value how many rows i have selected..." and now you are saying that you want to count the selected cells... The 2 things are totally different, and please make up your mind as to which way you want to go.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  15. #15

    Thread Starter
    Banned
    Join Date
    May 2008
    Posts
    461

    Re: [2005] Label didn't count when selected dgv rows

    I want to counting the selected rows of the DGV. I am sorry for posting the wrong situation. Hope this is the right one that you can assist me with this.



    Thanks,
    Mark

  16. #16
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: [2005] Label didn't count when selected dgv rows

    Well then Post #11 will answer your question.

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  17. #17
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Label didn't count when selected dgv rows

    Mark, probably we all misunderstand what you said you want. So let's back out a little bit and try to settle on our understanding of the problem. You have a DGV with SelectionMode set to FullRowSelect (that is, if you click on any cell on a row, that whole row is highlighted indicating that it's selected). Now suppose that you click on a cell in row#1, the label show 1 row selected and the is only one row highlighted in your DGV, right... So far so good. Now you click on a cell in row#2. Again, row#2 is highlighted and it still the only highlighted row in the DGV (because when you click on this row, the 1st row is automatically unselected)... So unless you hold down the shift key or control key while clicking on another row, and your DGV is allowed for multiselection, there is only 1 selected row at any given time, and that selected row is the row that you last clicked on. And that's the reason why your label always shows 1 row selected.

    If you want that label to count up each time a row is selected then you have to say so, otherwise we all try to solve the wrong puzzle for nothing.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  18. #18

    Thread Starter
    Banned
    Join Date
    May 2008
    Posts
    461

    Re: [2005] Label didn't count when selected dgv rows

    I have tried it but it still unresolved.


    Please help!!!!!!!!!!

    Quote Originally Posted by dminder
    Well then Post #11 will answer your question.

    D

  19. #19
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] Label didn't count when selected dgv rows

    Hey,

    Did you read stanav's post, #17.

    Is this the situation that you desire? Can you specify exactly what you want to achieve?

    Gary

  20. #20

    Thread Starter
    Banned
    Join Date
    May 2008
    Posts
    461

    Re: [2005] Label didn't count when selected dgv rows

    I am trying to achieve by selected selected one row of the DGV then select more rows while label is counting the rows i select.


    Thanks,
    Mark

  21. #21
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] Label didn't count when selected dgv rows

    So can you explain why the code that was posted in post #11 does not do what you want?

    It would appear to me that it should.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width