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

[Solved] How to pass a Grid control reference to a Menu Action

1 Answer 115 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Craig Gibbons
Top achievements
Rank 1
Craig Gibbons asked on 11 Oct 2010, 12:23 PM
Hi,

I'm using a Grid and Menu on the same page. The Menu has an item which fires an Action that exports the Grid to an Excel file, as per the sample provided here: http://www.telerik.com/support/kb/aspnet-mvc/grid/export-to-excel.aspx

This code looks something like the following:

<%

Html.Telerik().Menu()

.Name(

 

"clientsmenu")

 

.Items(items =>

{

items.Add()

.Text(

 

 

"Export")

 

.ImageUrl(

 

 

"/Content/Images/Icons/xls.png")

 

.Action(

 

 

"ExcelExport", "Clients", new { page = 1, orderBy = "~", filter = "~" });

 

})

.Render();

%>

Additionally, the controller looks like this:

 

 

 

 

public ActionResult ExcelExport(int page, string orderBy, string filter)

 

{

 

}

This is all fine and well, the Action fires and the Save file dialog opens to save the Excel file. However, the code is severely limited because in the controller method, I have to set column widths and field values. It would be great if I could pass a reference to the Grid control in the routeValues of the Action and then interrogate the grid for it's columns, widths and bindings, something like:

.Action(

 

"ExcelExport", "Clients", new { page = 1, orderBy = "~", filter = "~", grid = this.FindControl("clientsgrid") });

Unfortunately nothing comes through at the controller. Can anybody pls advise how this might be possible?

Thanks

 

 

 

1 Answer, 1 is accepted

Sort by
0
Craig Gibbons
Top achievements
Rank 1
answered on 28 Oct 2010, 04:20 PM
I came up with what is probably an ok second-prize solution to the problem. Instead of interrogating the grid for infromation about it's defintions and bindings, I'm just passing in the IQueryable<> and some column definitions, then using reflection to extract the property values. Hopefully this is helpful to somebody.


public class ExcelColumnDefinition
{
    public ExcelColumnDefinition(int width, string headerText, string propertyName) : this(width, headerText, propertyName, string.Empty)
    {
    }
    public ExcelColumnDefinition(int width, string headerText, string propertyName, string format)
    {
        Width = width;
        HeaderText = headerText;
        PropertyName = propertyName;
        Format = format;
    }
    public int Width { get; set; }
    public string HeaderText { get; set; }
    public string PropertyName { get; set; }
    public string Format { get; set; }
}
public class ExcelExportHelper
{
    public static byte[] Export<T>(IQueryable<T> data, IList<ExcelColumnDefinition> columns)
    {
        GridModel model = data.ToGridModel(1, int.MaxValue, "~", string.Empty, "~");
        var items = model.Data.Cast<T>();
        var workbook = new HSSFWorkbook();
        var sheet = workbook.CreateSheet();
        var headerRow = sheet.CreateRow(0);
        for (int i = 0; i < columns.Count; i++)
        {
            sheet.SetColumnWidth(i, 256 * columns[i].Width);
            headerRow.CreateCell(i).SetCellValue(columns[i].HeaderText);
        }
        //(Optional) freeze the header row so it is not scrolled
        sheet.CreateFreezePane(0, 1, 0, 1);
        int n = 0;
        double d = 0;
        DateTime dt = DateTime.Now;
        int rowNumber = 1;
        foreach (var item in items)
        {
            var row = sheet.CreateRow(rowNumber++);
            var type = item.GetType();
            for (int i = 0; i < columns.Count; i++)
            {
                var info = type.GetProperty(columns[i].PropertyName);
                if (info.CanRead)
                {
                    string value = string.Empty;
                    if (info.PropertyType == n.GetType())
                    {
                        value = Convert.ToInt32(info.GetValue(item, null)).ToString(columns[i].Format);
                    } else if (info.PropertyType == d.GetType())
                    {
                        value = Convert.ToDouble(info.GetValue(item, null)).ToString(columns[i].Format);
                    }
                    else if (info.PropertyType == dt.GetType())
                    {
                        value = Convert.ToDateTime(info.GetValue(item, null)).ToString(columns[i].Format);
                    }
                    else
                    {
                        value = info.GetValue(item, null).ToString();
                    }
                    row.CreateCell(i).SetCellValue(value);
                }
            }
        }
        MemoryStream output = new MemoryStream();
        workbook.Write(output);
        return output.ToArray();
    }
}

Tags
Grid
Asked by
Craig Gibbons
Top achievements
Rank 1
Answers by
Craig Gibbons
Top achievements
Rank 1
Share this question
or