T
tsarna
I am having a problem with the DataGrid control and sorting, and have found it's realted to if you use dynamic columns or not. If I set the columns up on the aspx page and not in the code behind dynamically, the SortEvent will fire without requiring a double bind to the DataGrid. Below is code that will recreate the problem... If you comment out the ' If Not IsPostBack Then', and have it bind on every postback the event fires.
I would prefer to not have to perfrom a double databind so any help on this is appreciated!!!
Thanks
-Tim Sarna
..ASPX Page
------------------------------------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Dynamic.aspx.vb" Inherits="TestWeb.Dynamic" %>
<HTML>
<body>
<form runat="server" ID="Form1">
<h3>DataGrid Paging/Sorting - Dynamic Columns</h3>
<aspataGrid id="DataGrid1" runat="server" BorderColor="black"
AllowPaging="true" AutoGenerateColumns="false" AllowSorting="True">
<HeaderStyle BackColor="#00aaaa"></HeaderStyle>
<PagerStyle Mode="NextPrev"></PagerStyle>
</aspataGrid>
</form>
</body>
</HTML>
'Code Behind
'------------------------------
Public Class Dynamic
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration 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
If Not IsPostBack Then
' Need to load this data only once.
CreateGridColumns()
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub 'Page_Load
Private Sub Sort_Change(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
' Rebind the data.
CreateGridColumns()
DataGrid1.DataSource = CreateDataSource(e.SortExpression)
DataGrid1.DataBind()
End Sub
Sub Page_Change(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
' Set CurrentPageIndex to the page the user clicked.
DataGrid1.CurrentPageIndex = e.NewPageIndex
' Rebind the data.
CreateGridColumns()
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End Sub 'Grid_Change
Public Function CreateDataSource(Optional ByVal SortColumn As String = "IntegerValue") As ICollection
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
Dim i As Integer
For i = 0 To 99
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
dv.Sort = SortColumn
Return dv
End Function 'CreateDataSource
Public Sub CreateGridColumns()
Me.DataGrid1.Columns.Clear()
Dim col As New BoundColumn
col = New BoundColumn
Me.DataGrid1.Columns.Add(col)
col.HeaderText = "Number"
col.DataField = "IntegerValue"
col.SortExpression = "IntegerValue"
col = New BoundColumn
Me.DataGrid1.Columns.Add(col)
col.HeaderText = "Item"
col.DataField = "StringValue"
col.SortExpression = "StringValue"
col = New BoundColumn
Me.DataGrid1.Columns.Add(col)
col.HeaderText = "Price"
col.DataField = "CurrencyValue"
col.SortExpression = "CurrencyValue"
col.DataFormatString = "{0:c}"
End Sub
End Class
I would prefer to not have to perfrom a double databind so any help on this is appreciated!!!
Thanks
-Tim Sarna
..ASPX Page
------------------------------------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Dynamic.aspx.vb" Inherits="TestWeb.Dynamic" %>
<HTML>
<body>
<form runat="server" ID="Form1">
<h3>DataGrid Paging/Sorting - Dynamic Columns</h3>
<aspataGrid id="DataGrid1" runat="server" BorderColor="black"
AllowPaging="true" AutoGenerateColumns="false" AllowSorting="True">
<HeaderStyle BackColor="#00aaaa"></HeaderStyle>
<PagerStyle Mode="NextPrev"></PagerStyle>
</aspataGrid>
</form>
</body>
</HTML>
'Code Behind
'------------------------------
Public Class Dynamic
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration 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
If Not IsPostBack Then
' Need to load this data only once.
CreateGridColumns()
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub 'Page_Load
Private Sub Sort_Change(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
' Rebind the data.
CreateGridColumns()
DataGrid1.DataSource = CreateDataSource(e.SortExpression)
DataGrid1.DataBind()
End Sub
Sub Page_Change(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
' Set CurrentPageIndex to the page the user clicked.
DataGrid1.CurrentPageIndex = e.NewPageIndex
' Rebind the data.
CreateGridColumns()
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End Sub 'Grid_Change
Public Function CreateDataSource(Optional ByVal SortColumn As String = "IntegerValue") As ICollection
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
Dim i As Integer
For i = 0 To 99
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
dv.Sort = SortColumn
Return dv
End Function 'CreateDataSource
Public Sub CreateGridColumns()
Me.DataGrid1.Columns.Clear()
Dim col As New BoundColumn
col = New BoundColumn
Me.DataGrid1.Columns.Add(col)
col.HeaderText = "Number"
col.DataField = "IntegerValue"
col.SortExpression = "IntegerValue"
col = New BoundColumn
Me.DataGrid1.Columns.Add(col)
col.HeaderText = "Item"
col.DataField = "StringValue"
col.SortExpression = "StringValue"
col = New BoundColumn
Me.DataGrid1.Columns.Add(col)
col.HeaderText = "Price"
col.DataField = "CurrencyValue"
col.SortExpression = "CurrencyValue"
col.DataFormatString = "{0:c}"
End Sub
End Class