Filtering Datagridview From Datagridview Not From Database - vb.net

i want to filter datagridview on my form..
on the form :
1 datagridview
1 label
1 timer
i have loaded database into datagridview (all data to datagridview)
on my datagridview i have 7 column the last column is date with format dd/MM/yyyy, and now how to filtering datagridview with label, i set this label to date like this
Private Sub TimerDate_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerDate.Tick
Dim FDate As String = Format(Today, "dd/MM/yyyy")
LblDate.Text = FDate
End Sub
and i want to eliminate the other data.. sooo in the end in my datagridview i have data with the last column same as LblDate.text
i dont want to filter datagridview from database.
can someone help me..? thanks.
sorry for my bad english.
this is how i populating data to datagrid
Public Class FrmJadwalSidang
Dim ConnString As String = ("Dsn=SqlConn;Server=192.168.100.1;uid=XXX;pwd=XXX;database=DBXXX;port=3306")
Public Function FillData(ByVal Sqlstring As String)
Dim OdbcConn As OdbcConnection = New OdbcConnection(ConnString)
OdbcConn.Open()
Dim MyDataSet As DataSet = New DataSet()
Dim MyOdbcdAdapter As OdbcDataAdapter = New OdbcDataAdapter()
MyOdbcdAdapter.SelectCommand = New OdbcCommand(Sqlstring, OdbcConn)
MyOdbcdAdapter.Fill(MyDataSet)
Me.DATAGRIDVIEW.DataSource = MyDataSet.Tables(0)
MyOdbcdAdapter.Dispose()
MyDataSet.Dispose()
OdbcConn.Close()
OdbcConn.Dispose()
End Function
Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillData("Select nomor_perkara, jam_sidang, para_pihak, majelis_hakim_text, panitera_pengganti_text, agenda, tanggal_sidang from v_jadwal_sidang")
End Sub
End Class
SOLVED
Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillData("Select nomor_perkara, jam_sidang, para_pihak, majelis_hakim_text, panitera_pengganti_text, agenda, tanggal_sidang from v_jadwal_sidang WHERE jadwal_sidang='" & LblDate.text.tostring & "'")
end sub
it work. in the end i have to filtering by the sql query..
thanks to someone who gives me the answer.

if you are using a SqlDataSource, set the FilterExpression of the control.
See this: How to: Enable Filtering for the SqlDataSource Control
And this: How to: Connect to an ODBC Database Using the SqlDataSource Control
EDIT : I provided the information for a web application not a winform application. Please read this for info on how to set up filtering for a BindingSource: BindingSource.Filter Property

Related

ifselectedindex changed , show data in a textbox

I've linked an access database to my form.
I have 1 table , 2 rows
1 = Researchtype short text
2 = Researchdetails (long text)
In my combobox1 i've binded my researchtype row so i can choose a type of research.
Question now: how can i bind the details data to the richtextbox below it in order to show the research data as soon as i choose a research type?
I've tried if else combos, try catch combos,
i'm thinking i'm actually overthinking the issue here.
What would be the easiest way to "select from dropdown" and show the result in textbox.
I'm a vb.net beginner
Public Class Onderzoeken
Private Sub Onderzoeken_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'PatientenDatabaseDataSetX.tbl_OnderzoeksTypes' table. You can move, or remove it, as needed.
Me.Tbl_OnderzoeksTypesTableAdapter.Fill(Me.PatientenDatabaseDataSetX.tbl_OnderzoeksTypes)
End Sub
Private Sub cboxOnderzoek_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboxOnderzoek.SelectedIndexChanged
If cboxOnderzoek.SelectedItem = Nothing Then
cboxOnderzoek.Text = ""
Else
rtbBeschrijvingOnderzoek.Text = CStr(CType(cboxOnderzoek.SelectedItem, DataRowView)("OZ_Onderzoeksbeschrijving"))
End If
End Sub
End Class
I added the entire code of that page now , it's not much, but as stated: I added the binding source and displaymember "researchtype" to the combobox.
So when i start the form, i can choose a type of research.
Now i need to show the description of the research in the richtextbox
In the Form.Load...
I have a function that returns a DataTable that contains columns called Name and Type. I bind the ComboBox to the DataTable and set the DisplayMember to "Name". Each Item in the ComboBox contains the entire DataRowView. I set the TextBox to the first row (dt(0)("Type")) Type column value so the correct information will be displayed for the initial selection.
I put the code to change the textbox display in ComboBox1.SelectionChangeCommitted because the other change events will produce a NRE since .SelectedItem has not yet been set when the form loads. The commited event will only occur when the user makes a selection.
First, cast the SelectedItem to its underlying type, DataRowView. Then you want the value of the Type column. This value is assigned to the text property of the textbox.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt = LoadCoffeeTable()
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Name"
TextBox1.Text = dt(0)("Type").ToString
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
TextBox1.Text = DirectCast(ComboBox1.SelectedItem, DataRowView)("Type").ToString
End Sub
Just substitute Researchtype for Name and Researchdetails for Type.
After using 'OleDbDataAdapter' to fill the dataset, you can set 'DisplayMember' and 'ValueMember' for your ComboBox. Every time the index of your ComboBox changes, it's 'ValueMember' will be displayed in richtextbox.
Here's the code you can refer to.
Private dataset As DataSet = New DataSet()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim connString As String = "your connection String"
Using con As OleDbConnection = New OleDbConnection(connString)
con.Open()
Dim cmd As OleDbCommand = New OleDbCommand()
cmd.Connection = con
cmd.CommandText = "SELECT Researchtype, Researchdetails FROM yourtable"
Dim adpt As OleDbDataAdapter = New OleDbDataAdapter(cmd)
adpt.Fill(dataset)
End Using
ComboBox1.DisplayMember = "Researchtype"
ComboBox1.ValueMember = "Researchdetails"
ComboBox1.DataSource = dataset.Tables(0)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
RichTextBox1.Text = ComboBox1.SelectedValue.ToString()
End Sub
Result of my test.

ADODB vb.net ComboBox value to Textbox working on the last index only

I'm having a problem here in vb.net where I need to get the value of the combobox value to textbox. but it only shows the last index of the combobox. /
//here is my code
Private Sub cboname_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboname.SelectedIndexChanged
strSql = "SELECT * FROM tblCashier WHERE ProductCode= '" & cboname.Text & "' "
Do Until myRecord.EOF
txtname.Text = myRecord.Fields("Product").Value
myRecord.MoveNext()
Loop
Call executeQuery2(strSql)
Call getRecord()
End Sub
// ADODB CONNECTIONS
Dim strSql As String
Dim myRecord As New ADODB.Recordset
// COMBO BOX VALUES
Sub fillcombo()
strSql = "SELECT * FROM tblCashier"
Do While Not myRecord.EOF
cboname.Items.Add(myRecord.Fields("ProductCode").Value)
myRecord.MoveNext()
Loop
cboname.Refresh()
Call executeQuery(strSql)
Call getRecord()
End Sub
// FORM_LOAD
Private Sub myPOS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call openConnection()
Call getRecord()
Call fillcombo()
End Sub
This sort of thing should be done something like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using adapter As New OleDbDataAdapter("SQL query here", "connection string here")
Dim table As New DataTable
adapter.Fill(table)
BindingSource1.DataSource = table
With ComboBox1
.DisplayMember = "ColumnNameToDisplay"
.ValueMember = "PrimaryKeyColumnName"
.DataSource = BindingSource1
End With
TextBox1.DataBindings.Add("Text", BindingSource1, "OtherColumnNameToDisplay")
End Using
End Sub
The TextBox will then automatically update as the selection in the ComboBox changes.

Can not Search in and Edit DB in the same times (and same Windows Form) by using DataGridView

I'm have DataGridView in a Windows Form with some data in it and I have button for edit, I want to search for row by using TextBox and button and when I find the wanted row I will change it and click edit button,when I edit any row (without using search button) and press edit the DB is Updated , my problem is that: when I search for specific row and find it then edit the data and press edit button the data in DB don't updated, please help, I'm use this code:
Imports System.Data.SqlClient
Public Class Form9
Private sqlConn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Clinic System\Clinic System\ClinicDB.mdf;Integrated Security=True;User Instance=True")
Private cmdSelect, cmdDelete As String
Private daEmployees As New SqlDataAdapter("Select * From History", sqlConn)
Private sqlCmndBuilder As New SqlCommandBuilder(daEmployees)
Private myDS As New DataSet
Private Sub HistoryBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.HistoryBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.ClinicDBDataSet3)
End Sub
Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
daEmployees.Fill(myDS, "History")
HistoryDataGridView.DataSource = myDS.Tables(0)
End Sub
Private Sub ButtonSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSearch.Click
Try
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Clinic System\Clinic System\ClinicDB.mdf;Integrated Security=True;User Instance=True")
Dim d1 As New SqlDataAdapter("Select * from History Where Name like '%" & TextBox1.Text & "%'", con)
Dim d2 As New DataTable
d1.Fill(d2)
HistoryDataGridView.DataSource = d2
Catch ex As Exception
MessageBox.Show("Err.Discription")
End Try
End Sub
Private Sub ButtonEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEdit.Click
daEmployees.Update(myDS.Tables(0))
MsgBox("Patient Details Updated!")
End Sub
Your issue is rather simple - you trying to update wrong table
daEmployees.Update(myDS.Tables(0))
While you need to update d2
In form scope
Dim _isSearch as boolean
On Form9_Load and when you reload as well
_isSearch = false
Here what you can do - In ButtonSearch_Click
_isSearch = true
If myDS.Tables.Contains("SEARCH_TBL") Then
myDS.Tables.Remove("SEARCH_TBL")
End if
Dim d2 As DataTable = myDS.Tables.Add("SEARCH_TBL")
d1.Fill(d2)
In ButtonEdit_Click
if _isSearch then
daEmployees.Update(myDS.Tables("SEARCH_TBL"))
else
daEmployees.Update(myDS.Tables(0))
End if
I think, daEmployees should update "SEARCH_TBL" because result set identical to first table. If not, just take your other adapter to a form scope.
But really, you can reuse the grid and update button, but you need to create logic tracking which table is currently loaded.

Public variable used for form opening not feeding through to from

Having some issues getting a form to populate based on a variable determined in current form.
I have a search result form that has a datagrid with all results, with an open form button for each row. When the user clicks this, the rowindex is used to pull out the ID of that record, which then feeds to the newly opened form and populates based on a SQL stored procedure run using the ID as a paramter.
However, at the moment the variable is not feeding through to the form, and am lost as to why that is. Stored procedure runs fine if i set the id within the code. Here is my form open code, with sci
Public Class SearchForm
Dim Open As New FormOpen
Dim data As New SQLConn
Public scid As Integer
Private Sub Search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sql As New SQLConn
Call sql.SearchData()
dgvSearch.DataSource = sql.dt.Tables(0)
End Sub
Private Sub dgvSearch_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSearch.CellContentClick
Dim rowindex As Integer
Dim oform As New SprinklerCardOpen
rowindex = e.RowIndex.ToString
scid = dgvSearch.Rows(rowindex).Cells(1).Value
TextBox1.Text = scid
If e.ColumnIndex = 0 Then
oform.Show()
End If
End Sub
End Class
The form opening then has the follwing:
Private Sub SprinklerCard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Populate fields from SQL
Try
Call Populate.SprinklerCardPopulate(ID)
cboInsured.Text = Populate.dt.Tables(0).Rows(0).Item(1)
txtAddress.Text = Populate.dt.Tables(0).Rows(0).Item(2)
txtContactName.Text = Populate.dt.Tables(0).Rows(0).Item(3)
txtContactPhone.Text = Populate.dt.Tables(0).Rows(0).Item(4)
txtContactEmail.Text = Populate.dt.Tables(0).Rows(0).Item(5)
numPumps.Value = Populate.dt.Tables(0).Rows(0).Item(6)
numValves.Value = Populate.dt.Tables(0).Rows(0).Item(7)
cboLeadFollow.Text = Populate.dt.Tables(0).Rows(0).Item(8)
cboImpairment.Text = Populate.dt.Tables(0).Rows(0).Item(9)
txtComments.Text = Populate.dt.Tables(0).Rows(0).Item(10)
Catch ex As Exception
MsgBox(ex.ToString & "SCID = " & ID)
End Try
End Sub
Set the ID variable in the form before you open it.
If e.ColumnIndex = 0 Then
oform.ID = scid
oform.Show()
End If

Fetching Data in GridView Cells/Column

I am a newbie in VB.net(programming). I need your advice regarding Gridview control.
I have a gridview loaded with two columns-one column(Name)is with some Text,another(Price) is empty.
I have got a TextBox with the data of Name and Price.
Now,I would like to loop through the Textbox,and see if the Data/symbols of the Column(Name) of GridView Control matches with the Data in Textbox.
If the Names of the GridView’s First Column’s data matches with the names of the Textbox,then the Price data should be fetched in the Second Column(Price) of GridView.
To make it more clear,say :
I have the following data in Textbox :
Name- Price
AB- 50
DE- 80
And I have two columns in GridView with following setups :
Name(column1) – Price(column2)
AB- Empty
DE- Empty
Now,how can I get the Price Data of Textbox,and fetch them into the Column2 of Gridview matching the Names of Column1. So,the output in GridView should be :
Name(column1) – Price(column2)
AB- 50
DE- 80
So far,I have been able to just loop through the first column of GridView…..I’m not sure how to get the data from Textbox and fetch the data into Column2 of GridView.
Any suggestion would be highly appreciated.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Data of GridView
Dim dt As New DataTable()
dt.Columns.Add("Name")
dt.Columns.Add("Price")
dt.Rows.Add(New [Object]() {"AB"})
dt.Rows.Add(New [Object]() {"DE"})
Me.DataGridView1.DataSource = dt
'Data of Textbox
TextBox1.Text = "AB, 50" & vbNewLine & "DE, 100"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'loop through the gridview
Dim marketvalue As String
For Each r As DataGridViewRow In Me.DataGridView1.Rows
marketvalue = r.Cells(0).Value
MessageBox.Show(marketvalue)
Next
End Sub End Class
I would use a separate function to get the price from your TextBox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'loop through the gridview
Dim marketvalue As String
Dim price As String
For Each r As DataGridViewRow In Me.DataGridView1.Rows
marketvalue = r.Cells(0).Value
MessageBox.Show(marketvalue)
'Get the price by calling a function and update it back to the DataGrid
price = get_price(marketvalue)
r.Cell(1).Value = price
Next
End Sub
The following function can return you the price by passing the name parameter, otherwise empty if the name is not found
Private Function get_price(ByVal strName As String) As String
Dim string_array() As String = TextBox1.Text.Split(System.Environment.vbNewLine)
For Each s As String In string_array
Dim string_detail() As String = s.Split(",")
If string_detail(0) = strName Then
Return Trim(string_detail(1))
End If
Next
Return ""
End Function