New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Programmatic Binding

RadSpreadsheet can be bound programmaticaly. This example shows binding to a DataTable object.

The declaration of the RadSpreadsheet object:

ASP.NET
<telerik:RadSpreadsheet runat="server" ID="RadSpreadsheet1"></telerik:RadSpreadsheet>

In the Page_Load event handler, create and fill the DataTable object, use it to fill the created Worksheet with data and add the Worksheet to the RadSpreadsheet's Sheets collection.

C#
using System.Data;
using Telerik.Web.Spreadsheet;

protected void Page_Load(object sender, EventArgs e)
{
    DataTable data = GetData();
    var sheet1 = FillWorksheet(data);
    RadSpreadsheet1.Sheets.Add(sheet1);
}

public DataTable GetData()
{
    DataTable table = new DataTable();

    table.Columns.Add("Product", typeof(string));
    table.Columns.Add("Price", typeof(int));
    table.Columns.Add("Quantity", typeof(int));

    table.Rows.Add("Product1", 100, 2);
    table.Rows.Add("Product2", 150, 10);
    table.Rows.Add("Product3", 120, 5);
    table.Rows.Add("Product4", 300, 10);

    return table;
}	

private static Worksheet FillWorksheet(DataTable data)
{
    var workbook = new Workbook();
    var sheet = workbook.AddSheet();
    
    sheet.Columns = new List<Column>();
    
    var row = new Row() { Index = 0 };
    int columnIndex = 0;

    foreach (DataColumn dataColumn in data.Columns)
    {
        sheet.Columns.Add(new Column());

        string cellValue = dataColumn.ColumnName;

        var cell = new Cell() { Index = columnIndex++, Value = cellValue, Bold = true };

        row.AddCell(cell);
    }

    sheet.AddRow(row);

    int rowIndex = 1;
    foreach (DataRow dataRow in data.Rows)
    {
        row = new Row() { Index = rowIndex++ };

        columnIndex = 0;
        foreach (DataColumn dataColumn in data.Columns)
        {
            string cellValue = dataRow[dataColumn.ColumnName].ToString();

            var cell = new Cell() { Index = columnIndex++, Value = cellValue };

            row.AddCell(cell);
        }

        sheet.AddRow(row);
    }

    return sheet;
}

See Also

In this article
See Also
Not finding the help you need?
Contact Support