G
Guest
I am trying to implement searchable DataGrid. I am creating a DataSet object with DataRelation on form load, as follows:
Private Sub BindDataGrid()
Dim planTS As New DataGridTableStyle
Dim appTS As New DataGridTableStyle
Try
'Call ExecuteDataset static method of SqlHelper class that returns a Dataset.
myDS = SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "spSearchPlanDetails")
'Create relation.
myDS.Relations.Add("Applicants", myDS.Tables(0).Columns("Client_ID"), myDS.Tables(1).Columns("Client_ID"), False)
'Set mapping name for planTS.
planTS.MappingName = "Table"
planTS.AllowSorting = False
Dim myCOL As New DataGridTextBoxColumn
With myCOL
.MappingName = "Plan_ID"
.HeaderText = "Plan_ID"
End With
planTS.GridColumnStyles.Add(myCOL)
myCOL = New DataGridTextBoxColumn
With myCOL
.MappingName = "Client_ID"
.HeaderText = "Client ID"
End With
planTS.GridColumnStyles.Add(myCOL)
myCOL = New DataGridTextBoxColumn
With myCOL
.MappingName = "PlanDate"
.HeaderText = "Plan Date"
.Format = "dd/MM/yyyy"
End With
planTS.GridColumnStyles.Add(myCOL)
'Set mapping name for appTS.
appTS.MappingName = "Table1"
appTS.AllowSorting = False
myCOL = New DataGridTextBoxColumn
With myCOL
.MappingName = "Client_ID"
.HeaderText = "Client_ID"
.Width = 0
End With
appTS.GridColumnStyles.Add(myCOL)
myCOL = New DataGridTextBoxColumn
With myCOL
.MappingName = "FirstName"
.HeaderText = "First Name"
End With
appTS.GridColumnStyles.Add(myCOL)
myCOL = New DataGridTextBoxColumn
With myCOL
.MappingName = "LastName"
.HeaderText = "Last Name"
End With
appTS.GridColumnStyles.Add(myCOL)
'Add planTS to the DataGrid.
grdDetails.TableStyles.Add(planTS)
'Add appTS to the DataGrid.
grdDetails.TableStyles.Add(appTS)
Catch ex As SqlException
Throw ex
End Try
End Sub
Here’s the code for spSearchPlanDetails stored proc:
CREATE PROCEDURE spSearchPlanDetails
AS
SELECT Plan_ID, Client_ID, PlanDate
FROM Plans
SELECT P.Client_ID, A.FirstName, A.LastName
FROM Plans P, Applicants A
WHERE P.Client_ID = A.Client_ID
GO
Now, I have three search criteria’s on my Form, Client_ID, FirstName and LastName. On click on Search button I am trying to show data in the DataGrid which matches the search criteria as follows:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
myDS.Tables(0).DefaultView.RowFilter = "Client_ID = " & txtClientID.Text & " AND FirstName LIKE '" & txtFirstName.Text & "%'" & " AND LastName LIKE '" & txtLastName.Text & "%'"
'Bind DataGrid.
grdDetails.DataSource = myDS.Tables(0).DefaultView
End Sub
But, I am getting an error saying "Cannot find column [FirstName]". Does anyone knows what it could be?
Thanx
Private Sub BindDataGrid()
Dim planTS As New DataGridTableStyle
Dim appTS As New DataGridTableStyle
Try
'Call ExecuteDataset static method of SqlHelper class that returns a Dataset.
myDS = SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "spSearchPlanDetails")
'Create relation.
myDS.Relations.Add("Applicants", myDS.Tables(0).Columns("Client_ID"), myDS.Tables(1).Columns("Client_ID"), False)
'Set mapping name for planTS.
planTS.MappingName = "Table"
planTS.AllowSorting = False
Dim myCOL As New DataGridTextBoxColumn
With myCOL
.MappingName = "Plan_ID"
.HeaderText = "Plan_ID"
End With
planTS.GridColumnStyles.Add(myCOL)
myCOL = New DataGridTextBoxColumn
With myCOL
.MappingName = "Client_ID"
.HeaderText = "Client ID"
End With
planTS.GridColumnStyles.Add(myCOL)
myCOL = New DataGridTextBoxColumn
With myCOL
.MappingName = "PlanDate"
.HeaderText = "Plan Date"
.Format = "dd/MM/yyyy"
End With
planTS.GridColumnStyles.Add(myCOL)
'Set mapping name for appTS.
appTS.MappingName = "Table1"
appTS.AllowSorting = False
myCOL = New DataGridTextBoxColumn
With myCOL
.MappingName = "Client_ID"
.HeaderText = "Client_ID"
.Width = 0
End With
appTS.GridColumnStyles.Add(myCOL)
myCOL = New DataGridTextBoxColumn
With myCOL
.MappingName = "FirstName"
.HeaderText = "First Name"
End With
appTS.GridColumnStyles.Add(myCOL)
myCOL = New DataGridTextBoxColumn
With myCOL
.MappingName = "LastName"
.HeaderText = "Last Name"
End With
appTS.GridColumnStyles.Add(myCOL)
'Add planTS to the DataGrid.
grdDetails.TableStyles.Add(planTS)
'Add appTS to the DataGrid.
grdDetails.TableStyles.Add(appTS)
Catch ex As SqlException
Throw ex
End Try
End Sub
Here’s the code for spSearchPlanDetails stored proc:
CREATE PROCEDURE spSearchPlanDetails
AS
SELECT Plan_ID, Client_ID, PlanDate
FROM Plans
SELECT P.Client_ID, A.FirstName, A.LastName
FROM Plans P, Applicants A
WHERE P.Client_ID = A.Client_ID
GO
Now, I have three search criteria’s on my Form, Client_ID, FirstName and LastName. On click on Search button I am trying to show data in the DataGrid which matches the search criteria as follows:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
myDS.Tables(0).DefaultView.RowFilter = "Client_ID = " & txtClientID.Text & " AND FirstName LIKE '" & txtFirstName.Text & "%'" & " AND LastName LIKE '" & txtLastName.Text & "%'"
'Bind DataGrid.
grdDetails.DataSource = myDS.Tables(0).DefaultView
End Sub
But, I am getting an error saying "Cannot find column [FirstName]". Does anyone knows what it could be?
Thanx