 |
| ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application . |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 12th, 2004, 06:45 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello,
This time i tried to sorting and Filtering Rows in a DataGrid.I coded like the following.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim gstrSortOrder As String
Dim gstrFindText As String
Dim dst As DataSet
Dim con As SqlConnection
Dim dad As SqlDataAdapter
Dim dvw As DataView
If Page.IsPostBack Then
'gstrSortOrder = "ProductName"
gstrFindText = "ProductName LIKE '*" & TextBox1.Text & " *'"
Else
gstrSortOrder = "ProductID"
TextBox1.Text = "Tofu"
End If
BindDataGrid()
End Sub
Public Sub BindDataGrid()
Dim gstrSortOrder As String
Dim gstrFindText As String
Dim dst As DataSet
Dim con As SqlConnection
Dim dad As SqlDataAdapter
Dim dvw As DataView
dst = New DataSet()
con = New SqlConnection("Server=cabin61;UID=sa;PWD=;Database =Northwind")
con.Open()
dad = New SqlDataAdapter("Select * From Products", con)
dad.Fill(dst, "Products")
dvw.Sort = gstrSortOrder
dvw.RowFilter = gstrFindText
DataGrid1.DataSource = dvw
DataGrid1.DataBind()
con.Close()
End Sub
Public Sub SortRows(ByVal objSender As Object, ByVal objArgs As DataGridSortCommandEventArgs)
Dim gstrSortOrder As String
gstrSortOrder = objArgs.SortExpression.ToString()
BindDataGrid()
End Sub
These codes are given in the wrox book. but it is not working.The error message returned was " object reference not set to an instance of an object " and the line containing error is dvw.Sort = gstrSortOrder. How to solve this problem ?
|
|

August 13th, 2004, 02:42 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry, but some line of codes have not been written by me, thats why the error was coming. Now the data can be viewed easily. But still there is some problem.The data retrieved can be sorted by any columnID, but the filtering is not happening. Is there any other lines of code to be inserted, or there is some error in the codes i wrote ? advise please
|
|

August 15th, 2004, 09:28 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
I think the problem is that you have gstrSortOrder and gstrFindText declared both globally and locally in the BindDataGrid function. Remove the declarations from the BindDataGrid function.
|
|

August 16th, 2004, 02:24 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I tried this also, i declared the variables globally, removing them from the BindDataGrid function. But it is not working. I am providing the code once again below.Please check this.
Imports System.Data.SqlClient
Public Class WebForm3
Inherits System.Web.UI.Page
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Dim gstrSortOrder As String
Dim gstrFindText As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Page.IsPostBack Then
gstrFindText = "LastName LIKE '*" & TextBox1.Text & " *'"
Else
gstrSortOrder = "EmployeeID"
TextBox1.Text = "Ann"
End If
BindDataGrid()
End Sub
Public Sub BindDataGrid()
Dim dst As DataSet
Dim con As SqlConnection
Dim dad As SqlDataAdapter
Dim dvw As DataView
dst = New DataSet()
con = New SqlConnection("Server=cabin61;UID=sa;PWD=;Database =Northwind")
con.Open()
dad = New SqlDataAdapter("Select * From Employees ", con)
dad.Fill(dst, "Employees")
dvw = dst.Tables("Employees").DefaultView
dvw.Sort = gstrSortOrder
dvw.RowFilter = gstrFindText
DataGrid1.DataSource = dvw
DataGrid1.DataBind()
con.Close()
End Sub
Public Sub SortRows(ByVal objSender As Object, ByVal objArgs As DataGridSortCommandEventArgs)
gstrSortOrder = objArgs.SortExpression.ToString()
BindDataGrid()
End Sub
End Class
Here also the same problem is coming, i can sort using any column name but filtering is not possible.
Also is it possible to link each column headers differently to some different pages such that on clicking on them we can navigate to some other page ?
|
|

August 16th, 2004, 08:27 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
A) One problem I didn't catch before is that you are binding the data every time the page loads. Move BindDataGrid() into the Else of the If in page_load.
Then add handlers for the two text boxes you want to filter by. Have the OnChange event for those text boxes set the dataset's rowfilter and rebind.
B) I'm not sure if it matters, but I would call .DefaultView after you set the row filter. That's the way I do it and it works.
C) Your filter doesn't look right. You have * as the wildcard, try % instead.
|
|

August 18th, 2004, 02:19 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You said that you would call .DefaultView after setting the row filter, but i cant understand how a dataview can be filtered before we attach a defaultview, and if there is any way i dont know it. Please make me understand.
|
|

August 18th, 2004, 10:02 PM
|
|
Authorized User
|
|
Join Date: Aug 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The filter * is for Access, % is for SQL server and others ISO Standard SQL
~ Human Knowledge Belongs to the World !
|
|

August 18th, 2004, 10:11 PM
|
|
Authorized User
|
|
Join Date: Aug 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
if you are doing asp.net then u should create a hyperlink column.
<asp:HyperLinkColumn Target="_self"
DataNavigateUrlField="FieldKey"
DataNavigateUrlFormatString="./index.aspx/URL={0}"
DataTextField=""
HeaderText=""
>
</asp:HyperLinkColumn>
~ Human Knowledge Belongs to the World !
|
|

August 19th, 2004, 06:17 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, thanks for the reply desmond047.But i was trying something different.Let me make it clear.
I have displayed the data from the database using datagrid.Suppose we have three column names in the displayed employee table viz. employeeid,employeename and division. When the data are displayed, these three fields are the output.Now I want that, the employee names that are displayed are the hyperlinks to another page where their personal informations are stored.
This was the application I was trying. Can you tell me how to do that ? I dont want to use the hyperlink column as every member in this column navigates to the same page and it leaves no option to navigate to different pages from different fields.
|
|

August 19th, 2004, 07:48 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
This works fine for me...
_objData.DefaultView.RowFilter = _strFilter
_objData.DefaultView.Sort = _strSort
grdSR.DataSource = _objData.DefaultView
grdSR.DataBind()
|
|
 |