This is a migrated thread and some comments may be shown as answers.

radgrid sort expression problem

1 Answer 38 Views
Grid
This is a migrated thread and some comments may be shown as answers.
zhan
Top achievements
Rank 1
zhan asked on 08 Nov 2013, 01:12 AM
the default view,  such as
  • id       name 
  • 1          tom
  • 2          jack

but when I user the custom sort,the radgrid view become follows:

  • id       name 
  • 1          tom            system.data.datarowview     system.data.datarowview
  • 2          jack           system.data.datarowview       system.data.datarowview
when I sort one time,the view will add two columns,who can help me?
the source code:
protected void Page_Init(object sender, EventArgs e)
      {
              GridBoundColumn idColumn = new GridBoundColumn();
              idColumn.DataField = "Id";
              idColumn.Display = true;
              idColumn.SortExpression = "Id";
              idColumn.UniqueName = "Id";
              idColumn.HeaderText = "Id";
              RadGrid1.MasterTableView.Columns.Add(idColumn);
          
              GridBoundColumn realNameColumn = new GridBoundColumn();
              realNameColumn.DataField = "RealName";
              realNameColumn.Display = true;
              realNameColumn.HeaderText = "name";
              realNameColumn.UniqueName = "RealName";
              realNameColumn.SortExpression = "RealName";
              RadGrid1.MasterTableView.Columns.Add(realNameColumn);
 
      }
 
protected void Page_Load(object sender, EventArgs e)
      {
              IDAL.IBaseRepository<Model.HS_Docter> doctors = new DAl.BaseRepository<Model.HS_Docter>();
              RadGrid1.DataSource = doctors.GetDataTable("select * from Hs_doctor");  
      }
 
protected void RadGrid1_SortCommand(object sender, GridSortCommandEventArgs e)
      {
 
 
          IDAL.IBaseRepository<Model.HS_Docter> doctors = new DAl.BaseRepository<Model.HS_Docter>();
          switch (e.OldSortOrder)
          {
               
              case GridSortOrder.Ascending:
               
                  e.Item.OwnerTableView.DataSource = doctors.GetDataTable("select * from hs_doctor").Select("", e.CommandArgument.ToString() + " asc");
                  break;
              case GridSortOrder.Descending:
                  e.Item.OwnerTableView.DataSource = doctors.GetDataTable("select * from  hs_doctor" ).Select("", e.CommandArgument.ToString() + " desc"); ;
                
              break;
          }
         
          
 
         
      }

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 08 Nov 2013, 09:36 AM
Hi ,

Please make sure that you create the RadGrid completely from code behind in the Page_Init event. The RadGrid should be populated in the NeedDataSource event of the RadGrid. Please try the following code snippet.

ASPX:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

C#:
RadGrid grid;   
protected void Page_Init(object sender, EventArgs e)
{
    grid = new RadGrid();
    grid.ID = "RadGrid1";      
    grid.Skin = "Vista";    
    grid.PageSize = 15;
    grid.AllowPaging = true;      
    grid.AutoGenerateColumns = false;
    grid.AllowSorting = true;
    grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
    grid.SortCommand += new GridSortCommandEventHandler(grid_SortCommand);
 
    GridBoundColumn idColumn = new GridBoundColumn();
    grid.MasterTableView.Columns.Add(idColumn);
    idColumn.DataField = "ID";
    idColumn.Display = true;
    idColumn.SortExpression = "ID";
    idColumn.UniqueName = "ID";
    idColumn.HeaderText = "ID";     
 
    GridBoundColumn realNameColumn = new GridBoundColumn();
    grid.MasterTableView.Columns.Add(realNameColumn);
    realNameColumn.DataField = "Name";
    realNameColumn.Display = true;
    realNameColumn.HeaderText = "Name";
    realNameColumn.UniqueName = "Name";
    realNameColumn.SortExpression = "Name";
 
    this.PlaceHolder1.Controls.Add(grid);
}
 
void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    IDAL.IBaseRepository<Model.HS_Docter> doctors = new DAl.BaseRepository<Model.HS_Docter>();
    RadGrid1.DataSource = doctors.GetDataTable("select * from Hs_doctor");  
}
 
void grid_SortCommand(object sender, GridSortCommandEventArgs e)
 {
    IDAL.IBaseRepository<Model.HS_Docter> doctors = new DAl.BaseRepository<Model.HS_Docter>();
          switch (e.OldSortOrder)
          {              
              case GridSortOrder.Ascending:              
                  e.Item.OwnerTableView.DataSource = doctors.GetDataTable("select * from hs_doctor").Select("", e.CommandArgument.ToString() + " asc");
                  break;
 
              case GridSortOrder.Descending:
                  e.Item.OwnerTableView.DataSource = doctors.GetDataTable("select * from  hs_doctor" ).Select("", e.CommandArgument.ToString() + " desc");
                 break;
     
 }

Thanks,
Princy
Tags
Grid
Asked by
zhan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or