Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnSortCommand example using VB or C# required

Status
Not open for further replies.

2xhelix

IS-IT--Management
Jul 15, 2004
9
CA
I am trying to specify the OnSortCommand


<ASP:DataGrid runat="server" id="ItemReportDG" OnSortCommand="ItemReportDG_Sort"></ASP:DataGrid>


I know this will run
Sub ItemReportDG_Sort(sender As Object, e As DataGridSortCommandEventArgs)
...
...
End Sub

Here is the trick that I can't seem to figure out. I am actually passing a DataGrid as a control back to the form that needs to be sorted. So how can i specify the OnSortCOmmand using code, and not inside the tag?

For example, right now I have

MyDataGrid.AllowSorting = True
MyDataGrid.AutoGenerateColumns = False

and then I proceed to add bounded columns.

Please help, this is urgent





 
myDg.OnSortCommand = sortMyGridNowPlease(sortExpr As String)

?
 
Nope, that won't work. OnSortCommand is protected. Also, the OnSorCommand event handler is located on the .aspx page.

Code:
Sub Page_Load(sender as Object, e as EventArgs)
  rptID = Cint(RTrim(Request.QueryString("id")))
  NewView = New DataViewer(SortField, rptID,ItemReportDG)
  If Not IsPostBack Then
    If SortField = "" Then
      SortField = "ItemNum"
    End If   
    
    NewView.setSort(SortField)
    NewView.BindData()
    ItemReportDG = NewView.getGrid()
  End If
End Sub

The DataViewer is a custom class that I built that will query the database, and return a given dataset. There is no problem doing it this way so far. The Grid is also formatted inside that class.

The problem , after using the debugging tool is after a PostBack, it doesn't know to run the


Sub ItemReportDG_Sort(sender As Object, e As DataGridSortCommandEventArgs)


Any suggestions?
 
Code:
html:
<asp:DataGrid Runat="server" ID="myGrid" AllowSorting="True">
  <Columns>
    <asp:TemplateColumn HeaderText="First Name" SortExpression="FirstName">
      <ItemTemplate>
        <asp:Label runat=server Text='DataBinder.Eval(Container.DataItem, "FirstName")' />
      </ItemTemplate>
    </asp:TemplateColumn>
  </Columns>
</asp:DataGrid>

code behind:
private void SortGrid(object source, DataGridSortCommandEventArgs e)
{
  string sortExpression = e.SortExpression;
  BindGrid(sortExpression);
}

private void BindGrid(string sortField)
{
   DataView dv = <hit database to return DataView>;
   dv.Sort = sortField;
   myGrid.DataSoure = dv;
   myGrid.DataBind();
}

private void InitializeComponent()
{
  myGrid.SortCommand += new DataGridSortCommandEventHandler(this.SortGrid);
}

I think you got the idea, the point is to pass the field name to sort by and rebind the grid to the newly sorted recordset.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top