Gridview Columns to Excel - export

I'm using GridView to show data. And for exporting Gridview data into excel format,
I'm using a button - "Export to Excel". The downloading into Excel format or
Exporting into Excel is working perfectly. That GridView is showing total of 15 Columns.
But in the excel document its showing only 14 columns. What might be the problem?

Try this coding
In Button click
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
Page.EnableViewState = false;
Response.AddHeader("Content-Disposition", "attachment;filename=report.xls");
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
and add the event
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
I hope the following link will help you
Export-GridView-to-Excel-without-losing-gridlines-in-Excel-file-in-ASPNet
Solution-ASPNet-GridView-Export-to-Excel-The-file-you-are-trying-to-open-is-in-a-different-format-than-specified-by-the-file-extension

Related

How to write condition on Filter String in code Reports Winforms devexpress?

I designed a report form and map fields with columns of tables from dataset. Now I need to set condition, so I used FilterString. Inside report form DetailReport, I wrote code like this:
DetailReport.FilterString = "[InvoiceNumber] = " + temp;
Now I need this same code (FilterString) for whole form that is top left of form Report Task some properties are available in that Data Source there we add dataset, Data Member, Data Adapter then FilterString is available.
Now I am able to add FilterString in Designer but I need to add FilterString in code ??
I Tried this one but not working
FilterString= "[InvoiceNumber] = " + temp"
I seggest you set the report FilterString property when you call for your XtraReport (for example : when you click on the print button of your form). Here is an code example :
private void simpleButton1_Click(object sender, EventArgs e) {
// Create a report instance.
XtraReport1 report = new XtraReport1();
// Some code like setting the report datasource
// Specify the report's filter string.
report.FilterString = "[InvoiceNumber] = myValue";
// Show the report's print preview.
pt.ShowPreviewDialog();
}
This code was taken from the DevExpress online documentation article : XtraReportBase.FilterString Property
Once you set the filter for the report, in the for you need to have it do:
public string p = "";
....
using (XtraReport_yourreport x = new XtraReport_yourreport ())
{
p = x.FilterString;
}

To open new Excel sheet from Silverlight

I creating Silverlight App, while exporting grid into excel, I need to open a new Excel sheet but I was not able to open a new Excel within silverlight. I am using Telerik controls, in their examples , they are saving a new excel and then exoporting data. But my client need not want the save operation performed before exporting the Grid data.
The flow should be like the one below:
1. Open new excel (Excel should be in front of the screen)
2. export data
3. Saving the excel is enduser 's choice .
The end user may or may not save the Excel sheet according to their need.
Can any one help me to solve this problem.
Thank You
private void button8_Click(object sender, RoutedEventArgs e)
{
dynamic excelApp;
excelApp = AutomationFactory.CreateObject("Excel.Application");
excelApp.Visible = true;
dynamic workbook = excelApp.workbooks;
workbook.Add();
dynamic sheet = excelApp.ActiveSheet;
dynamic cell = null;
int index = 1;
foreach (unite emp in dataGrid1.ItemsSource)
{
cell = sheet.Cells[index, 1];
cell.Value = emp.unite_description;
cell = sheet.Cells[index, 2];
//cell.Value = emp.EmployeeId;
//cell = sheet.Cells[index, 3];
//cell.Value = emp.Department;
index++;
}
}
This is what I found when I had the same problem as you and it's working as you asked. (Declaring variables as dynamic is probably not necessary)
Use excellite library. it is an appropriate free tool for reading and writing Excel files.
you can find it hear: http://excellite.codeplex.com/

How to modify a DataTable's Query Generated in the designer with code

I have a RDLC file with a dataset Created in the designer and the report loads ok no errors.
My problem is the query in the DataTable is alway's the same.
I would like to change that query in the designer but haveb't been able.
I also have tried to create the dataset in code and then add it yo the RDLC file but I was not successful to add the Dataset to the RDLC.
Thanks
Try this if you want to load a custom DataTable inside your report:
//clean old dataset
yourReport.LocalReport.DataSources.Clear();
DataTable yourDataTable = null;
yourDataTable = loadDataTableFromYourDataSource();
ReportDataSource datasource = new ReportDataSource("YourDataSourceNameFoundInsideTheRdlc", yourDataTable);
yourReport.LocalReport.DataSources.Add(datasource);
//refresh the report
yourReport.LocalReport.Refresh();
hope it helps

ReportViewer databinding (WPF)

I have a rdlc file and I try to show it using report viewer control in WPF. My DataSource is called 'OfficeDataSet' and DataSet used in report is called 'Address'. I try to initialize report viewer (I put it in WindowsFormsHost) like this:
private void PrepareReport()
{
reportDataSource = new ReportDataSource();
officeDataSet = new OfficeDataSet();
adapter = new AddressTableAdapter();
adapter.Fill(officeDataSet.Address);
reportDataSource.Name = "OfficeDataSet_Address";
reportDataSource.Value = officeDataSet.Address;
viewerInstance.LocalReport.DataSources.Add(reportDataSource);
viewerInstance.LocalReport.ReportEmbeddedResource = "WpfApplication1.Reports.TestReport.rdlc";
viewerInstance.Location = new System.Drawing.Point(0, 0);
viewerInstance.RefreshReport();
}
but I got the message "A data source instance has not been supplied for the data source 'Address'".
Anybody know what can be wrong with this code? (I'm totally new in WPF and Reporting Services).
If i remember well, the datasource into the report file should have the same name (or maybe value) as the reportDataSource. So in your report file the datasource should be called "OfficeDataSet_Address".

How to set the ColumnIndex of a newly added DataGridViewButton column

I have a really annoying issue with a button cell in a DataGridView control. I'm binding the grid to a dataset at runtime. Some of the rows in the grid will be linked to pdf documents. I create a button column and add it to the grid, then I loop through the rows and based on the value of a certain column I set the text of the cell in the button column. When I step through the code I can see the ColumnIndex of the button column is 10. However when the form appears, the button text values for the rows I want are blank.
When I click the button I check in the CellContentClick event to see if the ColumnIndex is 10 (which is the button column) it tells me the ColumnIndex is 0, even though it's the last column. Then when I reload the grid I call the BindHistoryGrid method again which drops the column if it exists and re-adds it. This time it sets the button text correctly. Is there some strange behavior going on that I can't see? How do I set the button ColumnIndex to 10 the first time I add it (even though it tells me that it's 10)?
private DataGridViewButtonColumn PDFButtonColumn;
private void BindHistoryGrid()
{
dataGridViewStmt.DataSource = ah.getAccountHistory(0, dateTimePicker1.Value, dateTimePicker2.Value);
if (dataGridViewStmt.Columns["GetPDFFile"] != null)
dataGridViewStmt.Columns.Remove("GetPDFFile");
dataGridViewStmt.Columns[0].DisplayIndex = 0;
dataGridViewStmt.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewStmt.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
dataGridViewStmt.Columns[0].Visible = false;
dataGridViewStmt.Columns[1].Visible = false;
dataGridViewStmt.Columns.Add(PDFButtonColumn);
dataGridViewStmt.RowHeadersVisible = false;
dataGridViewStmt.ReadOnly = true;
dataGridViewStmt.AllowUserToAddRows = false;
foreach (DataGridViewRow row in dataGridViewStmt.Rows)
{
//if (((string)row.Cells[5].Value).Contains("Invoice"))
if (((int)row.Cells[9].Value) > 0)
{
((DataGridViewButtonCell)(row.Cells[10])).Value = "Get Invoice";
}
else
{
((DataGridViewButtonCell)(row.Cells[10])).Value = "";
}
}
}
private void dataGridViewStmt_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 10 && dataGridViewStmt.CurrentRow.Cells[6].Value != System.DBNull.Value)
{
string pdfFile = "";
int docID = 0;
pdfFile = (string)dataGridViewStmt.CurrentRow.Cells[5].Value + ".pdf";
docID = (int)dataGridViewStmt.CurrentRow.Cells[9].Value;
if (docID > 0)
{
getPDFFile(docID, pdfFile, "pdf");
}
else
{
MessageBox.Show("No invoice available for this item"; }
}
}
I called my bindGrid() method from the two place one after the InitializeComponent() in form's constructor as well as from form1_load(). it works for me.
hope this will also helps you.
I didn't get any replies here so I posted on another forum. I eventually got an answer of sorts, but the whole thing is still pretty vague. The answer I got stated that in order to preserve resources, the grid doesn't always refresh itself. An example is if you have a form with a tab control that has 2 tabs, place a grid on the 1st tab and set column properties after binding in Form Load. This will work. However, when you place the grid on the 2nd tab, using the same binding won't work:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/99ab9fbf-9eaa-4eef-86b8-8f4e49fa81c5
I still haven't found out how or when it decides to preserve resources, if there's a way to bypass this behaviour, if this behaviour is documented anywhere etc. If anyone can throw more light on it I'm all ears.
I had the very same issue. I originally had a DataGridView on a separate form and it worked perfectly with the button column - which I add in code after setting the datasource. However, when I decided to move the grid onto another form with a Tabbed Control (on to the Tab(2) page as it happens), the button column index kept reverting to zero. It looked perfectly OK on the grid of course, i.e. in the correct physical location, and if I stepped through the code in debug mode the Index didn't change, but when I ran the program it did change! Very frustrating.
I solved it by setting the tab page to the page that my grid was located BEFORE setting the datasource.
My simple process was like this (I use VB10):
TabControl1.SelectedIndex = 2 ' this is where the datagridview is
MyGrid.DataSource = Nothing
MyGrid.Columns.Clear
' I execute an Sql command into a DataReader, then fill a DataTable and then assign it to the grid
MyGrid.DataSource = MyDataTable
' Now add button column
Dim btnCol as New DatGridViewButtonColumn
MyGrid.Columns.Add(btnCol)

Resources