Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a working code below that transfers data from a dataGridView to a Crystal Report using a dataset. Am using for loop to loop through dataGridView Rows. Problem is that when i have too much data on the dataGridView, and i run this code, i experience a flickering effect and it takes such a long time to finish populating the report. My quess is that, that happens because of the for loop. Is there a way i can transfer the data to crystal report without using a for loop or maybe a better solution to my flickering and time taken problem??
Thanks in advance for any help granted.
Here is my code
C#
DataSet4 ds = new DataSet4();
DataTable t = ds.Tables.Add("stock");
t.Columns.Add("Item_Name", Type.GetType("System.String"));
t.Columns.Add("Store", Type.GetType("System.String"));
t.Columns.Add("Total_Stock", Type.GetType("System.String"));
t.Columns.Add("Product_Code", Type.GetType("System.String"));


DataRow r;

string ItemName = string.Empty;
string sto = string.Empty;
string totostok = string.Empty;
string pcode = string.Empty;


for (int i = 0; i < dataGridView1.RowCount; i++)
{
    pcode = dataGridView1.Rows[i].Cells[0].Value.ToString();
    ItemName = dataGridView1.Rows[i].Cells[1].Value.ToString();
    sto = dataGridView1.Rows[i].Cells[2].Value.ToString();
    totostok = dataGridView1.Rows[i].Cells[3].Value.ToString();


    r = t.NewRow();
    r["Product_Code"] = pcode;
    r["Item_Name"] = ItemName;
    r["Store"] = sto;
    r["Total_Stock"] = totostok;

    t.Rows.Add(r);

    CrystalReport4 objRpt = new CrystalReport4();
    objRpt.SetDataSource(ds.Tables[1]);
    crystalReportViewer1.ReportSource = objRpt;
    crystalReportViewer1.Dock = DockStyle.Fill;
    crystalReportViewer1.Refresh();
    crystalReportViewer1.Show();
}
Posted
Comments
CHill60 21-Jan-13 10:32am    
Is your datagrid view bound to a database or are you populating each row dynamically?
If the former then you can just get dataGridView1.DataSource and pass that to the Crystal Report.
It also looks like you're creating a new report for each row of the datagrid - why not create the datatable first then populate the CrystalReport outside the loop passing it the datatable
Xonel 21-Jan-13 13:38pm    
Thank you CHill60 for the response
dataGridView is bound to a database, kindly how do i pass dataGridView1.DataSource to the Crystal report? The column heads of the Crystal report are those declared in the dataset. Some code snippets will greatly help me out.

you can allways transfer data from datagrdview using dataset

define a dataset the can update data from datagridview

the set the crystalreport datasource to that dataset
 
Share this answer
 
Comments
Xonel 29-Jan-13 11:25am    
Hi alamree748, thanks for the reply...I tried that like in my code snippet above but anytime the loop completes my report refreshes in a flickering manner and does that until the whole report is out...That's my major problem...That flickering effect. I thought it was because of the loop, and i wanna find a better way to have my report generated....Please give some code snippets to exactly understand your solution...Thank you
alamree 2-Feb-13 8:11am    
i have some code for you but i need to ask you
the data in the crystal report where to read from
is there a table in database
or you will pass them as values
or ado.net dataset
whats your datasource in your case
alamree 2-Feb-13 15:46pm    
Xonel how about the solution try it and tell me
Xonel 6-Feb-13 1:55am    
I have tried pass the data from datagridview directly to the report but invain...Seems like i still have to use the for loop like in my code snippet in the question. Or is it possible for me to pass the data directly from the database to the report?? Because that creation of a new report on each datagridview row is my major concern.....Thanks in advance
this is it
first you will work with ado.net tools will be easy for you
add new Dataset from
project Add New Item
Then Add Your Colums To \The dataSet Created In your solution Directory

Then If You Hhave A datagridView Set The Datasource From The peoperies to the dataset you created\
now in the crystal report you will need to specify the datasource which will be your dataset

in the button_click which will generate your report write these code

dataSet11.AcceptChanges();

CrystalReport1 objRpt = new CrystalReport1();
objRpt.SetDataSource(dataSet11.Tables[0]);
crystalReportViewer1.ReportSource = objRpt;
C#



no further code needed

full source example
download this solution
http://yourcodes.info/attachments/crystalreport.zip[^]



XML
hello bro

i  have recorded video for you to see how you can do this download the video from the link

i hope this solves your problem



<a href="www.yourcodes.info/crystal.wmv">download video</a>

Best Regards
 
Share this answer
 
v2
Comments
Xonel 6-Feb-13 1:51am    
Hi,
Thank you and sorry for the late reply...been trying out your advice but am getting this infor messagebox
One or more projects in the solution could not be loaded for the following reasons;
The project file or web has been moved, renamed or is not on your computer
These projects will be labeled as unavailable in Solution Explorer. Expand the project node to show the reason the project could not be loaded
chima kalu 14-Apr-13 6:25am    
I converted the code to vb.net, but it is only returning one value from the datagridview

This is the code:
Dim TypeOfFees As String
Dim Amount As String
Dim AmountInWords As String

Dim t As New DataTable
Dim r As DataRow

'Dim ds1 As DataSet1 = New DataSet1
't = ds1.Tables.Add("Fee")
t.Columns.Add("TypeOfFee", Type.GetType("System.String"))
t.Columns.Add("Amount", Type.GetType("System.String"))
t.Columns.Add("AmountInWords", Type.GetType("System.String"))

For i As Integer = 0 To DataGridView2.Rows.Count - 1
TypeOfFees = DataGridView2.Rows(i).Cells("TypeOfFee").Value
Amount = DataGridView2.Rows(i).Cells("Amount").Value
AmountInWords = DataGridView2.Rows(i).Cells("AmountInWords").Value

r = t.NewRow()

r(0) = TypeOfFees
r(1) = Amount
r(2) = AmountInWords

t.Rows.Add(r)

Dim rptobj As New CrystalReport6
rptobj.Database.Tables(0).SetDataSource(t)
PrintOtherFees.CrystalReportViewer1.ReportSource = rptobj
PrintOtherFees.CrystalReportViewer1.Dock = DockStyle.Fill
PrintOtherFees.CrystalReportViewer1.Refresh()
PrintOtherFees.CrystalReportViewer1.Show()
Next
hello bro

i have recorded video for you to see how you can do this download the video from the link

i hope this solves your problem



download video

Best Regards
 
Share this answer
 
Comments
Xonel 21-Feb-13 5:41am    
Thanks...Lemmi Look at it...Will inform you on the results

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900