我在窗体上(WINFORM)建立了一个DataGridView之后,有提示是与一个数据库的表绑定连接,按照提示操作下来之后,与数据库的一个表已经绑定,(IDE自动设好的),在设计器的下面出现了DataSet,BindingSource,TableAdapter之后
我要是想做三个按钮分别对DataGridView "新增","删除"和"更新" 怎么操作,代码该怎么写?请高手指交,帮我把不知道怎么写的补充完整,谢谢!!!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;namespace HomeAccount
{
    public partial class Income : Form
    {
        DataTable tempTable = new DataTable();
       public DataRow dr;
        
        public Income()
        {
           
            InitializeComponent();
            
        }
        
        private void Income_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“homeAccountsDataSet1.D_FUNDS”中。您可以根据需要移动或移除它。
           string useno="000000000000";
          this.d_FUNDSTableAdapter1.Fill(this.homeAccountsDataSet1.D_FUNDS,useno);//dataGridView1的自动绑定代码!
        
            // TODO: 这行代码将数据加载到表“homeAccountsDataSet.D_FUNDS”中。您可以根据需要移动或移除它。
           this.d_FUNDSTableAdapter.Fill(this.homeAccountsDataSet.D_FUNDS);//dataGridView2的自动绑定代码!
                  
        }      
        private void btnadd_Click(object sender, EventArgs e)
        {
             //对dataGridView新增功能要怎么写? 
             //dataGridView1.Rows.Add(1);我这新写不行,请高手请帮我把功能实现完整!
   
        }         private void btndel_Click(object sender, EventArgs e)
        {
             //对dataGridView1删除功能要怎么写?    
         }
 
         private void btnupdate_Click(object sender, EventArgs e)
        {
             //对dataGridView1更新功能要怎么写?    
        }
    }
}

解决方案 »

  1.   

    DataRow row;
    row = table.NewRow();//以下row[""]对应列名,也可以用索引代替
    row["fName"] = "John";
    row["lName"] = "Smith";

    dataGridView1.Rows.Add(row);
      

  2.   

    DataRow row; 
    row = dataGridView1.NewRow(); //以下row[""]对应列名,也可以用索引代替 
    row["fName"] = "John"; 
    row["lName"] = "Smith"; 

    dataGridView1.Rows.Add(row);还有一种方式(数组行)
    string[] newrow = { "6/30/1992", "3", "Dress", "P J Harvey", "Dry" };
    dataGridView1.Rows.Add(newrow);
     
      

  3.   

    当你的datagridview绑定了数据源之后,你就不能只对datagirdview进行数据行的新增和删除
    你要从数据源入手,在数据源上新增和删除行,然后重新绑定
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace DataSource
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private DataSet ds = new DataSet();
            private SqlConnection conn = null;
            private SqlDataAdapter da = null;
            private const string DRIVER = "server=.;database=northwind;uid=sa;pwd=sa";
            private const string sql_select = "select * from region";        /**
             * 此方法为将数据库northwind中的region表的数据查询出来并放入DataSet中 
            **/ 
            private void Form1_Load(object sender, EventArgs e)
            {
                conn = new SqlConnection(DRIVER);
                da = new SqlDataAdapter(sql_select,conn);
                da.Fill(ds,"table");
                this.dataGridView1.DataSource = ds.Tables["table"].DefaultView;
            }        private bool BtnInsert() //此方法作用于添加
            {
                da.InsertCommand = conn.CreateCommand();
                da.InsertCommand.CommandText = "insert into region values(@id,@ption)";
                da.InsertCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
                da.InsertCommand.Parameters.Add("@ption", SqlDbType.VarChar, 10, "regiondescription");
                int count = da.Update(ds);
                bool result = count > 0 ? true : false;
                return result;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                if (this.BtnInsert())//调用此方法
                {
                    MessageBox.Show("添加成功!");
                }
                else 
                {
                    MessageBox.Show("添加失败!");
                }
            }
            private bool BtnDelect() //此方法作用于删除
            {
                SqlParameter sp = new SqlParameter();
                da.DeleteCommand = conn.CreateCommand();
                da.DeleteCommand.CommandText = "delete region where regionid=@id";
                sp = da.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
                sp.SourceVersion = DataRowVersion.Original;
                ds.Tables["table"].Rows[this.dataGridView1.CurrentRow.Index].Delete();
                int count = da.Update(ds);
                bool result = count > 0 ? true : false;
                return result;
            }
            private void button2_Click(object sender, EventArgs e)
            {
                if (this.BtnDelect())//调用删除方法
                {
                    MessageBox.Show("删除成功!");
                }
                else
                {
                    MessageBox.Show("删除失败!");
                }
            }
            private bool BtnUpdate() //此方法作用于修改
            {
                SqlParameter sp = new SqlParameter();
                da.UpdateCommand = conn.CreateCommand();
                da.UpdateCommand.CommandText = "update region set regionid=@id,regiondescription=@ption where regionid=@oldid";            da.UpdateCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
                da.UpdateCommand.Parameters.Add("@ption", SqlDbType.VarChar, 10, "regiondescription");            sp = da.UpdateCommand.Parameters.Add("@oldid", SqlDbType.Int, 4, "regionid");
                sp.SourceVersion = DataRowVersion.Original;
                
                int count = da.Update(ds);
                bool result = count > 0 ? true : false;
                return result;
            }
            private void button3_Click(object sender, EventArgs e)
            {
                if (this.BtnUpdate())//调用修改方法
                {
                    MessageBox.Show("修改成功!");
                }
                else
                {
                    MessageBox.Show("修改失败!");
                }
            }
        }
    }
      

  5.   

    this.d_FUNDSTableAdapter.Update(this.homeAccountsDataSet.D_FUNDS)增删改全部解决。