I have a DataGrid that I create dynamically in codebehind. I add events to the datagrid, it works fine except of the SortCommand event. It will not fire.
If I change my page_load to create and bind the datagrid everytime when a postback it works. But I donĀ“t want to do an extra databind at a postback.
All other events is working fine so why not the SortCommand???
.net 1.1
Visual studio 2003

Thanx for any help and ideas!

private void Page_Load(object sender, System.EventArgs e)
{
  if(!IsPostBack)
  {
    createDataGrid();
    bindDataGrid();
  }
  else
  {
  createDataGrid();
  }
}
 
private void createDataGrid()
{
  //Set datagrid properties
  myDataGrid.ID = "myDataGrid";
  myDataGrid.AutoGenerateColumns = false;
  myDataGrid.AllowPaging = true;
  myDataGrid.PageSize = 20;
  myDataGrid.ShowHeader = true;
  myDataGrid.ShowFooter = true;
  myDataGrid.AllowSorting = true;
  
  //Create datagrid events
  myDataGrid.ItemCommand += new DataGridCommandEventHandler(myDataGrid_ItemCommand);
  myDataGrid.PageIndexChanged += new DataGridPageChangedEventHandler(myDataGrid_PageIndexChanged);
  myDataGrid.ItemDataBound += new DataGridItemEventHandler(myDataGrid_ItemDataBound);
  myDataGrid.SortCommand +=new DataGridSortCommandEventHandler(myDataGrid_SortCommand);
  
  //Create and add bound/template columns to myDataGrid
  ...
}
 
private void bindDataGrid()
{
                        //Get data from databse and bind datagrid
                        DataSet ds = new DataSet();
                        ds = getDataFromDatabase();
                        dgRuleTables.DataSource=ds.Tables[0];
                        dgRuleTables.DataBind();
                }
 
private void myDataGrid_SortCommand(object source, DataGridSortCommandEventArgs e)
{
        //Conde inside here will never be executed
        //SortExpression
        //Rebind datagrid with new sortexpression !
}

Recommended Answers

All 2 Replies

Did your paging works without rebinding the data?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.