本帖最后由 szyjp 于 2009-10-14 11:48:08 编辑

解决方案 »

  1.   

    呵呵,可能这个问题问的人太多了,所以都没有人回答了;
    功夫不负有心人,我已在书上找到解决办法了.
    现将代码公布在下面:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace temp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            SqlDataAdapter Adapter = null;
            DataSet ds = new DataSet();
            public static string sqlComm = "SELECT a.userid, a.[user_name], a.[password], a.admin, a.del, a.[add], a.[update], a.crdate, b.[user_name] as cruser FROM J_adminuser AS a , J_adminuser AS b where  a.cruser = b.userid";
            public static SqlConnection cn = new SqlConnection("server=IT01;database=book;uid=sa;pwd=123");
            public static string userid = "";
            public static int RowID =-1;
            
            private void Form1_Load(object sender, EventArgs e)
            {
                getdate();
            }        private void getdate()
            {
                //将数据填充到dataGridView1中:
                Adapter = new SqlDataAdapter(sqlComm,cn);
                Adapter.Fill(ds,"adminuser");
                this.dataGridView1.DataSource=ds.Tables[0];
            
            }        private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
            {
             
            }
            //点击保存按钮,执行插入语句和修改语句:
            private void butSave_Click(object sender, EventArgs e)
            {            try
                {
                    SqlCommand Insert = new SqlCommand("insert into j_adminuser(userid,[user_name],[password],admin,del,[add],[update]) values (@userid,@user_name,@password,@admin,@del,@add,@update)", cn);
                    Insert.Parameters.Add("@userid", SqlDbType.NVarChar, 10, "userid");
                    Insert.Parameters.Add("@user_name", SqlDbType.NVarChar, 20, "user_name");
                    Insert.Parameters.Add("@password", SqlDbType.NVarChar, 20, "password");
                    Insert.Parameters.Add("@admin", SqlDbType.Bit, 1, "admin");
                    Insert.Parameters.Add("@del", SqlDbType.Bit, 1, "del");
                    Insert.Parameters.Add("@add", SqlDbType.Bit, 1, "add");
                    Insert.Parameters.Add("@update", SqlDbType.Bit, 1, "update");
                                    SqlCommand Update = new SqlCommand("update j_adminuser set [user_name]=@user_name,[password]=@password,admin=@admin,del=@del,[add]=@add,[update]=@update where userid=@userid", cn);
                    Update.Parameters.Add("@user_name", SqlDbType.NVarChar, 20, "user_name");
                    Update.Parameters.Add("@password", SqlDbType.NVarChar, 20, "password");
                    Update.Parameters.Add("@admin", SqlDbType.Bit, 1, "admin");
                    Update.Parameters.Add("@del", SqlDbType.Bit, 1, "del");
                    Update.Parameters.Add("@add", SqlDbType.Bit, 1, "add");
                    Update.Parameters.Add("@update", SqlDbType.Bit, 1, "update");
                    Update.Parameters.Add("@userid", SqlDbType.NVarChar, 10, "userid");                Adapter.InsertCommand = Insert;
                    Adapter.UpdateCommand = Update;
                    SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(Adapter);
                    Adapter.Update(ds.Tables[0]);
                     //清空数据源
                    ds.Tables[0].Clear();
                     //刷新dataGridView1中的数据
                    getdate();
                }
                catch (DataException se)
                {
                    MessageBox.Show(se.ToString());
                }
            }        //当用户添加行时,给复选框一个默认值:
            private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
            {
               int i = this.dataGridView1.CurrentRow.Index;
               this.dataGridView1.Rows[i].Cells[3].Value = false;
               this.dataGridView1.Rows[i].Cells[4].Value = false;
               this.dataGridView1.Rows[i].Cells[5].Value = false;
               this.dataGridView1.Rows[i].Cells[6].Value = false;
            }
            //删除记录:
            private void butDel_Click(object sender, EventArgs e)
            {
                if (userid!="")
                {
                    DialogResult Result = MessageBox.Show("确定要删除:"+userid+"这个用户吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
                    if (Result==DialogResult.OK)
                    {                    ds.Tables[0].Rows[RowID].Delete();
                        SqlCommand Delete = new SqlCommand("delete from j_adminuser where userid='"+userid+"'", cn);
                        //Delete.Parameters.Add("@userid", SqlDbType.NVarChar, 10, "userid");                    Adapter.DeleteCommand = Delete;
                        SqlCommandBuilder Builder = new SqlCommandBuilder(Adapter);
                        Adapter.Update(ds.Tables[0]);
                        ds.Tables[0].Clear();
                        MessageBox.Show("删除成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
                        userid = "";
                        getdate();
                        MessageBox.Show("已刷新!");
                    }
                }
                else
                {
                    MessageBox.Show("请先选择要删除的记录!","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Asterisk);
                }
            }        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                userid = "";
                RowID = this.dataGridView1.CurrentRow.Index;
                userid = Convert.ToString(this.dataGridView1.Rows[RowID].Cells[0].Value);
              
            }        private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
            {
                
            }
            
        }
    }