在DataTable中添加新行,然后再重新绑定到DataGrid

解决方案 »

  1.   

    请教楼上的,怎么添加新行?能不能帮写个例子呢,我试过了,可是不好用,现在把代码粘上,请指教!谢谢
    DataRow dr = objDataSet.Tables[0].NewRow();

    objDataSet.Tables[0].Rows.InsertAt(dr,2);
    Session["Operator"] = objDataSet;
    DataGrid1.EditItemIndex = 1;
    DataGrid1.DataBind();
    我这样写是添加新行了,可是没有可编辑的文本框什么的
      

  2.   

    //*********************************************************************
    //****************添加模块*********************************************
    private void button1_Click(object sender, System.EventArgs e)
    {

    { try
    {
    if(txtNO.Text=="" || txtPrice.Text =="" || txtName.Text=="" || txtCB.Text=="" || txtSort.Text=="")
    {
    MessageBox.Show("请输入数据后添加","图书管理系统",MessageBoxButtons.OK,MessageBoxIcon.Information  );
    return;
    }
    else
    {
    //添加记录
    OleDbConnection myConn = new OleDbConnection( @"Integrated Security=SSPI;Packet Size=4096;Data Source=localhost;Tag with column collation when possible=False;Initial Catalog=Book;Use Procedure for Prepare=1;Auto Translate=True;Persist Security Info=False;Provider=""SQLOLEDB.1"";Workstation ID=FEI;Use Encryption for Data=False" );
    myConn.Open();
    string strInsert = " INSERT INTO cc_students( stu_no,stu_addr,stu_name,stu_DS,stu_sort,stu_addtime ) VALUES('"
    + txtNO.Text +"','"
    + txtPrice.Text +"','"
    + txtName.Text +"','"
    + txtCB.Text +"','"+txtSort.Text+"','"+txtTime.Text+"')";
    //MessageBox.Show(strInsert);
    OleDbCommand myCmd = new OleDbCommand(strInsert,myConn);
    myCmd.ExecuteNonQuery();
    myConn.Close();
    MessageBox.Show("成功添加图书" + txtNO.Text + "的信息,请点击‘重新加载数据’以显示信息!","图书管理系统",MessageBoxButtons.OK,MessageBoxIcon.Information );
    //this.Close();
    this.clear();
    }

    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message,"图书管理系统",MessageBoxButtons.OK,MessageBoxIcon.Information );
    }
    }
    } private void groupBox1_Enter(object sender, System.EventArgs e)
    {

    }
    public void clear()
    {
    this.txtCB.Text="";
    this.txtName.Text="";
    this.txtNO.Text="";
    this.txtPrice.Text="";
    this.txtSort.Text="";
    } private void frmAdd_Load(object sender, System.EventArgs e)
    {

    }
    }
    }
    //*********************************************************************
    //*********************************************************************
    主窗体
    //********************************************************************
    private string sql="select * from cc_students";
    private string cnn=@"Integrated Security=SSPI;Packet Size=4096;Data Source=localhost;Tag with column collation when possible=False;Initial Catalog=Book;Use Procedure for Prepare=1;Auto Translate=True;Persist Security Info=False;Provider=""SQLOLEDB.1"";Workstation ID=FEI;Use Encryption for Data=False";

    private System.Data.DataSet ccDS;
    private System.Data.OleDb.OleDbDataAdapter ccDA,ccDA2;
    private System.Data.OleDb.OleDbCommandBuilder  ccCMD;
    private System.Windows.Forms.BindingManagerBase mybind; /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void label13_Click(object sender, System.EventArgs e)
    {

    } private void groupBox7_Enter(object sender, System.EventArgs e)
    {

    } private void groupBox1_Enter(object sender, System.EventArgs e)
    {

    } private void groupBox2_Enter(object sender, System.EventArgs e)
    {

    }
    private void Form1_Load(object sender, System.EventArgs e)
    {

    try
    {

    this.ccDS = new DataSet();
    this.ccDA = new System.Data.OleDb.OleDbDataAdapter(sql,cnn);
    this.ccDA.Fill(this.ccDS,"cc_students");
    this.ccCMD = new System.Data.OleDb.OleDbCommandBuilder(this.ccDA);
    this.mybind = this.BindingContext[this.ccDS,"cc_students"];
    this.dataGrid1.DataSource = this.ccDS;
    this.dataGrid1.DataMember = "cc_students";
    this.textBox1.Text=mybind.Count.ToString();
    //绑定控件
    this.txtNO.DataBindings.Add("Text",this.ccDS,"cc_students.stu_no");
    this.txtPrice.DataBindings.Add("Text",this.ccDS,"cc_students.stu_addr");
    this.txtName.DataBindings.Add("Text",this.ccDS,"cc_students.stu_name");
    this.txtCB.DataBindings.Add("Text",this.ccDS,"cc_students.stu_DS");
    this.txtSort.DataBindings.Add("Text",this.ccDS,"cc_students.stu_sort");
    this.txtTime.DataBindings.Add("Text",this.ccDS,"cc_students.stu_addtime");


    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }

    } private void btnPre_Click(object sender, System.EventArgs e)
    {
    if (this.mybind.Position>0)
    this.mybind.Position-=1;
    else
    MessageBox.Show("当前记录已经是第一条","图书管理系统",MessageBoxButtons.OK,MessageBoxIcon.Information );
    } private void btnNext_Click(object sender, System.EventArgs e)
    {
    if (this.mybind.Position<this.mybind.Count-1)
    this.mybind.Position+=1;
    else
    MessageBox.Show("当前记录已经是最后一条","图书管理系统",MessageBoxButtons.OK,MessageBoxIcon.Information );
    } private void btnDelete_Click(object sender, System.EventArgs e)
    {
    Delete();
    }

    public void Delete()
    {
    // TODO:  添加 UserControl1.Delete 实现
    //MessageBox.Show("Test?ok");
    if(this.txtNO.Text =="")
    {
    MessageBox.Show("没有数据可以被删除","图书管理系统",MessageBoxButtons.OK,MessageBoxIcon.Information  );
    return;
    }
    string msg = "您确定要删除选定的记录吗?"; if(MessageBox.Show(msg, "图书管理系统", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No )
    return ;
    try
    {
    //连接到数据库
    string strCon = @"Integrated Security=SSPI;Packet Size=4096;Data Source=localhost;Tag with column collation when possible=False;Initial Catalog=Book;Use Procedure for Prepare=1;Auto Translate=True;Persist Security Info=False;Provider=""SQLOLEDB.1"";Workstation ID=FEI;Use Encryption for Data=False";
    OleDbConnection myConn = new OleDbConnection ( strCon ) ;
    myConn.Open ( ) ;
    string strDele = "DELETE FROM cc_students WHERE stu_no= '" + this.txtNO.Text +"'" ;
    OleDbCommand myCommand = new OleDbCommand ( strDele , myConn ) ;
    //从数据库中删除指定记录
    myCommand.ExecuteNonQuery ( ) ;
    //从DataSet中删除指定记录
    this.ccDS.Tables [ "cc_students" ] . Rows [ mybind.Position ] . Delete ( ) ;
    this.ccDS.Tables [ "cc_students" ] . AcceptChanges ( ) ;
    myConn.Close ( ) ;
    }
    catch ( Exception ed )
    {
    MessageBox.Show ( "删除记录错误信息: " + ed.ToString ( ) , "图书管理系统",MessageBoxButtons.OK,MessageBoxIcon.Information  ) ;
    }
    } private void btnFind_Click(object sender, System.EventArgs e)
    {
    if(this.txtKeyWord.Text == "")
    {
    MessageBox.Show("查询关键字不能为空!","图书管理系统",MessageBoxButtons.OK,MessageBoxIcon.Information );
    return;
    } try
    {
    string sql2;
    sql2 = "SELECT * FROM cc_students WHERE stu_no like '%" + this.txtKeyWord.Text + "%'";
    this.ccDA2 = new System.Data.OleDb.OleDbDataAdapter(sql2,cnn);
    this.ccDS.Clear();
    this.ccDA2.Fill(this.ccDS,"cc_students");
    this.txtKeyWord.Text = "";
    }
    catch (Exception e1)
    {
    MessageBox.Show(e1.Message);
    }
    } private void btnLoad_Click(object sender, System.EventArgs e)
    {
    this.ccDS.Clear();
    this.ccDA.Fill(this.ccDS,"cc_students");
    } private void btnSave_Click(object sender, System.EventArgs e)
    {
    //信息合法性检查
    if (txtNO.Text=="" || txtPrice.Text =="" || txtName.Text=="" || txtCB.Text=="" || txtSort.Text=="")
    {
    MessageBox.Show("提交的信息不完全!","图书管理系统",MessageBoxButtons.OK,MessageBoxIcon.Information );
    return;
    }
    try
    {
    this.dataGrid1.EndInit();
    this.mybind.EndCurrentEdit();
    this.ccDA.Update(this.ccDS,"cc_students");
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
      

  3.   

    谢谢各位的帮助,特别是lifeixie(lifeixie) ,我为你的诚恳感动!虽说我已经找到解决的办法了。
      

  4.   

    楼主,解决办法是什么??我也很想知道!!
    [email protected]