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;
}
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;
}