首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

textbox与datagirdview结合新增

2012-06-23 
textbox与datagirdview组合新增想做一个批量新增的页面,页面由两个textbox一个datagirdview还有两个按钮,

textbox与datagirdview组合新增

想做一个批量新增的页面,页面由两个textbox一个datagirdview还有两个按钮,新增的时候在textbox1、textbox2里填数据,另外也在datagirdview里填数据,datagirdview可以填多行数据,点击保存就把数据查到数据库里。datagirdview里有多少行数据插入到数据textbox就有多少条数据插入到数据库里。如图
表:
create table test3
(
id int identity (1,1) not null,
 [入仓单编号] int,
[经手人] nvarchar(8),
[物品名称] nvarchar(8),
[单价] int,
[数量] int,
[金额] money
)
批量的代码已写出,但是和textbox一起插入数据就不知道怎写了。
麻烦高手解决,帮忙修改一下代码
public partial class Form5 : Form
  {
  public Form5()
  {
  InitializeComponent();
  }
  SqlConnection conn;
  SqlDataAdapter adapter;
  private DataTable dbconn(string strSql)
  {
  conn.Open();
  this.adapter = new SqlDataAdapter(strSql, conn);
  DataTable dtSelect = new DataTable();
  int rnt = this.adapter.Fill(dtSelect);
  conn.Close();
  return dtSelect;
  }
  private void button1_Click(object sender, EventArgs e)
  {
  if (dbInsert())
  {
  MessageBox.Show("新增成功!");
  }
  }
  private Boolean dbInsert()
  {
  conn = new SqlConnection("Data Source=.;Initial Catalog=CS;Persist Security Info=True;User ID=sa;Password=rt");
  string strSql = "select GoodsName,StorageID,GoodsID,UnitPrice,Amount,SumPrice,Note,HandlePeople from TbShInDetail";
  DataTable dtInsert = new DataTable();
  dtInsert = this.dbconn(strSql);
  dtInsert.Rows.Clear();
  DataTable dtShow = new DataTable();
  dtShow = (DataTable)this.dataGridView1.DataSource;
  for (int i = 0; i < dtShow.Rows.Count; i++)
  {
  dtInsert.ImportRow(dtShow.Rows[i]);
  }
  try
  {
  this.conn.Open();
  SqlCommandBuilder CommandBuiler;
  CommandBuiler = new SqlCommandBuilder(this.adapter);
  this.adapter.Update(dtInsert);
  this.conn.Close();
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message.ToString());
  return false;
  }
  dtInsert.AcceptChanges();
  return true;
  }
  private void button3_Click(object sender, EventArgs e)
  {
  conn = new SqlConnection("Data Source=.;Initial Catalog=CS;Persist Security Info=True;User ID=sa;Password=rt");
  SqlDataAdapter sda = new SqlDataAdapter("select GoodsID as '物品编号',GoodsName as '物品名称',UnitPrice as '单价',Amount as '数量',SumPrice as 总价,Note as '备注' from TbShInDetail where InDetailID=0", conn);
  DataSet ds = new DataSet();
  sda.Fill(ds);
  dataGridView1.DataSource = ds.Tables[0];
  dataGridView1.RowHeadersVisible = false;
  for (int i = 0; i < dataGridView1.ColumnCount; i++)
  {
  dataGridView1.Columns[i].Width = 84;
  }
  button2.Enabled = false;
  dataGridView1.Columns[0].ReadOnly = true;
  }
  }
}


[解决办法]
参考代码。
conn = new SqlConnection(@"Data Source=.\sql2005;Initial Catalog=CS;Persist Security Info=True;User ID=sa;Password=strong");


string strSql = "select [物品名称],[单价],[数量],[金额] from test2 "; DataTable dtShow = new DataTable();
dtShow = (DataTable)this.dataGridView2.DataSource;
try
{
this.conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
foreach(DataRow dr in dtShow.Rows)
{
strSql = string.Format( "INSERT INTO test2 (入仓单编号,经手人,物品名称,单价,数量,金额) values ('{0}','{1}','{2}','{3}','{4}','{5}')", textBox1.Text.Trim(), textBox2.Text.Trim(), dr["物品名称"], dr["单价"], dr["数量"], dr["金额"] );

cmd.CommandText = strSql;
cmd.ExecuteNonQuery();
 } 
this.conn.Close();
}
catch (Exception ex){ }
return true;

热点排行