How to make check all in datagridview? - vb.net

Hey i'm curently learning vb.net and i want to know how to check in data gridview when all the row will be checked if i have a checkbox outside. And how can i get the data from whe the row checked

To check all you need to setup a loop like this :
For Each Rw As DataGridViewRow In DataGridView1.Rows
DataGridView1.Item(0, Rw.Index).Value = True
'item(Col.index, Row.index) so you can set value on each cell of the datagrid
Next
to uncheck you juste avec to set the line to false
To get a value when a row is check, you need to use events. Here an exemple that give you the value of the second column when you check the first one :
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.ColumnIndex = 0 Then
MessageBox.Show("Checkbox changed ! The value of the second column is : " & DataGridView1.Item(1, e.RowIndex).Value)
End If
End Sub

Related

How to pass the 2nd selected row in datagridview to textbox?

How do i pass my 2nd selected row in datagridview to textboxt. I only know how to put the first data. How do i pass the 2nd selected to textbox?
Dim i As Integer
i = DataGridView2.SelectedRows(0).Index
Me.txtEmployeeID.Text = DataGridView2.Item(0, i).Value
you can use this code it worked for me
Dim test As String = DataGridView1.SelectedRows(0).Cells(2).Value.ToString
You need to only change the .cell(Here write the index value of the cell)
Then you can use the string value to fill up textbox
Try using the following:
Dim secondRow as integer = 0
Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
'assuming that by second selected row, you mean the row after the selected row
secondRow = e.RowIndex + 1
End Sub
Private Sub RowToTextBox()
Try
For i = 0 to DataGridView2.ColumnCount - 1
txtEmployeeID.Text &= DataGridView2.Item(i, secondRow)
Next
Catch ex as Exception
MsgBox("You have selected the final row")
End Try
End Sub
I don't think Me. is needed when referring to a textbox in the same form.
Since you don't know how many rows your user will select, I think looping thought the SelectedRows collection might work. I used the Name column because that is what I happened to have in my grid. Instead of a MessageBox you could add the values to a ListBox.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each GridRow As DataGridViewRow In DataGridView1.SelectedRows
MessageBox.Show($"Value is {GridRow.Cells("Name").Value}")
Next
End Sub

How to remove a row from datagridview and reset a particular column (vb.net 2005)

I am using VS 2005 edition. I am developing a winform desktop application. Let's say I have a unbound datagridview, with table like below:
Original datagrid before delete:
If I removed Stuff D (Item No 5), the "Item No" column supposed to reset itself accordingly. The expected output should be:
After delete row:
The "Item No" column is not an autonumber, it's just the number I assigned incrementally as the user add in a new row(new Stuff). I tried using the following code in rowremoved event but failed to achieve the expected output. Please help. Thanks.
If the goal is just showing the record index in cell, then you don't need to assign a value to cell and it's enough to set e.Value = e.RowIndex + 1 in CellFormatting event.
But based on your comment it seems you need those cells have values as well. So you need to handle RowsRemoved and RowsAdded event of DataGridView and refresh the cell value with row number:
Public Sub RefreshRowNumbers(g As DataGridView)
For Each r As DataGridViewRow In g.Rows
r.Cells(1).Value = r.Index + 1
Next
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, _
e As DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub
Private Sub DataGridView1_RowsAdded(sender As Object, _
e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub

dgv add row number to headercell

I'm trying to show the rownumber of a datagridview in the headercell I was under the impression that this was possible but I can't get it to show the value.
What do I need to do?
** The dgv is bound with BindingSource **
My attempt:
Private Sub NumberRows()
For Each oRow As DataGridViewRow In dgvBodyOverview.Rows
oRow.HeaderCell.Value = (oRow.Index + 1).ToString
Next
End Sub
To reproduce your problem, I followed your comment:
I'm calling it from a sub that loads all rows and at the end the call is made.
Doing this, the numbers failed to show whether or not the grid had a DataSource. Instead, I've typically done this as Plutonix suggested above - in DataGridView.CellFormatting.
Private Sub dataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
Dim header As DataGridViewRowHeaderCell = Me.dataGridView1.Rows(e.RowIndex).HeaderCell
If e.ColumnIndex = 0 Then
header.Value = [String].Format("{0}", e.RowIndex + 1)
End If
End Sub
I check for ColumnIndex = 0 to ensure the header cell value is only set once per row. See this C# answer for additional explanation.

Hide rows if column contains particular text/ string

I have a datagridview wich I have no bound datasource too I also have a button and three rows in my datagridview. If my column named STATUS contains the word CLOSED I would like to hide that entire row but i dont want to delete it just hide it.
If anyone woyuld like to know I am ussing VB.net
How can I do this?
If you are using a bound datasource you want to capture the DataGridView.DataSourceChanged event.
Would look like this.
Private Sub DataGridView1_DataSourceChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.DataSourceChanged
For Each row As DataGridViewRow In DirectCast(sender, DataGridView).Rows
If row.Cells("status").Value.ToString.ToLower.Contains("Closed") Then
row.Visible = False
End If
Next
End Sub
If you are not using a datasource you would want to capture the DataGridView.RowsAdded event.
Would look like this.
Private Sub DataGridView1_RowsAdded(sender As Object, e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
Dim dg As DataGridView = sender
If dg.Columns.Count > 0 And e.RowIndex <> 0 Then
Dim theRow As DataGridViewRow = dg.Rows(e.RowIndex)
If theRow.Cells("status").Value.ToString.ToLower.Contains("closed") Then
theRow.Visible = False
End If
End If
End Sub

Data grid view colomns search

I have a code which will allow me to search every row for a specific text. however I want to modify it so as to be able to search for a specific text in the currently selected column?
The code I'm trying to work with is as follows:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button2.Click
Dim rowindex As String
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells.Item("Column1").Value = TextBox2.Text Then
rowindex = row.Index.ToString()
MsgBox(rowindex)
Else
MsgBox("No Record found")
End If
Next
End Sub
You will need to use the SelectionChanged event of the datagrid. The arguments will point to the row that is selected and then you can iterate through the cells in that row and compare values to see if you have a match in the selected row.
If you have a fancy grid that allows more than one row to be selected you will need to loop through each selected row and each cell in those rows.
If you want to search only in selected cells, use the DataGridView.SelectedCells property.
For Each cell As DataGridViewCell In datagridview1.SelectedCells
If cell.Value.ToString() = TextBox2.Text Then
End If
Next
My VB is rusty, just add a null check for cell.Value before calling ToString().
Look for a column instead of looking through Column1 using DataGridView.CurrentCell property:
DataGridView.CurrentCell # MSDN
Code:
Dim currentcolumn As DataGridViewColumn =
DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex)
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells.Item(currentcolumn.Name).Value = TextBox2.Text Then
End If
Next