Delete Datagridview Cell(s) Selection - vb.net

What I want to do is select rows(0).cells(0) to rows(1).cells(6) in a datagridview (just an example, selection will change), press a button and then all cells in the selected range delete or equal "".
Any help will be appreciated. Thanks

Here is the code:
Private Sub clearpb_Click(sender As Object, e As EventArgs) Handles clearpb.Click
Dim myrowcount As Integer = Me.datagridviewentry.Rows.Count
Dim mycolcount As Integer = Me.datagridviewentry.Columns.Count
a = 0
Do While a < myrowcount
b = 0
Do While b < mycolcount
If Me.datagridviewentry.Rows(a).Cells(b).Selected = True Then
Me.datagridviewentry.Rows(a).Cells(b).Value = ""
End If
b += 1
Loop
a += 1
Loop
End Sub

Related

How to count selected items in the listbox by just select and see result in a label?

getting the logic of counting is easy but in practice it gets hard sometimes.
Now I've a list with many items on it. how to count those items if there were repeated and i want to convert that to a number using the FOR loop since i know how many items in the list.
I tried some codes but i did not succeed
'''''''''''''''
' VB 2015
''''''''''''
Public Class Form1
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim count As Integer = 0
Dim strselection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
For i As Integer = 0 To lstWinners_List.Items.Count - 1
If lstWinners_List.Items(i) = strselection Then
count = count + 1
End If
Next
lblOutput.Text = count.ToString
End If
End Sub
End Class
for EX:
i wanna count the word "michigan " how many times repeated in the list by just clicking on it ?
Here's an example using Jim Hewitt's comment:
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim selection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
Dim wins As Integer = (From team As String In lstWinners_List.Items Where team.Equals(selection)).Count
lblOutput.Text = wins.ToString
End If
End Sub
Edit
Here's an equivalent, manual, indexed for loop:
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim count As Integer = 0
Dim selection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
For i As Integer = 0 To lstWinners_List.Items.Count - 1
If lstWinners_List.Items(i) = selection Then
count = count + 1
End If
Next
lblOutput.Text = count.ToString
End If
End Sub
Dim count As Integer = 0
For Each n As String In ListBox1.Items
If n = "red" Then
count += 1
End If
Next
lblOutput.Text(count)
something like this do you mean?

Automatically increment selected row through DataGridView

I am trying to update a selected row to a number and increment every other row after that by 1 from the newly updated value, leaving all previous rows unaffected
I have the selected row to update figured out:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim i = DataGridView1.CurrentRow.Index
With DataGridView1
.Rows(i).Cells("Value").Value = NumTextbox.Text
End With
I tried adding:
.Rows(i).Cells("Value").Value = i + 1
But only labels the value to the number of the row, can anyone point me in the right direction?
Putting it all together...
Private Sub IncrementRows()
Dim i = DataGridView1.CurrentRow.Index
Dim num As Integer
If Integer.TryParse(NumTextbox.Text, num) Then
With DataGridView1
.Rows(i).Cells("Value").Value = num
End With
For j As Integer = i + 1 To DataGridView1.Rows.Count - 1
num += 1
DataGridView1.Rows(j).Cells("Value").Value = num
Next
Else
MessageBox.Show("Please enter a valid number.")
NumTextbox.Focus
End If
End Sub
For j As Integer = i + 1 to DataGridView1.Rows.Count - 1
DataGridView1.Rows(j).Cells("Value").Value = i + 1
next

Index out of range error when selecting any row other than 1st

I'm getting the "Index out of range error", when double clicking the datagridview any row other than 1st. Double clicking 1st row gives the value. Please help
My code as follows:
Private Sub dgv_searchassistant_CellDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_searchassistant.CellDoubleClick
Dim rowindex As Integer = dgv_searchassistant.CurrentCell.RowIndex
Dim columnindex As Integer = dgv_searchassistant.CurrentCell.ColumnIndex
TextBox30.Text = dgv_searchassistant.Rows(rowindex).Cells(columnindex).Value
TextBox27.Text = dgv_searchassistant.Rows(rowindex).Cells(2).Value
dgv_searchassistant.Visible = False
dgv_searchassistant.AutoGenerateColumns = False
dgv_searchassistant.DataSource = Nothing
End Sub

Specify which button to run on event vb.net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim rows, column, j, i
rows = 4
column = 13
For j = 0 To rows - 1
Dim r As New TableRow()
For i = 0 To column - 1
Dim c As New TableCell()
If i = 0 And j = 0 Then
c.Text = "TIME/COURT"
r.Cells.Add(c)
ElseIf i = 0 And j = 1 Then
c.Controls.Add(New LiteralControl("Court A"))
r.Cells.Add(c)
ElseIf i = 0 And j = 2 Then
c.Controls.Add(New LiteralControl("Court B"))
r.Cells.Add(c)
ElseIf i = 0 And j = 3 Then
c.Controls.Add(New LiteralControl("Court C"))
r.Cells.Add(c)
ElseIf i >= 1 And j = 0 Then
c.Controls.Add(New LiteralControl(x & "-" & x + 1))
r.Cells.Add(c)
x += 1
Else
btn = New Button
btn.ID = "btnr" & j & "c" & i
AddHandler btn.Click, AddressOf Change_Colour
c.Controls.Add(btn)
r.Cells.Add(c)
End If
Next
booktable.Rows.Add(r)
Next
End Sub
Protected Sub Change_Colour(sender As Object, e As EventArgs)
btn.BackColor = Drawing.Color.LightGreen
End Sub
When I click on any of the button, only the last button turns light green.
How can I set the button to run the event when I click it?
Can I call the button by button.ID? How?
If multiple buttons can call your Change_Colour routine them you will need to actually determine which one did call it. so you will need something like:
Protected Sub Change_Colour(sender As Object, e As EventArgs)
Dim btn as Button = Ctype(sender, Button)
'here you will need to determine which button is the sender which you could do by checking its name for example
'eg Select case btn.name
'case add stuff here
'end select
btn.BackColor = Drawing.Color.LightGreen
End Sub

Can someone tell what's wrong with that code?

this button is to check if there is an empty cells in the last row .
if there is filled cell the button (confirmer) will be enabled .. the application crashes when i debug
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
DataGridView1.AllowUserToAddRows = "true"
DataGridView1.ClearSelection()
DataGridView1.Rows(DataGridView1.NewRowIndex).Selected = True
Dim c As Boolean
Dim D As String
Dim nbrcell As Integer = DataGridView1.CurrentRow.Cells.Count - 1
c = Confirmer.Enabled
Do
For i As Integer = 0 To nbrcell - 1
D = DataGridView1.SelectedRows(0).Cells(i).Value
If D <> "" Then
c = True
End If
Next
Loop Until c = True
End Sub
I have changed your code like this:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
DataGridView1.AllowUserToAddRows = "true"
DataGridView1.ClearSelection()
DataGridView1.Rows(DataGridView1.NewRowIndex).Selected = True
Dim c As Boolean
Dim D As Object
Dim nbrcell As Integer = DataGridView1.CurrentRow.Cells.Count - 1
c = Confirmer.Enabled
Do
For i As Integer = 0 To nbrcell - 1
D = DataGridView1.SelectedRows(0).Cells(i).Value
If Not IsDBNull(D) Then
c = True
End If
Next
Loop Until c = True
End Sub
Hope it helps
You are trying to read the number of cells from the CurrentRow property.
But if there is no CurrentRow set when you run this code then the code crashes with a Null Reference Exception. (A.K.A. Object Reference not set to an instance of an object)
Instead you should use the row at NewRowIndex.
Dim nbrcell As Integer = DataGridView1.Rows(DataGridView1.NewRowIndex).Cells.Count
and don't subtract 1 from the count of cells. You are doing this two times. One when you get the Count and one more time when you loop over that count (So the last cell is not checked)
Now the loop: You never exit from that loop if the cells are all filled
Dim D as Object
Dim allFilled = True
For i As Integer = 0 To nbrcell - 1
D = DataGridView1.SelectedRows(0).Cells(i).Value
If D Is Nothing OrElse
Convert.IsDBNull(D) OrElse
String.IsNullOrWitheSpace(D.ToString()) Then
allFilled = False
Exit For ' no need to continue the loop
End If
Next
Confirmer.Enabled = allFilled
I have removed the external while, the the loop is executed only using the for and it is stopped as soon as a cell with Nothing, DBNull or empty string value is found. At that point a boolean variable is set to false and applied to the Enabled property of the button. If no cells are found to be invalid then the boolen variable remains set to true and the button will be enabled.
Probably it is possible to change something in the evaluation code of the cell, but this depends on how do you fill the grid (manually or through a database data)
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Confirmer.Visible = True
Confirmer.Enabled = False
DataGridView1.AllowUserToAddRows = "true"
DataGridView1.ClearSelection()
DataGridView1.Rows(DataGridView1.NewRowIndex).Selected = True
Dim c As Boolean = False
Dim D As Object
Dim nbrcell As Integer = DataGridView1.CurrentRow.Cells.Count
Do
For i As Integer = 0 To nbrcell - 1
D = DataGridView1.Rows(DataGridView1.NewRowIndex).Cells(i).Value
If Not IsDBNull(D) Then
Confirmer.Enabled = True
c = True
End If
Next
Loop Until c = True
End Sub
The Problem now is that the button(confirmer) is always enabled even is the cell is empty