Datagridview selections with vb.net - vb.net

Help pls! I want to automatically select all the rows that has the same value in a particular cell of every rows when the user manually select or click on a particular row. am using vb.net. Thanks.

Although the question is not clear enough, you can try:
Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.RowIndex <> -1 Then
If DataGridView1.CurrentCell IsNot Nothing AndAlso DataGridView1.CurrentCell.Value IsNot Nothing Then
findOnGridview(DataGridView1, DataGridView1.CurrentCell.Value.ToString(), DataGridView1.CurrentCell.ColumnIndex)
End If
End If
End Sub
Private Sub findOnGridview(g As DataGridView, s As String, c As Integer)
Dim counter As Integer = 0
For i As Integer = 0 To g.Rows.Count - 1
If g.Rows(i).Cells(c).Value = s Then
g.Rows(i).Cells(c).Style.BackColor = Color.Yellow
Else
g.Rows(i).Cells(c).Style.BackColor = Color.White
End If
Next
End Sub

Related

VB.NET automatically populate DataGridView column cell with DateTime.Now

I have a VB.NET DataGridView that requires user input to fill Null Columns.
I have that functioning.
I want to have the date_time column populate with the current date/time when the other columns are not NULL. The goal being to have an accurate time attached to when the other cells were populated.
I expect that I can use a timer and monitor the cells not to be null and trigger the date/time being added but I am not sure how to code the date/time population to the cell.
Can anyone give me a push in the right direction?
Thanks
This should give you a good start:
Dim dateTimColumnIndex As Integer = 1 '(example number on my part)
Private Sub CellValueChanged(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellValueChanged
If (e.ColumnIndex <> dateTimColumnIndex) Then
For z As Integer = 0 To DataGridView1.ColumnCount -1
If DataGridView1.Item(z, e.RowIndex).Value Is Nothing Then
Return
End If
DataGridView1.Rows(e.RowIndex).Cells(dateTimColumnIndex).Value = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
Next
End if
End Sub
Updated won't do anything until you change value of allDoneLoading to True
Dim allDoneLoading As Boolean = False
Dim dateTimColumnIndex As Integer = 1 '(example number on my part)
Private Sub CellValueChanged(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellValueChanged
If allDoneLoading = True Then
If (e.ColumnIndex <> dateTimColumnIndex) Then
For z As Integer = 0 To DataGridView1.ColumnCount - 1
If DataGridView1.Item(z, e.RowIndex).Value Is Nothing Then
Return
End If
DataGridView1.Rows(e.RowIndex).Cells(dateTimColumnIndex).Value = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
Next
End If
End If
End Sub

vb.net DexExpress winform GridControl click column header select cell

I use GridControl with Gridview to display the data. I realized I click the column header is sorting the data instead of select all cell under the column header. May I know how to handle this? I have a sample code (below) but it use BandedGridview.
Private Sub bandedGridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles bandedGridView1.MouseDown
If Control.ModifierKeys <> (Keys.Shift Or Keys.Control) Then
Return
End If
Dim view As BandedGridView = CType(sender, BandedGridView)
Dim hInfo As BandedGridHitInfo = view.CalcHitInfo(e.Location)
If hInfo.InColumn Then
view.ClearSelection()
SelectCells(hInfo.Column)
ElseIf hInfo.InBandPanel AndAlso hInfo.Band IsNot Nothing Then
view.ClearSelection()
SelectCells(hInfo.Band)
Else
Return
End If
CType(e, DXMouseEventArgs).Handled = True
End Sub
Private Sub SelectCells(ByVal column As BandedGridColumn)
For i As Integer = 0 To column.View.RowCount - 1
column.View.SelectCell(i, column)
Next i
End Sub
Private Sub SelectCells(ByVal band As GridBand)
For Each column As BandedGridColumn In band.Columns
SelectCells(column)
Next column
End Sub
I need GridView only, anyone can help?
If you want to use this code for GridView then you can just remove the word Banded from everywhere and remove anything that belongs to GridBand. For SelectedCells method you need to convert column.View to GridView. Also I suggest you to add GridView.BeginSelection and GridView.EndSelection methods into SelectedCells method.
Here is example:
Imports DevExpress.Utils
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
'...
Private Sub gridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles gridView1.MouseDown
If Control.ModifierKeys <> (Keys.Shift Or Keys.Control) Then
Return
End If
Dim view As GridView = CType(sender, GridView)
Dim hInfo As GridHitInfo = view.CalcHitInfo(e.Location)
If hInfo.InColumn Then
view.ClearSelection()
SelectCells(hInfo.Column)
Else
Return
End If
CType(e, DXMouseEventArgs).Handled = True
End Sub
Private Sub SelectCells(ByVal column As GridColumn)
Dim view As GridView = CType(column.View, GridView)
view.BeginSelection()
For i As Integer = 0 To view.RowCount - 1
view.SelectCell(i, column)
Next i
view.EndSelection()
End Sub

How to edit a cell in a Datagridview that is bound to a bindingsource

I have a datagridview that is updated when the bindingsource is updated. After the last row that has actual data there should be an empty row. If the user selects a cell in this row they should be able to type data into that cell (as if it were a textbox).
I have set the "AllowUserToAddRows" property of the datagridview to "true"; this adds a blank row after the last row with data. However, when the user clicks the row, the cells are populated with the default value for the field (zero, in my case).
Since the properties that I have bound the datagridview columns to are of type double, it seems like I can not display any non numeric values.
So first, is it possible to either leave the cells blank or display an empty string when they are clicked. Second, how can I allow the user to enter data into certain cells?
Try using the CellFormatting event:
Private Sub dgv_CellFormatting(sender As Object, _
e As DataGridViewCellFormattingEventArgs) _
Handles dgv.CellFormatting
If (e.ColumnIndex = 0) Then
If e.Value IsNot Nothing AndAlso DirectCast(e.Value, Double) = 0 Then
e.Value = String.Empty
e.FormattingApplied = True
End If
End If
End Sub
The Double type isn't great for comparisons. Consider using a Decimal type instead.
Trying to keep the cells "blank" for the new row until they were "touched" by the user requires hacking around with a flag on each cell of the new row, so here is an example using the Tag property:
Dim rowEditing As Integer = -1
Private Sub dgv_RowEnter(sender As Object, _
e As DataGridViewCellEventArgs) _
Handles dgv.RowEnter
If e.RowIndex = dgv.NewRowIndex Then
rowEditing = e.RowIndex
For i As Integer = 0 To dgv.Columns.Count - 1
dgv.Rows(rowEditing).Cells(i).Tag = "tagged"
Next
End If
End Sub
Private Sub dgv_RowLeave(sender As Object, _
e As DataGridViewCellEventArgs) _
Handles dgv.RowLeave
If rowEditing > -1 Then
For i As Integer = 0 To dgv.Columns.Count - 1
dgv.Rows(rowEditing).Cells(i).Tag = String.Empty
Next
rowEditing = -1
End If
End Sub
Private Sub dgv_CellEndEdit(sender As Object, _
e As DataGridViewCellEventArgs) _
Handles dgv.CellEndEdit
If e.RowIndex = rowEditing AndAlso e.ColumnIndex = 1 Then
dgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Tag = String.Empty
End If
End Sub
Private Sub dgv_CellFormatting(sender As Object, _
e As DataGridViewCellFormattingEventArgs) _
Handles dgv.CellFormatting
If e.RowIndex = rowEditing AndAlso e.ColumnIndex = 1 Then
If dgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Tag IsNot Nothing AndAlso _
dgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Tag = "tagged" Then
If e.Value IsNot Nothing Then
e.Value = String.Empty
e.FormattingApplied = True
End If
End If
End If
End Sub
This example is just testing the second column with is of the type Double.
I made a pretty simple adjustment. Inside the DataGridView_DoubleClick event:
With Me.DataGridView
Select Case .Columns(e.ColumnIndex).HeaderText'or preferred variable
Case "Qty"
Dim v As Object = InputBox("Enter positive numeric value for " & .Columns(e.ColumnIndex).HeaderText,
.Columns(e.ColumnIndex).HeaderText & " Value", .Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
If Not IsNumeric(v) OrElse CInt(v) < 0 Then
MsgBox("You must enter a positive numeric value", MsgBoxStyle.OkOnly, "Invalid Entry")
Exit Sub
End If
Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = v
End Select
End With

Change the row color in DataGridView based on the quantity of a cell value

I need to change the color of a row in datagridview but my code is not working for me.
I always get a error that says "Column named Quantity: cannot be found. Parameter name: columnName"
Here is my code:
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
If Me.DataGridView1.Rows(i).Cells("Quantity:").Value < 5 Then
Me.DataGridView1.Rows(i).Cells("Quantity:").Style.ForeColor = Color.Red
End If
Next
End Sub
Please help me fix it. Thank you.
This might be helpful
Use the "RowPostPaint" event
The name of the column is NOT the "Header" of the column. You have to go to the properties for the DataGridView => then select the column => then look for the "Name" property
I converted this from C# ('From: http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=74)
Private Sub dgv_EmployeeTraining_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs)
Handles dgv_EmployeeTraining.RowPostPaint
If e.RowIndex < Me.dgv_EmployeeTraining.RowCount - 1 Then
Dim dgvRow As DataGridViewRow = Me.dgv_EmployeeTraining.Rows(e.RowIndex)
'<== This is the header Name
'If CInt(dgvRow.Cells("EmployeeStatus_Training_e26").Value) <> 2 Then
'<== But this is the name assigned to it in the properties of the control
If CInt(dgvRow.Cells("DataGridViewTextBoxColumn15").Value.ToString) <> 2 Then
dgvRow.DefaultCellStyle.BackColor = Color.FromArgb(236, 236, 255)
Else
dgvRow.DefaultCellStyle.BackColor = Color.LightPink
End If
End If
End Sub
I fixed my error. just removed "Value" from this line:
If drv.Item("Quantity").Value < 5 Then
So it will look like
If drv.Item("Quantity") < 5 Then
Try this (Note: I don't have right now Visual Studio ,so code is copy paste from my archive(I haven't test it) :
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
Dim drv As DataRowView
If e.RowIndex >= 0 Then
If e.RowIndex <= ds.Tables("Products").Rows.Count - 1 Then
drv = ds.Tables("Products").DefaultView.Item(e.RowIndex)
Dim c As Color
If drv.Item("Quantity").Value < 5 Then
c = Color.LightBlue
Else
c = Color.Pink
End If
e.CellStyle.BackColor = c
End If
End If
End Sub
Just remove the : in your Quantity. Make sure that your attribute is the same with the parameter you include in the code, like this:
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
If Me.DataGridView1.Rows(i).Cells("Quantity").Value < 5 Then
Me.DataGridView1.Rows(i).Cells("Quantity").Style.ForeColor = Color.Red
End If
Next
End Sub
If drv.Item("Quantity").Value < 5 Then
use this to like this
If Cint(drv.Item("Quantity").Value) < 5 Then
Using the CellFormating event and the e argument:
If CInt(e.Value) < 5 Then e.CellStyle.ForeColor = Color.Red
Dim dgv As DataGridView = Me.TblCalendarDataGridView
For i As Integer = 0 To dgv.Rows.Count - 1
For ColNo As Integer = 4 To 7
If Not dgv.Rows(i).Cells(ColNo).Value Is DBNull.Value Then
dgv.Rows(i).Cells(ColNo).Style.BackColor = vbcolor.blue
End If
Next
Next

VB.NET ListView Questions

I have Questions?
If I enter a data in a textbox,
I want my listview to select the same data entered in the textbox,
example,
I have a StudentNumber column in my listview and it has data on it(ex. 123456)
I will enter 123456 in the textbox.
The ListView must select 123456 ?
Please Help
THANK YOU,
I think this will do what you want. It will search the first column of the ListView for the text in the TextBox.
Setup the listview:
With ListView1
.MultiSelect = False 'Ensure only one item selected at a time
.HideSelection = False 'Shows the selection when the textbox changes
'Add some items for testing
.Items.Add("1234")
.Items.Add("1122")
.Items.Add("1133")
End With
Then in the textbox TextChanged changed event:
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
ListView1.SelectedItems.Clear()
Dim foundItem As ListViewItem = ListView1.FindItemWithText(TextBox1.Text, False, 0, False)
If (foundItem IsNot Nothing) Then foundItem.Selected = True
End Sub
Alternatively if you want to specify which column of your ListView to search for the text then this function should do the trick:
Private Sub SelectListViewItem(ByRef listviewSource As ListView, ByVal textToFind As String, ByVal column As Integer)
Dim foundItem As ListViewItem = Nothing
Dim startIndex As Integer = 0
listviewSource.SelectedItems.Clear()
Do Until Not foundItem Is Nothing AndAlso foundItem.SubItems(column).Text = TextBox2.Text
If foundItem Is Nothing Then startIndex = 0 Else startIndex = foundItem.Index + 1
If startIndex > listviewSource.Items.Count - 1 Then Exit Sub 'We have reached end of the listview
foundItem = listviewSource.FindItemWithText(textToFind, True, startIndex)
If foundItem Is Nothing Then Exit Sub
Loop
If (foundItem IsNot Nothing) Then foundItem.Selected = True
End Sub
Usage:
Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
SelectListViewItem(ListView1, TextBox2.Text, 1)
End Sub
Warning - In both cases, this may cause your application to perform poorly if you have a lot of items in your listview, in that case you could consider moving the code into a background worker