Search using textbox VB - vb.net

I need to search DataGridView by date. If I enter 2/11/2017 in text box, I want grid to show me rows that contain this date. Date column in my DataGridView is named AppointmentDate and is created in SQL Server. Type of this column is only date.
I saw this, but I am using Entity Framework model, I don't know how to realize it.
dgvPacientet.CurrentCell = Nothing
Dim found As Boolean = False
For Each row As DataGridViewRow In dgvPacientet.Rows
row.Visible = row.Cells("Dt_terminit").Value = txtData.Text
If row.Visible Then found = True
Next
If Not found Then MsgBox("Item not found")`

Simply show or hide the rows that contain the text you want.
DataGridView1.CurrentCell = Nothing
Dim found as Boolean = false
For Each row As DataGridViewRow In DataGridView1.Rows
Row.Visible = Not IsDBNull(row.Cells("DT_Terminit").Value) andalso row.Cells("DT_Terminit").Value = TextBox5.Text
If Row.Visible then found = true
Next
If Not found Then MsgBox("Item not found")
However you really need to parse that textbox to make sure it is a date first and if that cell contains a true Date, you need to convert the test into a date.
Dim DT As Date
If Not Date.TryParse(TextBox5.Text, DT) Then
MsgBox("Please enter a date") '<-- remove this line if searching on the fly... (as you enter text)
Exit Sub
End If
DataGridView1.CurrentCell = Nothing
Dim found As Boolean = False
For Each row As DataGridViewRow In DataGridView1.Rows
Row.Visible = Not IsDBNull(row.Cells("DT_Terminit").Value) andalso row.Cells("DT_Terminit").Value =DT
If row.Visible Then found = True
Next
If Not found Then MsgBox("Item not found")

Related

How to use function to prevent duplicated record for datagridview

I use function to prevent the same record goes into my datagridview but it doesnt work , when i separate the code out then its worked
i tried to seperate the for loop part out then the code work , but i wan to use function to do it so the code look more neater
Private Sub PicFavNote10_Click(sender As Object, e As EventArgs) Handles picFavNote10.Click
If validationDataGrid(lblNameNote10.Text) <> True Then
'if item didn added to the favorite data table yet
'add to favorite table
addTofavorite(lblUserLogin.Text, lblNameNote10.Text, lblDecpNote10.Text, txtPicNote10.Text, "SmartPhone", lblPriceNote10.Text)
End If
lblPriceNote10.Text = FormatCurrency(lblPriceNote10.Text)
End Sub
Private Function validationDataGrid(ByRef data As String) As Boolean
'validation on data grid view
For Each itm As DataGridViewRow In DGTFavTable.Rows 'loop though every item in datagrid
If itm.Cells(0).Value = data Then 'check wherter the text already exist
MsgBox(data & " Already added to your favorite cart")
Return True
Else
Return False
End If
Next
End Function
I expected the MsgBox(data & " Already added to your favorite cart") will excecute but instead the validationDataGrid function return false value even the item is already added to favorite datagridview
Before you loop all rows you need to call this sub as is an efficient workaround to validate new data on DataGridView:
Private Sub ForceGridValidation()
'Get the current cell
Dim currentCell As DataGridViewCell = DGTFavTable.CurrentCell
If currentCell IsNot Nothing Then
Dim colIndex As Integer = currentCell.ColumnIndex
If colIndex < DGTFavTable.Columns.Count - 1 Then
DGTFavTable.CurrentCell = DGTFavTable.Item(colIndex + 1, currentCell.RowIndex)
ElseIf colIndex > 1 Then
DGTFavTable.CurrentCell = DGTFavTable.Item(colIndex - 1, currentCell.RowIndex)
End If
'Set the original cell
DGTFavTable.CurrentCell = currentCell
End If
End Sub

Delete UNSELECTED rows in a datagridview

Hi I have a datagridview with multiselection set to true.
How would I go about letting a user delete all BUT the selected rows in a datagridview?
I tried this, but it doesn't seem to work:
For Each r As DataGridViewRow In DataGridView1.Rows
If r.Selected = False Then
DataGridView1.Rows.Remove(r)
End If
Next
For Each loops use an Enumerator, and you're not supposed to change the data source for an enumerator while you're still looping through it.
There's likely a better way, but if all else fails you should be able to do this by adding references to each unselected row to a new list, and then looping through the list to remove them rows from the grid.
Try this:
For Each r As DataGridViewRow In DataGridView1.Rows
If r.Selected = False Then
Dim row As String = r.ToString.Split("=")(1)(0)
DataGridView1.Rows(row).Visible = False
End If
Next
I suggest you to use LINQ here
'This will selects all the selected rows in your Datagridview
Dim sel_rows As List(Of DataGridViewRow) = (From row In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
Where row.Selected = False).ToList()
If MsgBox(String.Format("Do you want to delete {0} row(s)?", sel_rows.Count) _
, MsgBoxStyle.Information + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton3) = MsgBoxResult.Yes Then
For Each row As DataGridViewRow In sel_rows
If row.DataBoundItem IsNot Nothing Then
DataGridView1.Rows.Remove(row)
End If
Next
End If
Note : MsgBox() is optional !

Datagridview: How to change color of alternate rows having cell value alternate days?

Respected Sir,
I have a datagridview filled with data having various timestamps in one column
And trying to colour rows in range of alternate days like the image above. So far, i am trying like this
Private Sub alternateDaysRows()
For i As Integer = 1 To Me.datagridview1.Rows.Count - 1
If i > 1 Then
Dim myLastRowDate As Date
myLastRowDate = CType(Me.datagridview1.Rows(Me.datagridview1.Rows.Count - 1).Cells(2).Value, DateTime).Date
myCountDate = myLastRowDate.AddDays(1)
For Each row As DataGridViewRow In Me.datagridview1.Rows
If CType(row.Cells(2).Value, DateTime).Date = myCountDate Then
row.DefaultCellStyle.BackColor = Color.WhiteSmoke
End If
Next
End If
Next
End Sub
any suggestions ?
yours faithfully
Murulimadhav
Something along these lines may do what you want:
Private Sub alternateDaysRows()
Dim myLastRow As DataGridViewRow = Nothing
Dim myLastColor = Color.Yellow
Dim isFirstRow As Boolean = True
For Each row As DataGridViewRow In Me.DataGridView1.Rows
If myLastRow IsNot Nothing Then
If DateTime.Parse(myLastRow.Cells(0).Value.ToString).Date <> DateTime.Parse(row.Cells(0).Value.ToString).Date Then
myLastColor = If(myLastColor = Color.Yellow, Color.Red, Color.Yellow)
End If
End If
myLastRow = row
row.DefaultCellStyle.BackColor = myLastColor
Next
This assumes that the cells are sorted in date order and that the values are convertable to a date. It alternates when the date changes rather than on every other day but would highlight when a different day is being viewed.

VB.NET DataGridView rows

NET Hi I'm new in VB NET and I'm trying to check if all values in the same row of a datagridview are the same.
Exemple :
For Each row In DataGridView1.Rows
If 'all values in row are not the same' Then
row.DefaultCellStyle.BackColor = Color.Red
End if
Next
Tell me if there is anything that you don't understand in my question ^^
Thank you in advance for your help! :P
May be better if you use handler of .RowPrepaint
private sub datagridivew_RowPrepaint(sender as Object, e as DataGridViewRowPrePaintEventArgs) Handles datagridview.RowPrePaint
if e.RowIndex >= 0 then
Dim dgvr as DataGridViewRow = DirectCast(sender, DataGridView).Rows(e.RowIndex)
if IsAllValuesAreSame(dgvr) = True Then
dgvr.DefaultCellStyle.BackColor = Color.Red
End If
End If
End Sub
Then you don't need to loop all rows one more time after all rows initialized.
And function for checking all values of row:
private function IsAllValuesAreSame(dgvr as DataGridViewRow) as Boolean
Dim distinctQnt As Int32 = (From cell As DataGridViewCell In dgvr.Cells
Where cell.ColumnIndex >= 0
Select cell.Value).Distinct().Count()
Return (distinctQnt = 1)
End Function
You need to match the value of all the cells in each row with one cell in that row to know if all cells in that row have the same value. Below, I'm giving you the simplest method to do it and I am choosing the value of the first column of each row to verify if the values of other columns in that row are equal to it. If all the columns/cells of a row don't have the same value, then that row's back color will turn red.
For x = 0 To DataGridView1.RowCount - 1
For y = 0 To DataGridView1.ColumnCount - 1
If DataGridView1.Item(y, x).Value = DataGridView1.Item(0, x).Value Then
'yes
Else
'no
DataGridView1.Rows.Item(x).DefaultCellStyle.BackColor = Color.Red
End If
Next
Next

System.NullReferenceException on a ForEach of a datagrid

So all is in the question, I have a datagrid view who's parcoured by a foreach in his rows collection like so dataGridView1.Rows and I get and error of null type in the second if of the for each
Sub DataColumnFirstDouble(ByRef dGridView As DataGridView, ByVal iCol As Integer)
Dim bFirstRow As Boolean = False
Dim sTemp As String = ""
For Each RW As DataGridViewRow In dGridView.Rows
If (bFirstRow) Then
If (RW.Cells(iCol).Value.ToString() = sTemp) Then
RW.Cells(iCol).Selected = True
dGridView.CurrentCell.Style.BackColor = Color.LightGreen
dGridView.CurrentCell.Style.ForeColor = Color.White
End If
End If
sTemp = RW.Cells(iCol).Value.ToString()
bFirstRow = True
Next
End Sub
By the way the Datagrid is populated with 1 entry going
LongString, Number, Number
Hello , 8 , 8
The bug occur when I click on a new row also the function is called on the event of row leave
Need some help
By the way what I try to do is to check when the user enter the name in the longstring space who's a primary unique key in a database but It seems I can't find anythings to handle it by vb so I try to parse it every times he leave the rows to check if there's any double
It isn't clear where the error is, you should debug to figure out which variable exactly is null. So I'll assume it's RW.Cells(iCol).Value.
If there's no value in the cell, it might be null. This mean ToString won't work.
If (bFirstRow) Then
If RW.Cells(iCol).Value IsNot Nothing AndAlso RW.Cells(iCol).Value.ToString() = sTemp Then
RW.Cells(iCol).Selected = True
dGridView.CurrentCell.Style.BackColor = Color.LightGreen
dGridView.CurrentCell.Style.ForeColor = Color.White
End If
End If
You could even check if RW.Cells(iCol) exists, maybe it's trying to fetch the data in a cell that doesn't exists in the row.