 悬赏园豆:5
                [已关闭问题] 
            
                    关闭于 2012-12-22 22:05
                悬赏园豆:5
                [已关闭问题] 
            
                    关闭于 2012-12-22 22:05
                 
        使用BindingSource绑定数据,,请问新增保存要怎么写。。测试用???
public partial class Form2 : Form
    {
        BindingSource mBindingSource;
        public Form2()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form2_Load);
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.ReadOnly = true;
        }
        void Form2_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Age");
            DataRow drTemp;
            drTemp = dt.NewRow();
            drTemp[0] = "Jordan";
            drTemp[1] = 10;
            dt.Rows.Add(drTemp);
            drTemp = dt.NewRow();
            drTemp[0] = "KOBE";
            drTemp[1] = 20;
            dt.Rows.Add(drTemp);
            mBindingSource = new BindingSource();
            mBindingSource.DataSource = dt;
            this.dataGridView1.DataSource = mBindingSource;
            this.textBox1.DataBindings.Add("Text", mBindingSource, "Name");
            this.textBox2.DataBindings.Add("Text", mBindingSource, "Age");
        }
        //新增
        private void button1_Click(object sender, EventArgs e)
        {
            
        }
        //保存(需要判断是否有更改...是新建的还是编辑的,支持单条更新到dt中)
        private void button2_Click(object sender, EventArgs e)
        {
            
        }
        //删除
        private void button3_Click(object sender, EventArgs e)
        {
            DataRow DeleteRow = ((DataRowView)mBindingSource.Current).Row;
            ((DataTable)mBindingSource.DataSource).Rows.Remove(DeleteRow);
        }
新建一个新的窗体
我是问那两个方法要怎么写!!!
1.新增dataGridView1 新增一行记录
2.保存:参数strSQL 是可以写成 select top 1 * from TEST
public int DataBaseSave(string strSQL, DataTable Da_table)
 {
 SqlConnection myCn = new SqlConnection(ConFiguration.SqlConectionStr);
 try
 {
 myCn.Open();
 SqlCommand selectCMD = new SqlCommand(strSQL, myCn);
 SqlDataAdapter sda = new SqlDataAdapter(selectCMD);
 //上面的语句中使用select 0,不是为了查询出数据,而是要查询出表结构以向DataTable中填充表结构
 sda.Fill(Da_table);
 SqlCommandBuilder scb = new SqlCommandBuilder(sda);
 //执行更新
 //Da_table.GetChanges(); 
 sda.Update(Da_table.GetChanges());
 //使DataTable保存更新
 Da_table.AcceptChanges();
 return 0;
 }
 catch (System.Data.SqlClient.SqlException e)
 {
 throw new Exception(e.Message);
 return -1;
 }
 finally
 {
 myCn.Close();
 }
 }
我的意思是列表还是要显示多条记录的。。只是每次点保存的时候只保存当前行。。如果没有更新就不保存。。。
@彬彬@科比: 加工标记,查询带出来的默认为S,新增的为N,修改的E,保存的时候对表遍历一下!
lz问的太笼统了,别人不好回答啊
dataGridView网上有很多非常详细的使用说明,应该有你需要的