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

Sort Grid imported from Excel

2 Answers 67 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eloi
Top achievements
Rank 1
Eloi asked on 10 Dec 2009, 11:43 AM
Help.
I can't identify what happen with the sort capability in my routines.
I would like that the data sort occurr in the client-side.
Thanks
Eloi
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CSExcel.aspx.cs" Inherits="CSExcel" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
    Namespace="System.Web.UI" TagPrefix="asp" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>Import Excel Data into GridView</title> 
</head> 
 
<body> 
    <form id="form1" runat="server">  
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnableTheming="True">  
    </telerik:RadScriptManager> 
    <div> 
        <asp:FileUpload ID="FileUpload1" runat="server" /> 
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /> 
        <br /> 
        <asp:Label ID="Label1" runat="server" Text="Has Header ?"></asp:Label> 
        <asp:RadioButtonList ID="rbHDR" runat="server">  
            <asp:ListItem Text="Yes" Value="Yes" Selected="True"></asp:ListItem> 
            <asp:ListItem Text="No" Value="No"></asp:ListItem> 
        </asp:RadioButtonList> 
    </div> 
    <br /> 
    <div> 
        <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True" 
            GridLines="Horizontal" OnSortCommand="RadGrid1_SortCommand" PageSize="50">  
            <MasterTableView AutoGenerateColumns="True">  
                <DetailTables> 
                    <telerik:GridTableView runat="server" Width="100%">  
                        <Columns> 
                            <telerik:GridBoundColumn SortExpression="CodAluno" HeaderText="CodAluno" HeaderButtonType="TextButton" 
                                DataField="CodAluno" /> 
                            <telerik:GridBoundColumn SortExpression="CodTurma" HeaderText="CodTurma" HeaderButtonType="TextButton" 
                                DataField="CodTurma" /> 
                            <telerik:GridBoundColumn SortExpression="NomeAluno" HeaderText="NomeAluno" HeaderButtonType="TextButton" 
                                DataField="NomeAluno" /> 
                            <telerik:GridBoundColumn SortExpression="EmailAluno" HeaderText="EmailAluno" HeaderButtonType="TextButton" 
                                DataField="EmailAluno" /> 
                        </Columns> 
                    </telerik:GridTableView> 
                </DetailTables> 
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                <SortExpressions> 
                    <telerik:GridSortExpression FieldName="CodAluno" SortOrder="Ascending" /> 
                </SortExpressions> 
            </MasterTableView> 
        </telerik:RadGrid> 
    </div> 
    </form> 
</body> 
</html> 
 
using System;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using System.Data;  
using System.Data.OleDb;  
using System.IO;  
using System.Configuration;  
using System.Drawing;  
using System.Text;  
using Telerik.Web.UI;  
 
public partial class CSExcel : System.Web.UI.Page   
{  
    public string FileName;  
 
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
    }  
    protected void btnUpload_Click(object sender, EventArgs e)  
    {  
        if (FileUpload1.HasFile)  
        {  
            string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);  
            string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);  
            string FolderPath = ConfigurationManager.AppSettings["FolderPath"];  
            string FilePath = Server.MapPath(FolderPath + FileName);  
            FileUpload1.SaveAs(FilePath);  
            Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text);  
        }  
    }  
    //  
    private void Import_To_Grid(string FilePath, string Extension, string isHDR)  
    {  
        string conStr="";  
        switch (Extension)  
        {  
            case ".xls"//Excel 97-03  
                conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;  
                break;  
            case ".xlsx"//Excel 07  
                conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;  
                break;  
        }  
        conStr = String.Format(conStr, FilePath, isHDR);  
        OleDbConnection connExcel = new OleDbConnection(conStr);  
        OleDbCommand cmdExcel = new OleDbCommand();  
        OleDbDataAdapter oda = new OleDbDataAdapter();  
        DataTable DadosImportados = new DataTable();   
        cmdExcel.Connection = connExcel;  
 
        //Get the name of First Sheet  
        connExcel.Open();  
        DataTable dtExcelSchema;  
        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);  
        string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();  
        connExcel.Close();  
 
        //Read Data from First Sheet  
        connExcel.Open();  
        cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";  
        oda.SelectCommand = cmdExcel;  
        oda.Fill(DadosImportados);  
        connExcel.Close();   
 
        //Bind Data to GridView  
        RadGrid1.DataSource = DadosImportados;  
        RadGrid1.DataBind();  
    }  
    //  
      
    //  
    protected void PageIndexChanging(object sender, GridViewPageEventArgs e)  
    {  
        string FolderPath = ConfigurationManager.AppSettings["FolderPath"];  
        string Extension = Path.GetExtension(FileName);  
        string FilePath = Server.MapPath(FolderPath + FileName);  
 
        Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text);  
        RadGrid1.CurrentPageIndex = e.NewPageIndex;  
        RadGrid1.DataBind();  
 
    }  
    //protected void RadGrid1_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)  
    //{  
    //    GridSortExpression expression = new GridSortExpression();  
    //    expression.FieldName = "CodAluno";  
    //    expression.SortOrder = GridSortOrder.Descending;  
    //    RadGrid1.MasterTableView.SortExpressions.AddSortExpression(expression);  
    //    RadGrid1.MasterTableView.Rebind();   
    //}  
    //  
    protected void RadGrid1_SortCommand(object source, GridSortCommandEventArgs e)  
    {  
        GridTableView tableView = e.Item.OwnerTableView;  
         
            e.Canceled = true;  
            GridSortExpression expression = new GridSortExpression();  
            expression.FieldName = "EmployeeID";  
            if (tableView.SortExpressions.Count == 0 ||  
                tableView.SortExpressions[0].FieldName != "EmployeeID")  
            {  
                expression.SortOrder = GridSortOrder.Descending;  
            }  
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Descending)  
            {  
                expression.SortOrder = GridSortOrder.Ascending;  
            }  
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Ascending)  
            {  
                expression.SortOrder = GridSortOrder.None;  
            }  
            tableView.SortExpressions.AddSortExpression(expression);  
            tableView.Rebind();  
          
    }   
 
}  
 

 

2 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 15 Dec 2009, 01:19 PM
Hi esartori,

 I noticed in your code, that you are setting the datasource for the grid, and calling .DataBind() directly. Please, keep in mind that this is not a recommended approach, and it would break the default functionalities of the control, such as sorting and filtering. To provide data for the control, you need to use the NeedDataSource event handler. Whenever you want to pass new data to the control, you can use the .Rebind() method, which would in turn cause the NeedDataSource event handler to be raised.
I hope this approach gets you started properly.


Regards,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Eloi
Top achievements
Rank 1
answered on 16 Dec 2009, 10:51 AM
Thanks Yavor.
Tags
Grid
Asked by
Eloi
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Eloi
Top achievements
Rank 1
Share this question
or