Click here to Skip to main content
15,895,799 members

Transfering data from dataGridView to Crystal Report without using a for loop

Xonel asked:

Open original thread
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();
}
Tags: C#, Windows, Windows Forms

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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