I'm using visual studio 2013 and widows Form and i want to generate reports with report viewer but i'm new on this topic. This is my class wich must be used as object Datasource
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication3
{
public class Result
{
public String epc
{
get;
set;
}
public int nombreEsseyage
{
get;
set;
}
public double dureeEssay
{
get;
set;
}
public String marque
{
get;
set;
}
public String nom
{
get;
set;
}
public String couleur
{
get;
set;
}
String taille
{
get;
set;
}
public Result(String epc1, int nombreEsseyage1, double dureeEssay1, String marque1, String nom1, String couleur1, String taille1)
{
epc = epc1;
nombreEsseyage = nombreEsseyage1;
dureeEssay = dureeEssay1;
marque = marque1;
nom = nom1;
couleur = couleur1;
taille = taille1;
}
}
}
Then I get Data from my DataBase and i added code to calculate 2 fields of the classe result which are nombreessayage et dureeessay. the code is below:
namespace WindowsFormsApplication3
<pre lang="c#"><pre>{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection Cn;
SqlCommand Cmd;
Cn = new SqlConnection(@"Data Source=DESKTOP-ODCFVR1\SQLEXPRESS;Initial Catalog=RFID;Integrated Security=True");
Cn.Open();
Cmd = new SqlCommand("SELECT Hab_essaye_EPC, hab_essaye_time, article_marque, article_nom, article_couleur,article_taille FROM Habits_essayes, Habits where Hab_essaye_EPC = article_epc and article_nom LIKE '%'+ @Nom + '%' and Hab_essaye_EPC NOT IN (Select Hab_vendu_Epc From habits_vendus)", Cn);
Cmd.Parameters.Add("@Nom", SqlDbType.VarChar, 30).Value = textBox1.Text;
SqlDataAdapter da = new SqlDataAdapter(Cmd);
DataTable dataTable = new DataTable();
da.Fill(dataTable);
String epcmoinun = "";
\\Initialization of variables
List<Result> resultats = new List<Result>();
DateTime timemoinun = new DateTime();
double dureeEssayage = 0;
int nombreEssay = 0;
String marqueM = "";
String couleurM = "";
String nomM = "";
String tailleM = "";
foreach (DataRow row in dataTable.Rows)
{
String epcLocal = row["Hab_essaye_EPC"].ToString();
DateTime timeLocal = (DateTime)row["hab_essaye_time"];
String marqueL = row["article_marque"].ToString();
String couleurL = row["article_couleur"].ToString();
String nomL = row["article_nom"].ToString();
String tailleL = row["article_taille"].ToString();
if (timemoinun == null)
{
epcmoinun = epcLocal;
nombreEssay++;
timemoinun = timeLocal;
marqueM = marqueL;
nomM = nomL;
couleurM = couleurL;
tailleM = tailleL;
}
else
{
if (epcmoinun.Equals(epcLocal))
{
marqueM = marqueL;
nomM = nomL;
couleurM = couleurL;
tailleM = tailleL;
var diff = (timeLocal - timemoinun).TotalMinutes;
if (diff <= 2)
{
dureeEssayage = dureeEssayage + diff;
}
else
{
nombreEssay++;
}
timemoinun = timeLocal;
}
else
{
Result rr = new Result(epcmoinun, nombreEssay, dureeEssayage, marqueM, nomM, couleurM, tailleM);
resultats.Add(rr);
epcmoinun = epcLocal;
timemoinun = timeLocal;
dureeEssayage = 0;
nombreEssay = 1;
marqueM = marqueL;
nomM = nomL;
couleurM = couleurL;
tailleM = tailleL;
}
}
}
Result rr2 = new Result(epcmoinun, nombreEssay, dureeEssayage, marqueM, nomM, couleurM, tailleM);
resultats.Add(rr2);
chart1.DataSource = resultats;
chart2.DataSource = resultats;
chart1.Series[0].IsValueShownAsLabel = true;
chart2.Series[0].IsValueShownAsLabel = true;
chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
chart2.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart2.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
foreach (Result tt in resultats)
{
chart1.Series[0].XValueMember = "epc";
chart1.Series[0].YValueMembers = "nombreEsseyage";
chart1.DataBind();
chart2.Series[0].XValueMember = "epc";
chart2.Series[0].YValueMembers = "dureeEssay";
chart2.DataBind();
Cn.Close();
da.Dispose();
}
}
}
}
What I have tried:
I Added then a reportViwer to my project and i selected Result as a dataSource. I inserted then a Chart. Now i want to set the datasource "binding" and i don't know how to do it.
Can someone help me? :)