C#/Passing Value in Datagrid to Crystal Report in C#
Expert: Srini Nagarajan - 12/28/2011
QuestionQUESTION: Hi Sir Srini! I just wanna ask how to create a report in c# getting one row or data in my database i'm using MySQL. Please reply. Thanks and God bless
ANSWER: Hi,
What kind a report, do you want to use crystal report? if not you can use grid view, here is the url gives you some idea how to use grid view
http://msdn.microsoft.com/en-us/library/aa479353.aspx
hope it helps.
Cheers
-Srini
---------- FOLLOW-UP ----------
QUESTION: Sir, thanks for that very kind reply. But now I wanna ask how to pass data from datagridview to crystal report? I hope you can give me best codes for this problem. I'm using C# and MySQL. Thank you and God Bless
Answerhi,
Hope this one helps
http://csharpdotnetfreak.blogspot.com/2009/07/creating-crystal-reports-in-aspnet
or
Add this before namespace:
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
Then:
ParameterValues paramValue;
ParameterDiscreteValue discreteValue;
ParameterFieldDefinition fieldDefinition;
CrystalReport1 crystalReport1;
private void btnLoad_Click(object sender, EventArgs e)
{
crystalReport1 = new CrystalReport1();
DataSet1 dataSet1 = new DataSet1(); //Created dataset that contains datatable columns as shown as the below fields..
DataTable u = dataSet1.Tables.Add("DataTable1"); //"DataTable1" is the name of datatable from the created dataset
u.Columns.Add("Qty", Type.GetType("System.String"));
u.Columns.Add("UOM", Type.GetType("System.String"));
u.Columns.Add("Description", Type.GetType("System.String"));
u.Columns.Add("UnitPrice", Type.GetType("System.String"));
u.Columns.Add("Amount", Type.GetType("System.String"));
DataRow s;
int i = 0;
try
{
for (i = 0; i < dgvDesc.Rows.Count; i++)
{
s = u.NewRow();
s["Qty"] = dgvDesc.Rows[i].Cells[Qty.Name].Value.ToString();
s["UOM"] = dgvDesc.Rows[i].Cells[Uom2.Name].Value.ToString();
s["Description"] = dgvDesc.Rows[i].Cells[Description.Name].Value.ToString();
s["UnitPrice"] = dgvDesc.Rows[i].Cells[UnitPrice.Name].Value.ToString();
s["Amount"] = dgvDesc.Rows[i].Cells[Amount2.Name].Value.ToString();
u.Rows.Add(s);
}
}
catch (NullReferenceException) { }
crystalReport1.SetDataSource(dataSet1.Tables[1]);
loadTextboxValue(); //load parametized value of textboxes, labels, etc.
crystalReportViewer1.ReportSource = crystalReport1;
crystalReportViewer1.Refresh();
loadTextboxValue();
}
private void loadTextboxValue()
{
ParameterFieldDefinitions paramFieldDefinitions;
paramValue = new ParameterValues();
discreteValue = new ParameterDiscreteValue();
discreteValue.Value = txtCustomer.Text;
paramFieldDefinitions = crystalReport1.DataDefinition.ParameterFields;
fieldDefinition = paramFieldDefinitions["CustomerName"];
commonParam();
discreteValue.Value = txtAddress.Text;
fieldDefinition = paramFieldDefinitions["Address"];
commonParam();
discreteValue.Value = dtpDate.Text;
fieldDefinition = paramFieldDefinitions["Date"];
commonParam();
discreteValue.Value = txtTotal.Text;
fieldDefinition = paramFieldDefinitions["TotalSales"];
commonParam();
discreteValue.Value = txtTax.TabIndex;
fieldDefinition = paramFieldDefinitions["Tax"];
commonParam();
discreteValue.Value = txtNetTotal.Text;
fieldDefinition = paramFieldDefinitions["NetTotal"];
commonParam();
}
void commonParam()
{
paramValue.Clear();
paramValue.Add(discreteValue);
fieldDefinition.ApplyCurrentValues(paramValue);
}
Hope it helps.