winform chart控件 案例

工具: freamwork   3.5 版本  vs2010   sql2008

 小女刚刚学习winform报表,如果做的有什么不对的,还望大N们指点指点,嘻嘻

 

代码部分:

namespace MicroStore_B03
{
    public partial class ReferStorage : Form
    {
        //设置全局的数据数据库数据传送桥接对象
        Common.DataCon don = new Common.DataCon();
        Common.DataOperate dot = new Common.DataOperate();

        public ReferStorage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 首次加载展现报表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// yhl  1012添加
        private void ReferStorage_Load(object sender, EventArgs e)
        {
            //绑定仓库数据
            dot.cboxBind("select StoreName from Tb_Storage", "Tb_Storage", "StoreName", comboBox1);
        }

        /// <summary>
        /// 获取数据显示在XY轴
        /// </summary>
        /// yhl    1014添加
        private void bindone(string one ,string two,string three)
        {
            string sqlone = "select  GoodsName,TotalInPrice,TotalOutPrice from VW_baobiao where time between'" + one + "' and '" + two + "' and StoreName='" + three + "'";

            //图表绑定数据源
            DataTable dt = default(DataTable);
            dt = don.getBinder(sqlone, "VW_baobiao");

            //设置图表的数据源
            chart1.DataSource = dt;

            //设置图表Y轴对应项的显示
            chart1.Series[0].YValueMembers = "TotalInPrice";
            chart1.Series[1].YValueMembers = "TotalOutPrice";

            //设置图表X轴对应项
            chart1.Series[0].XValueMember = "GoodsName";

            //绑定数据
            chart1.DataBind();

            //保存图片
            chart1.SaveImage(@"F:\1.PNG", ImageFormat.Bmp);
        }

        /// <summary>
        /// 点击查询  根据查询的仓库  查询日期获得报表信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// yhl  1016添加
        private void button1_Click(object sender, EventArgs e)
        {
            string time1 = dateTimePicker1.Text.Trim();//查询起始时间      ----这里使用的是日期控件
            string  time2= dateTimePicker2.Text.Trim();//查询结束时间
            string cmb = comboBox1.Text;//获取仓库的名称
            if (Convert.ToDateTime(dateTimePicker1.Text) > DateTime.Now)
            {
                errorProvider1.SetError(dateTimePicker1, "日期必须小于或等于当前系统日期!");
                return;
            }
            else
            {
                errorProvider1.Clear();
                if (Convert.ToDateTime(dateTimePicker2.Text) > DateTime.Now)
                {
                    errorProvider1.SetError(dateTimePicker2, "日期必须小于或等于当前系统日期!");
                    return;
                }
                else
                {
                    errorProvider1.Clear();
                    if (Convert.ToDateTime(dateTimePicker1.Text) >= Convert.ToDateTime(dateTimePicker2.Text))
                    {
                        errorProvider1.SetError(dateTimePicker1, "起始日期必须小于或等于结束日期!");
                        return;
                    }
                    else
                    {
                        errorProvider1.Clear();
                        bindone(time1, time2, cmb);
                    }
                }
            }
        }
    }
}

    试用了这两个控件

 

 

公共方法:Datacon   代码部分:

 

        #region  建立数据库连接
        /// <summary>
        /// 建立数据库连接.
        /// </summary>
        /// <returns>返回SqlConnection对象</returns>
        public SqlConnection getcon()
        {
            //string M_str_sqlcon = "Data Source=.; Integrated Security=true; Initial Catalog =DB_StockMS;Persist Security Info=false";

           //呵呵,这里的连接数据库就你自己写咯,祝你成功哦!
            string M_str_sqlcon = "Data Source=***;Initial Catalog=DB_Stock;Persist Security Info=True;User ID=***;Password=***";         
            SqlConnection myCon = new SqlConnection(M_str_sqlcon);
            return myCon;
        }
        #endregion

 

 

        #region  创建表单对象  yhl  0929
        public DataTable getBinder(string sql, string TableName)
        {
            SqlConnection sqlcon = this.getcon();
            SqlDataAdapter adpter = new SqlDataAdapter(sql, sqlcon);
            SqlCommandBuilder scb = new SqlCommandBuilder(adpter);
            DataSet ds = new DataSet();
            adpter.Fill(ds);
            return ds.Tables[0];
        }
        #endregion

 

 这是运行效果:

 winform chart控件 案例_第1张图片

你可能感兴趣的:(数据库,String,Security,报表,WinForm,dataset)