随笔 - 6  文章 - 129  trackbacks - 0
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(14)

随笔档案(6)

文章分类(467)

文章档案(423)

相册

收藏夹(18)

JAVA

搜索

  •  

积分与排名

  • 积分 - 822288
  • 排名 - 49

最新评论

阅读排行榜

评论排行榜

BLL层      

  public void Update(DataTable dt)
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (dr.RowState == DataRowState.Deleted)
                    continue;
                if (dr.RowState == DataRowState.Modified || dr.RowState == DataRowState.Added)
                {
                    if (dr["usr_id"] == DBNull.Value)
                    {
                        throw new Exception("用户代码不能为空");
                    }                }
            }

            dal.Update(dt);
        }

       

DAL层

 public void Update(DataTable dt)
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlConnection conn = new SqlConnection(SqlHelper.ConnectionString);
            conn.Open();

            adapter.InsertCommand = new SqlCommand("INSERT INTO dbo.t6( com_id ,usr_id ) VALUES( @com_id ,@usr_id)", conn);
            adapter.InsertCommand.CommandType = CommandType.Text;
            adapter.InsertCommand.Parameters.Add("@com_id", SqlDbType.VarChar, 4, "com_id");
            adapter.InsertCommand.Parameters.Add("@usr_id", SqlDbType.VarChar, 8, "usr_id");

            adapter.UpdateCommand = new SqlCommand("update t6 setwhereand usr_id", conn);
            adapter.UpdateCommand.CommandType = CommandType.Text;
            adapter.UpdateCommand.Parameters.Add("@com_id", SqlDbType.VarChar, 4, "com_id");
            adapter.UpdateCommand.Parameters.Add("@usr_id", SqlDbType.VarChar, 8, "usr_id");
            adapter.UpdateCommand.Parameters.Add("@usr_id1", SqlDbType.VarChar, 8, "usr_id");
            adapter.UpdateCommand.Parameters["@usr_id1"].SourceVersion = DataRowVersion.Original;

            adapter.DeleteCommand = new SqlCommand("delete from t6 whereand usr_id", conn);
            adapter.DeleteCommand.CommandType = CommandType.Text;
            adapter.DeleteCommand.Parameters.Add("@com_id", SqlDbType.VarChar, 4, "com_id");
            adapter.DeleteCommand.Parameters.Add("@usr_id", SqlDbType.VarChar, 8, "usr_id");

            adapter.DeleteCommand.Parameters["@com_id"].SourceVersion = DataRowVersion.Original;
            adapter.DeleteCommand.Parameters["@usr_id"].SourceVersion = DataRowVersion.Original;

            adapter.Update(dt);
        }

 

前台

    public partial class DataTableUpdate : Form
    {

        public BLL.Users Bu = new BLL.Users();
        public DataTable dtUsr = new DataTable();
        public MOD.Users modObject = new MOD.Users();

        public string _UserId = "";

        public DataTableUpdate()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
        }

//窗口加载

        private void Form1_Load(object sender, EventArgs e)
        {
            dtUsr = Bu.GetList();
            dataGridView1.DataSource = dtUsr;
            BindValue();
        }

        private void BindValue()
        {
            tb_com_id.DataBindings.Add("Text", dataGridView1.DataSource, "com_id");
            tb_UsrId.DataBindings.Add("Text", dataGridView1.DataSource, "usr_id");

            //手动修改可以; 代码赋值需要用下面这句话
            tb_UsrId.DataBindings[0].DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
        }

        private void 保存_Click(object sender, EventArgs e)
        {
            foreach (DataRow dr in dtUsr.Rows)
            {
                dr.EndEdit();
            }
            try
            {
                Bu.Update(dtUsr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            MessageBox.Show("保存成功!", "系统提示");
        }

        private void 删除_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要删除用户:" + tb_UsrName.Text.Trim()+" ?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                return;

            if (dataGridView1.CurrentCell == null)
                return;

            try
            {
                dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
                Bu.Update(dtUsr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            MessageBox.Show("删除成功!", "系统提示");
           
        }

        private void 增加_Click(object sender, EventArgs e)
        {
            DataRow dr = dtUsr.NewRow();
            dr["usr_id"] = _UserId;
            dtUsr.Rows.Add(dr);
            dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
        }

        private void 取消_Click(object sender, EventArgs e)
        {
            dtUsr.RejectChanges();
        }
牛铁:http://blog.csdn.net/fangxinggood/archive/2008/04/18/2304047.aspx



posted on 2014-06-30 15:49 Ke 阅读(1927) 评论(0)  编辑  收藏 所属分类: C#

只有注册用户登录后才能发表评论。


网站导航: