将数据绑定到控件(TextBox、ComboBox),BindingNavigator控件、DataGridView的使用及编程、PropertyGrid组件

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
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值