Print DatagridView to rdlc Report - c#

How to print items from dataGridview , to rdlc Report.
The items in the dataGridView are filled manually, and not from database, that's why this gives me issues.
p.s i know there are ways to print it with PrintForm but i want to print it with .rdlc.
Tryed with DataTable but it doenst show me the right colums on the
RDLC Dataset
DataSet ds = new DataSet();
DataTable dt = new DataTable("ProdFromDGV");
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dt.Columns.Add(col.HeaderText);
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataRow dRow = dt.NewRow();
foreach (DataGridViewCell cell in row.Cells)
{
dRow[cell.ColumnIndex] = cell.Value;
}
dt.Rows.Add(dRow);
}
ds.Tables.Add(dt);
dt.WriteXml("table.xml");`

Related

DataGridView without DataSource to XML string

I have some DataGridViews on a form, and would like to get a XML string from it.
But for some reason it gives "Exception thrown: 'System.NullReferenceException'" at the point of return.
When I look inside the foreach rows, it has the data I have put in.
What is the problem here?
public string DataGridViewToXML(DataGridView DGV)
{
DataTable DT = new DataTable();
foreach (DataGridViewColumn col in DGV.Columns) { DT.Columns.Add(col.Name); }
foreach (DataGridViewRow row in DGV.Rows)
{
DataRow dRow = DT.NewRow();
foreach (DataGridViewCell cell in row.Cells) { dRow[cell.ColumnIndex] = cell.Value; }
DT.Rows.Add(dRow);
}
return DT.DataSet.GetXml();
}
You haven't told as at what line it trows it but as i get it you copy all data from DGV to DT so you can parse DataSet.GetXml();?
If that is the case why you just do not do:
public string DataGridViewToXML(DataGridView DGV)
{
return (DGV.DataSource as DataTable).DataSet.GetXml();
}
I know it doesn't solve your null error but why would you solve it when you have easier way of doing you command. Other way tell me at what line error drops and i will try explaining why it happens.

How to print data from a GridView in Crystal Reports using Winforms?

I have a DataGridView populated using index numbers of Rows and Columns (No Dataset or DataSource)
e.g:
dataGridView1[0, 0].Value = "ABC";
dataGridView1[0, 1].Value = "XYZ";
Now I want it to be printed on Crystal Report. How do I provide dataSource to Crystal Report or simply how can I achieve it?
As well as data from GridView, I also need to print some other data from textboxes.
I have tried this kind of application.
First, I export to XML the datasource of the datagridview
Second, I used this XML as the source for the crystal reports.
void TransferDataToReport()
{
DataTable dT = GetDataTableFromDGV(dataGridView1);
dSet.Tables.Add(dT);
try
{
dSet.AcceptChanges();
dSet.WriteXml(#"C:\Data.xml", XmlWriteMode.WriteSchema);
}
catch (FileLoadException) { }
ReportDocument cr = new ReportDocument();
string filePath = #"C:\CrystalReportData.rpt";
cr.Load(filePath);
cr.SetDataSource(dSet);
crystalReportViewer1.ReportSource = cr;
}
public DataTable GetDataTableFromDGV(DataGridView dgv)
{
DataTable dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns)
{
dt.Columns.Add(column.Name, column.ValueType);
}
object[] cellValues = new object[dgv.Columns.Count];
foreach (DataGridViewRow row in dgv.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
cellValues[i] = row.Cells[i].Value;
}
dt.Rows.Add(cellValues);
}
return dt;
}

Remove a Datarow from a Datatable

I have a windows application in which I am going to delete a datarow from a datatable.
But I got an exception.
The given DataRow is not in the current DataRowCollection.
The code:
DataTable dt = new DataTable();
DataRowView currentDataRowView = (DataRowView)DataGridView1.CurrentRow.DataBoundItem;
DataRow row = currentDataRowView.Row;
dt.Rows.Remove(row); // exception here.
DataGridView1.DataSource = dt;
The datatable dt's information as the image shown.
I think that I already have casted datarowview to datarow.
EDIT:
dt was created from another DataGridView.
foreach (DataGridViewRow row in DatGridView2.Rows)
{
DataGridViewCheckBoxCell check = row.Cells[0] as DataGridViewCheckBoxCell;
if (check.Value != null)
{
if ((bool)check.Value)
{
//this row has a checkBox set to true (tick is added)
//add this row to dataTable ...
DataRow myRow = (row.DataBoundItem as DataRowView).Row;
DataRow dr = dt.NewRow();
dr[0] = myRow[0];
dr[1] = myRow[1];
dr[2] = myRow[2];
dr[3] = myRow[3];
if (!dt.Rows.Contains(dr[0]))
{
dt.Rows.Add(dr);
}
}
}
I don't think you can reference a different DataTable than the one where CurrentRow.DataBoundItem is coming from.
Your dt DataTable is being constructed from DataGridView2 but CurrentRow.DataBoundItem is coming from DataGridView1.
You will have to find the matching row in DataGridView1 yourself before you can delete it.
I have tried your code, and it's working with no problem:
DataTable dt = (DataTable)dataGridView1.DataSource;
DataRowView currentDataRowView = (DataRowView)dataGridView1.Rows[0].DataBoundItem;
DataRow row = currentDataRowView.Row;
dt.Rows.Remove(row);
dataGridView1.DataSource = dt;
So as i have wrote above, the error "The given DataRow is not in the current DataRowCollection" means that your trying to delete a row that is not in the DataTable "dt".

Filling Datatable with DataGridView data in VB/C# .NET

How can I fill Datatable with DataGridView data (VB/C# .NET)?
Assuming that it is a Winform
below code can be of help
//dgv is the name of your data grid view.
DataTable dt = new DataTable();
DataColumn[] dcs = new DataColumn[]{};
foreach (DataGridViewColumn c in dgv.Columns)
{
DataColumn dc = new DataColumn();
dc.ColumnName = c.Name;
dc.DataType = c.ValueType;
dt.Columns.Add(dc);
}
foreach (DataGridViewRow r in dgv.Rows)
{
DataRow drow = dt.NewRow();
foreach (DataGridViewCell cell in r.Cells)
{
drow[cell.OwningColumn.Name] = cell.Value;
}
dt.Rows.Add(drow);
}

How to make a DataTable from DataGridView without any Datasource?

I want to get a DataTable from DataGridView of the Grid values.
In other words DataTable same as DataGridView Values
Might be a nicer way to do it but otherwise it would be fairly trivial to just loop through the DGV and create the DataTable manually.
Something like this might work:
DataTable dt = new DataTable();
foreach(DataGridViewColumn col in dgv.Columns)
{
dt.Columns.Add(col.Name);
}
foreach(DataGridViewRow row in dgv.Rows)
{
DataRow dRow = dt.NewRow();
foreach(DataGridViewCell cell in row.Cells)
{
dRow[cell.ColumnIndex] = cell.Value;
}
dt.Rows.Add(dRow);
}
You can cast the DataSource object from the DataGridView to a DataTable
DataTable dt = new DataTable();
dt = (DataTable)dataGridView1.DataSource;
you can use the following code also, this code fill not effect on your DataGridView when you do some add or delete rows in the datatable
DataTable dt = new DataTable();
dt = Ctype(dataGridView1.DataSource,DataTable).copy();
You can try as follow:
using System.Data;
using System.Windows.Forms;
namespace BLL
{
public class Class_DataGridview_To_DataTable
{
public static DataTable UDF_Convert_DataGridView_To_Datatable(DataGridView ImpGrd)
{
DataTable ExportDataTable = new DataTable();
foreach (DataGridViewColumn col in ImpGrd.Columns)
{
ExportDataTable.Columns.Add(col.Name);
}
foreach (DataGridViewRow row in ImpGrd.Rows)
{
DataRow dRow = ExportDataTable.NewRow();
foreach (DataGridViewCell cell in row.Cells)
{
dRow[cell.ColumnIndex] = cell.Value;
}
ExportDataTable.Rows.Add(dRow);
}
return ExportDataTable;
}
}
}

Categories