工具: 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
這是運行效果: