using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BindExp
{
public partial class BindForm : Form
{
private SqlDataAdapter sda = new SqlDataAdapter();
private DataTable da;//= new DataTable();
public BindForm()
{
InitializeComponent();
}
#region 页面加载时调用方法
private void BindForm_Load(object sender, EventArgs e)
{
ComboxInit();
}
#endregion
#region 控件事件
/// <summary>
/// ComboxBox所选定的值发生变化时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbxCustomer_SelectedIndexChanged(object sender, EventArgs e)
{
string sName = cbxCustomer.SelectedItem.ToString();
string strSql1 = string.Format("select CUSTOMER_NAME,CUSTOMER_EMAIL,CUSTOMER_REMARK from CUSTOMER where CUSTOMER_NAME='{0}'",sName);
InitPrGrid(strSql1);
Bind(sName);
}
#endregion
#region 自定义方法
/// <summary>
/// PropertyGrid组件的绑定
/// </summary>
/// <param name="strSql"></param>
protected void InitPrGrid(string strSql)
{
DataSet ds = DB.GetInstance().GetDataSet(strSql);
//创建并初始化customer对象
Customer customer = new Customer();
customer.Name = ds.Tables[0].Rows[0]["CUSTOMER_NAME"].ToString();
customer.Email = ds.Tables[0].Rows[0]["CUSTOMER_EMAIL"].ToString();
customer.Mark = ds.Tables[0].Rows[0]["CUSTOMER_REMARK"].ToString();
prCustomer.SelectedObject = customer;//PropertyGrid控件的绑定
}
/// <summary>
/// 初始化ComboxBox
/// </summary>
protected void ComboxInit()
{
string strSql = "select CUSTOMER_NAME from CUSTOMER";
DataSet ds = DB.GetInstance().GetDataSet(strSql);
cbxCustomer.Items.Clear();
for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
{
cbxCustomer.Items.Add(ds.Tables[0].Rows[i]["CUSTOMER_NAME"].ToString());
}
cbxCustomer.SelectedIndex = 0;
}
/// <summary>
/// BindingNavigator控件、DataGridView、TextBox
/// </summary>
/// <param name="strSql"></param>
protected void Bind(string sName)
{
da = new DataTable();
string strSql = string.Format("select CASH_ID,CASH_HANDLER as '操作人',CASH_MONEY as '金额',CASH_REMARK as '备注' from CASH where CASH_HANDLER='{0}'", sName);
SqlConnection sqlCon = new SqlConnection("server=.;integrated security=SSPI;database=CashRecord");
SqlCommand cmd = new SqlCommand(strSql,sqlCon);
sda.SelectCommand = cmd;
sda.Fill(da);
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = da;
this.CashDataGrid.DataSource = bindingSource1;
this.CashDataGrid.Columns["CASH_ID"].Visible = false;
this.txtHandler.DataBindings.Clear();
this.txtMoney.DataBindings.Clear();
this.txtRemark.DataBindings.Clear();
this.txtHandler.DataBindings.Add("Text", bindingSource1, "操作人");
this.txtMoney.DataBindings.Add("Text", bindingSource1, "金额");
this.txtRemark.DataBindings.Add("Text", bindingSource1, "备注");
this.bindingNavigator1.BindingSource = bindingSource1;
}
/// <summary>
/// 对新建,删除,编辑的保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, EventArgs e)
{
try
{
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
this.CashDataGrid.EndEdit();
this.CashDataGrid.CurrentCell = null;
sda.Update(da);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("更新成功!");
}
#endregion
}
}