Extend functionality of datagrid12.aspx sample

S

Sal Young

I want to add "Paging" to a DataGrid that already has sort by column
funtionality. Here I'm including the code I have so far which displays a
two column DataGrid with letters and numbers. The letters are stored in
ascending order (a, b, c, d, e, etc.) while numbers are in descending order
(26, 25, 24, 23, etc.) in the database table. In other words, a = 26, b =
25, c = 24.



-----------------------------------------------------------
sub Page_Load(Source as Object, e as EventArgs)
if not isPostback then
bindDataGrid("letters")
end if
end sub

sub sortMyDataGrid(a as object, e as DataGridSortCommandEventArgs)
bindDataGrid(e.SortExpression)
end sub

sub bindDataGrid(SortField as string)
Dim myConn as sqlConnection
Dim connStr as String
Dim DS as DataSet
Dim myCommand as SqlDataAdapter

connStr = "server=myservername;database=test;uid=sa;pwd=SoFoHaI"
myConn = new sqlConnection(connStr)
myCommand = new SqlDataAdapter("SELECT letters, numbers FROM tbl_sort",
myConn)

DS = new DataSet()
MyCommand.Fill(DS, "tbl_sort")
Dim Source as DataView = DS.Tables("tbl_sort").DefaultView
Source.Sort = SortField
DataGrid1.DataSource = Source
DataGrid1.DataBind()
myConn.Close()
end sub

'sub doPaging(a as object, e as DataGridPageChangedEventArgs)
' dataGrid1.CurrentPageIndex=e.NewPageIndex
' bindDataGrid
'end sub
-----------------------------------------------------------

<asp:datagrid id="dataGrid1"
Runat="server"
DataKeyField="letters"
onSortCommand="sortMyDataGrid"
AllowSorting="True"
autoGenerateColumns="false">
<Columns>
<asp:BoundColumn
DataField="letters"
HeaderText="Letter"
SortExpression="letters">
</asp:BoundColumn>
<asp:BoundColumn
DataField="numbers"
HeaderText="Numbersl"
SortExpression="numbers">
</asp:BoundColumn>
</Columns>
</asp:datagrid>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top