winform chart控制項 案例

來源:互聯網
上載者:User

工具: 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

 

 這是運行效果:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.