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

RadGrid Bind and export on one click

1 Answer 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gauri
Top achievements
Rank 1
Gauri asked on 31 Oct 2014, 07:54 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;
        }







1 Answer, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 05 Nov 2014, 11:43 AM
Hi Gauri,

If you want to create an Export file without displaying RadGrid control and you can use the ExportInfrastructure and manually generate it. Please examine the following code library which demonstrates how to achieve that.

Regards,
Kostadin
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
Kostadin
Telerik team
Share this question
or