c#语言报表和图表。

打印报表操作步骤:

1 创建dataset数据集

2 创建rdlc报表文件

3 在窗体中创建reportviewer视图,指定具体数据源

4 给dataset里的datatablede 的对象进行赋值。

参考代码:

 private void Form1_Load(object sender, EventArgs e)

        {

          //连接数据库

            SqlConnection coon = new SqlConnection("server=.;database=master;user=sa;pwd=123");

            SqlCommand cmd = coon.CreateCommand();

            coon.Open();  

           cmd.CommandText = "select*from student ,score where student.sno=score.sno";

            SqlDataReader dr = cmd.ExecuteReader();   

          if (dr.HasRows)

            {

                while (dr.Read ())

                {  

                 //给dataset里的datatable赋值

                    DataRow row = DataSet1.lianjiebiao.NewRow();  

                    row["sno"]=dr["sno"];

                    row["ssex"] = dr["ssex"];

                    row["sname"] = dr["sname"];

                    row["sbirthdy"] = dr["sbirthdy"];

                    row["class"] = dr["class"];

                    row["cno"] = dr["cno"];

                    row["degree"] = dr["degree"];

                    DataSet1.lianjiebiao.Rows.Add(row);    //row为行集合

               }  

           }  

           coon.Close();   

          this.reportViewer1.RefreshReport();

        }

      图表:柱状图,把数据库查出来的数据集放在一个泛型集合里,然后把这个泛型集作为参数赋给柱状图。参考代码如下:

 public partial class Form1 : Form

    {

        private DataClasses1DataContext Context;  //LINQ to SQL类,连接数据库查询

        public Form1()

        {

            InitializeComponent();

            Context = new DataClasses1DataContext();

        }

        private void Form1_Load(object sender, EventArgs e)  

       {

           //car为表名,take(5)只显示其中五列

            List<Car> list = Context.Car.Take(5).ToList();

            //为图表指定数据源

            chart1.DataSource = list;

            chart1.Series["Series1"].XValueMember = "Name";

            chart1.Series["Series1"].YValueMembers = "Oil";

            chart1.Series["Series2"].XValueMember = "Name";

            chart1.Series["Series2"].YValueMembers = "powers";

            //下面是线状图显示

            chart1.Series["Series2"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;

        }    

}

    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

        private void Form1_Load(object sender, EventArgs e)         {             List<Car> list = Context.Car.Take(5).ToList();             //为图表指定数据源             chart1.DataSource = list;             chart1.Series["Series1"].XValueMember = "Name";             chart1.Series["Series1"].YValueMembers = "Oil";             chart1.Series["Series2"].XValueMember = "Name";             chart1.Series["Series2"].YValueMembers = "powers";             //下面是线状图显示             chart1.Series["Series2"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;         }     } }

你可能感兴趣的:(C#)