eddyinperu
asked on
I do not know how to sort a datagrid using a dropdownlist
I do not know if My last message was posted, my computer kick me out.
I have a dropdownlist(lstSort) and I want to be able to sort my datagrid depending of the value of my dropdownlist.
I have three value in my dropdownList(Name, Submit date ,Topic) I poblated my dropdownlist from ASP.
Andmy datagrid has 5 columns (First_name,Last_name,subm it_date,To pic,Url) how can I sort the columns with the object dropdownlist?
Here is my code:
<form id="Form1" method="post" runat="server">
<TBODY>
<tr align="center">
<td style="HEIGHT: 52px" align="center">
<asp:label id="lblLink" runat="server" Font-Size="Small" Width="179px">Click <a href="Submit.aspx" target="_self"><u>here</u> </a> to submit.
</asp:label>
<asp:label id="lblSort" runat="server" Font-Size="Small" Width="52px">Sort: </asp:label>
<asp:dropdownlist id="lstSort" runat="server">
<asp:ListItem Value="First_Name ASC">Name Asc</asp:ListItem>
<asp:ListItem Value="Submit_DateTime ASC" Selected="True">DateTime ASC</asp:ListItem>
<asp:ListItem Value="Topic ASC">Topic Asc</asp:ListItem>
</asp:dropdownlist>
</tr>
<tr>
<td><asp:datagrid id=dgADVTopics runat="server"
Font-Size="10px"
Width="476px"
HorizontalAlign="Center"
AlternatingItemStyle-BackC olor="#abd 6fe"
HeaderStyle-BackColor="Des ktop"
HeaderStyle-VerticalAlign= "Middle"
HeaderStyle-BorderColor="w hite"
HeaderStyle-BorderStyle="D ouble"
HeaderStyle-ForeColor="whi te"
HeaderStyle-HorizontalAlig n="Center"
CellPadding="5"
DataSource="<%# dsADVTopics %>"
BorderColor="#01305C"
BackColor="Info"
BorderStyle="Outset" Height="344px"
PageSize="10"
AllowPaging="True"
PagerStyle-NextPageText="N ext"
PagerStyle-PrevPageText="P rev"
OnPageIndexChanged="NewPag e">
<PagerStyle Font-Bold="True" HorizontalAlign="Right" ForeColor="Blue" Mode="NumericPages"></Page rStyle>
</asp:datagrid></td>
</tr>
<tr>
</tr>
</td>
</tr>
</FORM></table>
MY code of ASPX.vb :
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
conString = "Network Library=DBMSSOCN;" & _
"Data Source=@@@@;" & _
"Initial Catalog=VB2;" & _
"User ID=sa;" & _
"Password=##"
sqlCon = New SqlClient.SqlConnection(co nString)
Catch ex As Exception
lblError.Visible = True
lblError.Text = "Error connecting to the db." & vbCrLf & ex.ToString()
End Try
'get the data from the DB and display it in the datagrid
Try
daADVTopics = New SqlDataAdapter("SELECT First_Name + '' + Last_Name as Name, Submit_DateTime as [Date Submitted] , Topic, URL FROM ADVTopics ", sqlCon)
daADVTopics.Fill(dsADVTopi cs, "ADVTopics")
'dvADVTopics = dsADVTopics.Tables("ADVTop ics").Defa ultView
dgADVTopics.DataSource = dsADVTopics
dgADVTopics.DataBind()
Catch ex As Exception
lblError.Visible = True
lblError.Text += "Error displaying information" & vbCrLf & ex.ToString()
End Try
End Sub
Sub NewPage(ByVal sender As Object, ByVal e As DataGridPageChangedEventAr gs)
dgADVTopics.CurrentPageInd ex = e.NewPageIndex
dgADVTopics.DataBind()
End Sub
Private Sub lstSort_SelectedIndexChang ed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSort.SelectedIndexChang ed
' HOW CAN I DO THIS
End Sub
I really will appreciate your help!!
THanks
I have a dropdownlist(lstSort) and I want to be able to sort my datagrid depending of the value of my dropdownlist.
I have three value in my dropdownList(Name, Submit date ,Topic) I poblated my dropdownlist from ASP.
Andmy datagrid has 5 columns (First_name,Last_name,subm
Here is my code:
<form id="Form1" method="post" runat="server">
<TBODY>
<tr align="center">
<td style="HEIGHT: 52px" align="center">
<asp:label id="lblLink" runat="server" Font-Size="Small" Width="179px">Click <a href="Submit.aspx" target="_self"><u>here</u>
</asp:label>
<asp:label id="lblSort" runat="server" Font-Size="Small" Width="52px">Sort: </asp:label>
<asp:dropdownlist id="lstSort" runat="server">
<asp:ListItem Value="First_Name ASC">Name Asc</asp:ListItem>
<asp:ListItem Value="Submit_DateTime ASC" Selected="True">DateTime ASC</asp:ListItem>
<asp:ListItem Value="Topic ASC">Topic Asc</asp:ListItem>
</asp:dropdownlist>
</tr>
<tr>
<td><asp:datagrid id=dgADVTopics runat="server"
Font-Size="10px"
Width="476px"
HorizontalAlign="Center"
AlternatingItemStyle-BackC
HeaderStyle-BackColor="Des
HeaderStyle-VerticalAlign=
HeaderStyle-BorderColor="w
HeaderStyle-BorderStyle="D
HeaderStyle-ForeColor="whi
HeaderStyle-HorizontalAlig
CellPadding="5"
DataSource="<%# dsADVTopics %>"
BorderColor="#01305C"
BackColor="Info"
BorderStyle="Outset" Height="344px"
PageSize="10"
AllowPaging="True"
PagerStyle-NextPageText="N
PagerStyle-PrevPageText="P
OnPageIndexChanged="NewPag
<PagerStyle Font-Bold="True" HorizontalAlign="Right" ForeColor="Blue" Mode="NumericPages"></Page
</asp:datagrid></td>
</tr>
<tr>
</tr>
</td>
</tr>
</FORM></table>
MY code of ASPX.vb :
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
conString = "Network Library=DBMSSOCN;" & _
"Data Source=@@@@;" & _
"Initial Catalog=VB2;" & _
"User ID=sa;" & _
"Password=##"
sqlCon = New SqlClient.SqlConnection(co
Catch ex As Exception
lblError.Visible = True
lblError.Text = "Error connecting to the db." & vbCrLf & ex.ToString()
End Try
'get the data from the DB and display it in the datagrid
Try
daADVTopics = New SqlDataAdapter("SELECT First_Name + '' + Last_Name as Name, Submit_DateTime as [Date Submitted] , Topic, URL FROM ADVTopics ", sqlCon)
daADVTopics.Fill(dsADVTopi
'dvADVTopics = dsADVTopics.Tables("ADVTop
dgADVTopics.DataSource = dsADVTopics
dgADVTopics.DataBind()
Catch ex As Exception
lblError.Visible = True
lblError.Text += "Error displaying information" & vbCrLf & ex.ToString()
End Try
End Sub
Sub NewPage(ByVal sender As Object, ByVal e As DataGridPageChangedEventAr
dgADVTopics.CurrentPageInd
dgADVTopics.DataBind()
End Sub
Private Sub lstSort_SelectedIndexChang
' HOW CAN I DO THIS
End Sub
I really will appreciate your help!!
THanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hey NunoGodinho , It did not work!
It doesn't want to accept the symbol " != " from " if (lstSort.SelectedIndex != -1)" , so I tried with the symbo '<>' but it didn't work either.
I do not know if is reading this function:
Private Sub lstSort_SelectedIndexChang ed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSort.SelectedIndexChang ed
Dim dv as new DataView(dsADVTopics.Table s("ADVTopi cs"))
if (lstSort.SelectedIndex != -1)
dv.Sort = lstSort.SelectedValue
dgADVTopics.DataSource = dv
dgADVTopics.DataBind()
end if
End Sub
By the way I am using VB.net and ASPX . thanks
Eddy
It doesn't want to accept the symbol " != " from " if (lstSort.SelectedIndex != -1)" , so I tried with the symbo '<>' but it didn't work either.
I do not know if is reading this function:
Private Sub lstSort_SelectedIndexChang
Dim dv as new DataView(dsADVTopics.Table
if (lstSort.SelectedIndex != -1)
dv.Sort = lstSort.SelectedValue
dgADVTopics.DataSource = dv
dgADVTopics.DataBind()
end if
End Sub
By the way I am using VB.net and ASPX . thanks
Eddy
ASKER
Another question more, sorry for bother you..... DO i need to create a function for the sort? if the answert is yes, how do i do that?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Eddy,
I'm sorry about the '!=' but i normally work with c# and so you should use the '<>' symbol.
The problem with the sorting and the pagging it's complicated to make the sort and stay in the same page that you were, and i think it doesn't make that mutch sense because if you sort the things you were seeing on the page will not be the same.
Hope it helped,
Nuno Godinho
I'm sorry about the '!=' but i normally work with c# and so you should use the '<>' symbol.
The problem with the sorting and the pagging it's complicated to make the sort and stay in the same page that you were, and i think it doesn't make that mutch sense because if you sort the things you were seeing on the page will not be the same.
Hope it helped,
Nuno Godinho
ASKER
Hy deanvanrooyen and SKumar_1981
I made this changes to my code : " dgADVTopics.DataSource = Source "
It didn't work! whyyyyyyyyyy yyy? I am getting so mad with this problem!!
And deanvanrooyen ,..I couldn't understand your answer, sorry about this , I recently started to study ASP and all these stuff, I apologize for my lack of knowledge refering to this subject. Could you be more specific about the connection? Because I don't think that I have a problem with my connection(my datagrid work great). I want to work with a dataview and sort my datagrid with my dropdownlist(depending of the value that is inside the dropdownlist 'name', 'topic',sumitdat')
I want to fire the sort using the 'SelectedIndexChanged' and if this doesn't work I will try to put a command button to make the sort. Could you use the example with the same variable that I wrote in my code? Thanks..!!
Do I need to write a function that sort? how do I do this?
Thanks EXPERTS, I really appreciate your help
I made this changes to my code : " dgADVTopics.DataSource = Source "
It didn't work! whyyyyyyyyyy yyy? I am getting so mad with this problem!!
And deanvanrooyen ,..I couldn't understand your answer, sorry about this , I recently started to study ASP and all these stuff, I apologize for my lack of knowledge refering to this subject. Could you be more specific about the connection? Because I don't think that I have a problem with my connection(my datagrid work great). I want to work with a dataview and sort my datagrid with my dropdownlist(depending of the value that is inside the dropdownlist 'name', 'topic',sumitdat')
I want to fire the sort using the 'SelectedIndexChanged' and if this doesn't work I will try to put a command button to make the sort. Could you use the example with the same variable that I wrote in my code? Thanks..!!
Do I need to write a function that sort? how do I do this?
Thanks EXPERTS, I really appreciate your help
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
HI DEANVANROOYEN;
I try this:
Private Sub lstSort_SelectedIndexChang ed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSort.SelectedIndexChang ed
Dim Source As DataView = New DataView(dsADVTopics.Table s("ADVTopi cs"), "field = " + lstSort.SelectedValue, "orderbyvalue", DataViewRowState.CurrentRo ws)
Source.Sort = lstSort.SelectedItem.Value
dgADVTopics.DataSource = Source
dgADVTopics.DataBind()
End Sub
It didn't work!!!!!!!
This is the error:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.DataException: DataTable must be set prior to using DataView.
Source Error:
Line 86: Private Sub lstSort_SelectedIndexChang ed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSort.SelectedIndexChang ed
Line 87:
Line 88: Dim Source As DataView = New DataView(dsADVTopics.Table s("ADVTopi cs"), "field = " + lstSort.SelectedValue, "orderbyvalue", DataViewRowState.CurrentRo ws)
Line 89: Source.Sort = lstSort.SelectedItem.Value
Line 90: dgADVTopics.DataSource = Source
Thanksssss
I try this:
Private Sub lstSort_SelectedIndexChang
Dim Source As DataView = New DataView(dsADVTopics.Table
Source.Sort = lstSort.SelectedItem.Value
dgADVTopics.DataSource = Source
dgADVTopics.DataBind()
End Sub
It didn't work!!!!!!!
This is the error:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.DataException:
Source Error:
Line 86: Private Sub lstSort_SelectedIndexChang
Line 87:
Line 88: Dim Source As DataView = New DataView(dsADVTopics.Table
Line 89: Source.Sort = lstSort.SelectedItem.Value
Line 90: dgADVTopics.DataSource = Source
Thanksssss
ASKER
Hey experts
I added a command button and I think it is workin but still I have a couple questions.
1) It sort my datagrid depending of the value of my dropdownlist but I am using a IF STATEMENT which allow me to decide for only 2 options , BUT I need three options(3 values in my dropdownlist ('name', 'SUbmit date', 'Topic') how can i do this?
2) I am paging my datagrid and when I am sorting a column and I go to the next page and that page is not excuting the sort that started in my first page
This is my code in VB.net i really do not know to much about C# :
Public Class WebForm2
Inherits System.Web.UI.Page
Dim sqlCon As SqlClient.SqlConnection
Dim daADVTopics As SqlDataAdapter
Protected WithEvents Form1 As System.Web.UI.HtmlControls .HtmlForm
Protected WithEvents lblError As System.Web.UI.WebControls. Label
Protected WithEvents lstSort As System.Web.UI.WebControls. DropDownLi st
Protected WithEvents lblSort As System.Web.UI.WebControls. Label
Protected WithEvents bttnSub As System.Web.UI.WebControls. Button
Dim conString As String
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.dsADVTopics = New System.Data.DataSet
Me.dvADVTopics = New System.Data.DataView
CType(Me.dsADVTopics, System.ComponentModel.ISup portInitia lize).Begi nInit()
CType(Me.dvADVTopics, System.ComponentModel.ISup portInitia lize).Begi nInit()
'
'dsADVTopics
'
Me.dsADVTopics.DataSetName = "NewDataSet"
Me.dsADVTopics.Locale = New System.Globalization.Cultu reInfo("en -US")
CType(Me.dsADVTopics, System.ComponentModel.ISup portInitia lize).EndI nit()
CType(Me.dvADVTopics, System.ComponentModel.ISup portInitia lize).EndI nit()
End Sub
Protected WithEvents dsADVTopics As System.Data.DataSet
Protected WithEvents dvADVTopics As System.Data.DataView
Protected WithEvents dgADVTopics As System.Web.UI.WebControls. DataGrid
Protected WithEvents lblLink As System.Web.UI.WebControls. Label
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara tion As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
conString = "Network Library=DBMSSOCN;" & _
"Data Source=####;" & _
"Initial Catalog=VB2;" & _
"User ID=sa;" & _
"Password=???"
sqlCon = New SqlClient.SqlConnection(co nString)
Catch ex As Exception
lblError.Visible = True
lblError.Text = "Error connecting to the db." & vbCrLf & ex.ToString()
End Try
Try
daADVTopics = New SqlDataAdapter("SELECT First_Name + '' + Last_Name as Name, convert(varchar (10), Submit_DateTime,101) as [Date Submitted], Topic, URL FROM ADVTopics ", sqlCon)
daADVTopics.Fill(dsADVTopi cs, "ADVTopics")
dgADVTopics.DataSource = dsADVTopics
dgADVTopics.DataBind()
Catch ex As Exception
lblError.Visible = True
lblError.Text += "Error displaying information" & vbCrLf & ex.ToString()
End Try
End Sub
Sub NewPage(ByVal sender As Object, ByVal e As DataGridPageChangedEventAr gs)
dgADVTopics.CurrentPageInd ex = e.NewPageIndex
dgADVTopics.DataBind()
End Sub
Private Sub bttnSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnSub.Click
dsADVTopics.Clear()
dgADVTopics.Dispose()
Try
conString = "Network Library=DBMSSOCN;" & _
"Data Source=##" & _
"Initial Catalog=VB2;" & _
"User ID=sa;" & _
"Password=??"
sqlCon = New SqlClient.SqlConnection(co nString)
Catch ex As Exception
lblError.Visible = True
lblError.Text = "Error connecting to the db." & vbCrLf & ex.ToString()
End Try
'refill the datagrid with the data sorted by the selected value in the list box
Try
If lstSort.SelectedItem.Text = "Name" Then
daADVTopics = New SqlDataAdapter("SELECT First_Name + '' + Last_Name as Name,convert(varchar (10), Submit_DateTime,101) as [Date Submitted], Topic, URL from ADVTopics order by First_Name ASC", sqlCon)
Else
daADVTopics = New SqlDataAdapter("SELECT First_Name + '' + Last_Name as Name, convert(varchar (10), Submit_DateTime,101) as [Date Submitted], Topic, URL from ADVTopics order by Topic ASC", sqlCon)
End If
daADVTopics.Fill(dsADVTopi cs, "ADVTopics")
dvADVTopics = dsADVTopics.Tables("ADVTop ics").Defa ultView
dgADVTopics.DataSource = dvADVTopics
dgADVTopics.DataBind()
Catch ex As Exception
End Try
End Sub
I added a command button and I think it is workin but still I have a couple questions.
1) It sort my datagrid depending of the value of my dropdownlist but I am using a IF STATEMENT which allow me to decide for only 2 options , BUT I need three options(3 values in my dropdownlist ('name', 'SUbmit date', 'Topic') how can i do this?
2) I am paging my datagrid and when I am sorting a column and I go to the next page and that page is not excuting the sort that started in my first page
This is my code in VB.net i really do not know to much about C# :
Public Class WebForm2
Inherits System.Web.UI.Page
Dim sqlCon As SqlClient.SqlConnection
Dim daADVTopics As SqlDataAdapter
Protected WithEvents Form1 As System.Web.UI.HtmlControls
Protected WithEvents lblError As System.Web.UI.WebControls.
Protected WithEvents lstSort As System.Web.UI.WebControls.
Protected WithEvents lblSort As System.Web.UI.WebControls.
Protected WithEvents bttnSub As System.Web.UI.WebControls.
Dim conString As String
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
Me.dsADVTopics = New System.Data.DataSet
Me.dvADVTopics = New System.Data.DataView
CType(Me.dsADVTopics, System.ComponentModel.ISup
CType(Me.dvADVTopics, System.ComponentModel.ISup
'
'dsADVTopics
'
Me.dsADVTopics.DataSetName
Me.dsADVTopics.Locale = New System.Globalization.Cultu
CType(Me.dsADVTopics, System.ComponentModel.ISup
CType(Me.dvADVTopics, System.ComponentModel.ISup
End Sub
Protected WithEvents dsADVTopics As System.Data.DataSet
Protected WithEvents dvADVTopics As System.Data.DataView
Protected WithEvents dgADVTopics As System.Web.UI.WebControls.
Protected WithEvents lblLink As System.Web.UI.WebControls.
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
conString = "Network Library=DBMSSOCN;" & _
"Data Source=####;" & _
"Initial Catalog=VB2;" & _
"User ID=sa;" & _
"Password=???"
sqlCon = New SqlClient.SqlConnection(co
Catch ex As Exception
lblError.Visible = True
lblError.Text = "Error connecting to the db." & vbCrLf & ex.ToString()
End Try
Try
daADVTopics = New SqlDataAdapter("SELECT First_Name + '' + Last_Name as Name, convert(varchar (10), Submit_DateTime,101) as [Date Submitted], Topic, URL FROM ADVTopics ", sqlCon)
daADVTopics.Fill(dsADVTopi
dgADVTopics.DataSource = dsADVTopics
dgADVTopics.DataBind()
Catch ex As Exception
lblError.Visible = True
lblError.Text += "Error displaying information" & vbCrLf & ex.ToString()
End Try
End Sub
Sub NewPage(ByVal sender As Object, ByVal e As DataGridPageChangedEventAr
dgADVTopics.CurrentPageInd
dgADVTopics.DataBind()
End Sub
Private Sub bttnSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnSub.Click
dsADVTopics.Clear()
dgADVTopics.Dispose()
Try
conString = "Network Library=DBMSSOCN;" & _
"Data Source=##" & _
"Initial Catalog=VB2;" & _
"User ID=sa;" & _
"Password=??"
sqlCon = New SqlClient.SqlConnection(co
Catch ex As Exception
lblError.Visible = True
lblError.Text = "Error connecting to the db." & vbCrLf & ex.ToString()
End Try
'refill the datagrid with the data sorted by the selected value in the list box
Try
If lstSort.SelectedItem.Text = "Name" Then
daADVTopics = New SqlDataAdapter("SELECT First_Name + '' + Last_Name as Name,convert(varchar (10), Submit_DateTime,101) as [Date Submitted], Topic, URL from ADVTopics order by First_Name ASC", sqlCon)
Else
daADVTopics = New SqlDataAdapter("SELECT First_Name + '' + Last_Name as Name, convert(varchar (10), Submit_DateTime,101) as [Date Submitted], Topic, URL from ADVTopics order by Topic ASC", sqlCon)
End If
daADVTopics.Fill(dsADVTopi
dvADVTopics = dsADVTopics.Tables("ADVTop
dgADVTopics.DataSource = dvADVTopics
dgADVTopics.DataBind()
Catch ex As Exception
End Try
End Sub
ASKER
IS THERE NO EXPERTS THAT CAN HELP ME WITH THIS PROBLEM?
EDDY
EDDY
ASKER
Sorting the column will mess up with the paging?
ALso; do you think that It is more easy to do it through my dataset instead of my dataview?
I will appreaciate your answer
Thanks
eddy