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

RadGrid Bind and export on one click

3 Answers 69 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gauri
Top achievements
Rank 1
Gauri asked on 03 Nov 2014, 12:33 PM
Looks like if I want to export to excel I first need to bind radgrid and load to  the page and then the user does something to initiate the download.

I am trying to do the bind and export in one event. Thats why I didn't add my grid to the page, I just wanted to leverage its ability to export the data for me. I don't plan on my user ever seeing a bound radgrid.

Is something like this possible? 
public void RunReport_Click(object sender, EventArgs e)
{
    //bind a radgrid in code behind
    //export the radgrid to excel when its done binding
}

Please advise.

My data has dynamic column. I use dynamic query. The code I'm using is
       private void BuildReport(string @SpecID)
        {
            RadGrid gvTemp = new RadGrid();
            
            int db = (int)CommonFunctions.CurrentDatabase();
            DateTime StartDate = UIStartDate.SelectedDate.Value;
            DateTime EndDate = UIEndDate.SelectedDate.Value;

            using (STS32Entities ctx = new STS32Entities())
            {
                SqlConnection caConn = new SqlConnection(CaConn);
                SqlCommand caCmd = new SqlCommand("CreateDynamicQuery", caConn);
                caCmd.CommandType = CommandType.StoredProcedure;
                caCmd.Parameters.Add(new SqlParameter("@DB", db));
                //caCmd.Parameters.Add(new SqlParameter("@SeqNo", SeqNo));
                //caCmd.Parameters.Add(new SqlParameter("@DataVrsn", UIDataVersion.SelectedItem.Text));
                caCmd.Parameters.Add(new SqlParameter("@SpecID", SpecID));
                caCmd.Parameters.Add(new SqlParameter("@DateType", UIEventType.SelectedValue));
                caCmd.Parameters.Add(new SqlParameter("@StartDate", StartDate.ToShortDateString()));
                caCmd.Parameters.AddWithValue("@EndDate", EndDate.ToShortDateString());
                caCmd.Parameters.Add(new SqlParameter("@HarvestCode", UIHarvestCode.Checked));

                caConn.Open();
                SqlDataReader caRdr = caCmd.ExecuteReader();

                gvTemp.DataSource = caRdr;
                gvTemp.DataBind();
                this.Form.Controls.Add(gvTemp);
                //Download.Enabled = true;
            }
            gvTemp.ExportSettings.IgnorePaging = true;
            gvTemp.ExportSettings.FileName = "DQRecords";
            gvTemp.ExportSettings.OpenInNewWindow = true;
            gvTemp.ExportSettings.Csv.FileExtension = "xls";
            gvTemp.ExportSettings.Csv.ColumnDelimiter = GridCsvDelimiter.Comma;
            gvTemp.MasterTableView.ExportToCSV();
            gvTemp.Visible = false;
        }

3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 06 Nov 2014, 07:57 AM
Hello Gauri,

You could use a PlaceHolder control placed somewhere on the page, create and bound the RadGrid programmatically within the OnClick event handler of the button and add it to the Controls collection of the PlaceHolder. Following is a simple example demonstrating that approach:
<telerik:RadButton runat="server" ID="RadButton1" Text="Export data" OnClick="RadButton1_Click"></telerik:RadButton>
<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>

And the code-behind:
protected void RadButton1_Click(object sender, EventArgs e)
{
    RadGrid grid = new RadGrid();
    grid.DataSource = GetGridData();
    grid.DataBind();
    grid.ExportSettings.IgnorePaging = true;
    grid.ExportSettings.FileName = "DQRecords";
    grid.ExportSettings.OpenInNewWindow = true;
    grid.ExportSettings.Csv.FileExtension = "xls";
    grid.ExportSettings.Csv.ColumnDelimiter = GridCsvDelimiter.Comma;
    PlaceHolder1.Controls.Add(grid);
    grid.MasterTableView.ExportToCSV();
}
 
private DataTable GetGridData()
{
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("FirstName", typeof(string));
    table.Columns.Add("LastName", typeof(string));
    table.Columns.Add("Age", typeof(int));
    table.Columns.Add("Date", typeof(DateTime));
    table.Columns.Add("BoolValue", typeof(Boolean));
    for (int i = 0; i < 555; i++)
    {
        table.Rows.Add(i, "FirstName" + i, "LastName" + i, 20 + i, DateTime.Now.AddDays(i), i % 2 == 0);
    }
 
    return table;
}

Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Gauri
Top achievements
Rank 1
answered on 06 Nov 2014, 03:57 PM
Thanks for your reply. But it creates the excel with only header and no data. That was happening before too when I was adding the control directly to the page.
0
Konstantin Dikov
Telerik team
answered on 07 Nov 2014, 03:41 PM
Hello Gauri,

Can you please that you have tested the provided sample and that you are observing the problem that you are describing with it. On my end, the dummy data is correctly exported.

If you continue to experience that problem, please elaborate which version of the controls you are using. Additionally, if the example from my previous post is working correctly, please modify it in such manner, so it could replicate the problem from your actual project or open a regular support ticket with sample, runnable project replicating the issue. Thus we will be able to inspect it locally.


Best Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Gauri
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Gauri
Top achievements
Rank 1
Share this question
or