请教各位高手:怎样使用 DataGrid 控件添加新记录?加急:在线等!!!! 在DataTable中添加新行,然后再重新绑定到DataGrid 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 请教楼上的,怎么添加新行?能不能帮写个例子呢,我试过了,可是不好用,现在把代码粘上,请指教!谢谢DataRow dr = objDataSet.Tables[0].NewRow(); objDataSet.Tables[0].Rows.InsertAt(dr,2);Session["Operator"] = objDataSet;DataGrid1.EditItemIndex = 1;DataGrid1.DataBind();我这样写是添加新行了,可是没有可编辑的文本框什么的 //*********************************************************************//****************添加模块*********************************************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); } } 谢谢各位的帮助,特别是lifeixie(lifeixie) ,我为你的诚恳感动!虽说我已经找到解决的办法了。 楼主,解决办法是什么??我也很想知道!![email protected] asp.net中事务(transactions)的作用是什么? C#中能实现折叠菜单吗? c# 删除处理! 在点击dataGridview 控件中的数据时,如何显示在文本框中? 如何通过一段字符串实体化一个类?并调用该类中的方法? 我想把数据库中的一条表单信息导入word文档然后打印出现问题! C#与Visual C++ 如何通讯? 各位DX,如何在DATAGRID控件中合并某一列? 请各位高手帮忙,小弟的确没办法了!非常急! 哪位大侠能指点如何做音频,视频?谢谢! .net中怎样编译单独的一个类文件? 未将对象引用设置到对象的实例??
DataRow dr = objDataSet.Tables[0].NewRow();
objDataSet.Tables[0].Rows.InsertAt(dr,2);
Session["Operator"] = objDataSet;
DataGrid1.EditItemIndex = 1;
DataGrid1.DataBind();
我这样写是添加新行了,可是没有可编辑的文本框什么的
//****************添加模块*********************************************
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);
}
}
[email protected]