[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
Re: [2005] Label didn't count when selected dgv rows
What is your DGV's SelectionMode property set to?
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
Re: [2005] Label didn't count when selected dgv rows
Hi,
Here's the code to count rows in a datagridview;
vb 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 +1
End If
End Sub
Wkr,
sparrow1
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:
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 +1
End If
End Sub
Wkr,
sparrow1
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
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
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
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
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
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
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
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....:blush:
Hope this helps some! :wave:
D
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.
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
Re: [2005] Label didn't count when selected dgv rows
Well then Post #11 will answer your question.
D
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.
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
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
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
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