本人拉了一个datagridview控件,想制作一张表格,没有绑定数据库的,可以直接在单元格里面填写内容,填完后,通过一个button单击事件,把整张表的内容插入到数据库中,把表格对应的列对应地插入到数据库中对应的列,请问这代码怎么写?小弟是新手,刚刚接触c#,还请各位牛人多帮小弟解决疑惑,谢谢!
我现在是在做一张入库单,把表填完后,点击入库,就把表格中的数据插入到数据库中对应的表,表格的列我添加好了,至于行是没有固定的,请问该怎么弄?而且入库后,怎么使表格清空回原来的状态,就像一张空表格?
我现在是在做一张入库单,把表填完后,点击入库,就把表格中的数据插入到数据库中对应的表,表格的列我添加好了,至于行是没有固定的,请问该怎么弄?而且入库后,怎么使表格清空回原来的状态,就像一张空表格?
标准做法是搞个DataAdapter,并设置其Deletecommand/insertcommand/selectcommand/updatecommand,为各个操作写好sql语句。然后做数据绑定。当然,可以搞一个空的datatable(或者从数据库里select出来的),column都设置好,绑上gv,在gv上的操作,会反映到绑定的dt中,然后可以判断dt里每一行的rowstate来判断该行是新增还是删除或者是添加等。然后分别处理
IList<Employee> ListEmp = new List<Employee>();
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
Employee emp = new Employee();
emp.ID = Convert.ToInt32(this.dataGridView1.Rows[i].Cells["ID"].Value);
emp.Name = (string)this.dataGridView1.Rows[i].Cells["Name"].Value;
ListEmp.Add(emp);
}//循环读取数据,封装入List,然后在数据访问层循环插入
//查询数据,返回一个List,再循环填充进DataGridView
for (int i = 0; i < ListEmp.Count; i++)
{
this.dataGridView1.Rows.Add(1);
this.dataGridView1.Rows[i].Cells["ID"].Value = ListEmp[i].ID;
this.dataGridView1.Rows[i].Cells["Name"].Value = ListEmp[i].Name;
}
先将dataGridView1.AllowUserToAddRows = true;把dataGridView1和数据集关联,从数据库中查出空集,这样就只显示列名(也就是清空表格了)。
然后在dataGridView1中写上数据,至于添加数据库就是循环了。
1: 用DataSet取出并暂存dataGridView中的数据 然后循环遍历这个DataSet用SQL语句将对应数据存入数据库
2: 用 集合 取出并暂存dataGridView中的数据 然后循环遍历这个集合用SQL语句将对应数据存入数据库
for(int i=0;i<DataGridView1.RowCount;i++)
{
string str1 = DataGridView1.Rows[i].Cells[0].Value.ToString();
string str2 = DataGridView1.Rows[i].Cells[1].Value.ToString();
string str3 = DataGridView1.Rows[i].Cells[2].Value.ToString(); string strSql = "insert into 表名(字段1,字段2,字段3) values('"+str1+"','"+str2+"','"+str3+"')"; //执行你的sql操作
//....
}
Sqlconnect connection=new Sqlconnect();//创建连接对象
SqlDataAdpter dataAdapter=new SqlDataAdter(sql,connection);
SqlCommandBuilder builder=new SqlCommandBuilder(dataAdapter);
dataAdapter.Update(dataSet,"数据库表名");
public class BatchNoUseCard
{ #region Private Elements
private string s_id;
private string s_CardSerial;
private string s_CardType;
private int n_Money;
private int n_Month;
#endregion Private Elements #region Properties
public string Id
{
set { this.s_id = value; } get { return this.s_id; }
}
public string CardSerial
{
get { return this.s_CardSerial; }
set { this.s_CardSerial = value; }
} public string CardType
{
get { return this.s_CardType; }
set { this.s_CardType = value; }
} public int Month
{
get { return this.n_Money; }
set { this.n_Money = value; }
}
public int Money
{
get { return this.n_Month; }
set { this.n_Month = value; }
} #endregion Properties #region Constructor Methods
public BatchNoUseCard(string id)
{
//
// TODO: 在此处添加构造函数逻辑
//
this.s_id = id;
}
#endregion Constructor Methods
}
有一个TextBox和Button,用于用户设置每次添加记录的数量,Button的click事件:
protected void Btn_Set_Click(object sender, EventArgs e)
{
Set_GV();
}
private void Set_GV()
{
IList list = new ArrayList();
BatchNoUseCard card = new BatchNoUseCard("1"); card.Id = "编号1"; for (int i = 0; i < Get_GV_Rows(); i++)
{
BatchNoUseCard card1 = new BatchNoUseCard(Convert.ToString(i + 1));
card1.Id = "编号" + Convert.ToString(i + 1);
list.Add(card1);
} this.GV_Card.RowDataBound += new GridViewRowEventHandler(GV_Card_RowDataBound);
this.GV_Card.DataSource = list;
this.GV_Card.DataBind();
}void GV_Card_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
BatchNoUseCard card = (BatchNoUseCard)e.Row.DataItem;
TextBox txt_Box1 = (TextBox)e.Row.FindControl("Txt_CardNo");
txt_Box1.Text = card.CardSerial; TextBox txt_Box2 = (TextBox)e.Row.FindControl("Txt_Money");
txt_Box2.Text = card.Money.ToString();
///初始为100元
txt_Box2.Text = "100"; TextBox txt_Box3 = (TextBox)e.Row.FindControl("Txt_Term");
txt_Box3.Text = card.Month.ToString();
//初始为12个月
txt_Box3.Text = "12";
}
}设置完需要添加的记录数,填写好数据以后,提交进数据库的事件:
protected void Btn_Add_Click(object sender, EventArgs e)
{
Hashtable [] ht = new Hashtable[this.GV_Card.Rows.Count];
for (int i = 0; i < this.GV_Card.Rows.Count; i++)
{
ht[i] = new Hashtable();
ht[i].Add("CardSerial", InternetBar.BusinessLayerHelper.Char.Replace_Lawless_Char(((TextBox)this.GV_Card.Rows[i].Cells[0].FindControl("Txt_CardNo")).Text));
ht[i].Add("CardType", InternetBar.BusinessLayerHelper.Char.Replace_Lawless_Char(((TextBox)this.GV_Card.Rows[i].Cells[0].FindControl("Txt_Card_Type")).Text));
ht[i].Add("money", InternetBar.BusinessLayerHelper.Char.Replace_Lawless_Char(((TextBox)this.GV_Card.Rows[i].Cells[0].FindControl("Txt_Money")).Text));
ht[i].Add("UseMonths", InternetBar.BusinessLayerHelper.Char.Replace_Lawless_Char(((TextBox)this.GV_Card.Rows[i].Cells[0].FindControl("Txt_Term")).Text));
}
if (NoUsePaymentCardInfo.BatchNoUserCardInfo(ht))
{
ClientScript.RegisterStartupScript(GetType(), "", "<Script>alert('批量添加成功!')</script>"); }
else
{
ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('批量添加失败')</script>");
}
}if (NoUsePaymentCardInfo.BatchNoUserCardInfo(ht))
这里是我在NoUsePaymentCardInfo类里面写的一个BatchNoUserCardInfo批量提交的方法,还涉及到数据层的方法,比较复杂,这里就不 给出了...如果需要的话,也可以发给你...我的建议就是你赶开始学C#,你就先用最基本的吧..你循环gridview中的行,然后用13楼哥们说的循环构造出你要写就数据库的Sql,循环完了以后,一次执行这些SQL..执行不成功就让他回滚,这样就不回出现有的数据被添加进去了,有的又没有写进去...倘若你一条一条想数据库写,可能有写数据符合数据库的定义能写就去,有些有写不进去,那你也不 知道哪些被写进去了,哪些没有写进去..
public class BatchNoUseCard
{ #region Private Elements
private string s_id;
private string s_CardSerial;
private string s_CardType;
private int n_Money;
private int n_Month;
#endregion Private Elements #region Properties
public string Id
{
set { this.s_id = value; } get { return this.s_id; }
}
public string CardSerial
{
get { return this.s_CardSerial; }
set { this.s_CardSerial = value; }
} public string CardType
{
get { return this.s_CardType; }
set { this.s_CardType = value; }
} public int Month
{
get { return this.n_Money; }
set { this.n_Money = value; }
}
public int Money
{
get { return this.n_Month; }
set { this.n_Month = value; }
} #endregion Properties #region Constructor Methods
public BatchNoUseCard(string id)
{
//
// TODO: 在此处添加构造函数逻辑
//
this.s_id = id;
}
#endregion Constructor Methods
}
有一个TextBox和Button,用于用户设置每次添加记录的数量,Button的click事件:
protected void Btn_Set_Click(object sender, EventArgs e)
{
Set_GV();
}
private void Set_GV()
{
IList list = new ArrayList();
BatchNoUseCard card = new BatchNoUseCard("1"); card.Id = "编号1"; for (int i = 0; i < Get_GV_Rows(); i++)
{
BatchNoUseCard card1 = new BatchNoUseCard(Convert.ToString(i + 1));
card1.Id = "编号" + Convert.ToString(i + 1);
list.Add(card1);
} this.GV_Card.RowDataBound += new GridViewRowEventHandler(GV_Card_RowDataBound);
this.GV_Card.DataSource = list;
this.GV_Card.DataBind();
}void GV_Card_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
BatchNoUseCard card = (BatchNoUseCard)e.Row.DataItem;
TextBox txt_Box1 = (TextBox)e.Row.FindControl("Txt_CardNo");
txt_Box1.Text = card.CardSerial; TextBox txt_Box2 = (TextBox)e.Row.FindControl("Txt_Money");
txt_Box2.Text = card.Money.ToString();
///初始为100元
txt_Box2.Text = "100"; TextBox txt_Box3 = (TextBox)e.Row.FindControl("Txt_Term");
txt_Box3.Text = card.Month.ToString();
//初始为12个月
txt_Box3.Text = "12";
}
}设置完需要添加的记录数,填写好数据以后,提交进数据库的事件:
protected void Btn_Add_Click(object sender, EventArgs e)
{
Hashtable [] ht = new Hashtable[this.GV_Card.Rows.Count];
for (int i = 0; i < this.GV_Card.Rows.Count; i++)
{
ht[i] = new Hashtable();
ht[i].Add("CardSerial", InternetBar.BusinessLayerHelper.Char.Replace_Lawless_Char(((TextBox)this.GV_Card.Rows[i].Cells[0].FindControl("Txt_CardNo")).Text));
ht[i].Add("CardType", InternetBar.BusinessLayerHelper.Char.Replace_Lawless_Char(((TextBox)this.GV_Card.Rows[i].Cells[0].FindControl("Txt_Card_Type")).Text));
ht[i].Add("money", InternetBar.BusinessLayerHelper.Char.Replace_Lawless_Char(((TextBox)this.GV_Card.Rows[i].Cells[0].FindControl("Txt_Money")).Text));
ht[i].Add("UseMonths", InternetBar.BusinessLayerHelper.Char.Replace_Lawless_Char(((TextBox)this.GV_Card.Rows[i].Cells[0].FindControl("Txt_Term")).Text));
}
if (NoUsePaymentCardInfo.BatchNoUserCardInfo(ht))
{
ClientScript.RegisterStartupScript(GetType(), "", "<Script>alert('批量添加成功!')</script>"); }
else
{
ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('批量添加失败')</script>");
}
}if (NoUsePaymentCardInfo.BatchNoUserCardInfo(ht))
这里是我在NoUsePaymentCardInfo类里面写的一个BatchNoUserCardInfo批量提交的方法,还涉及到数据层的方法,比较复杂,这里就不 给出了...如果需要的话,也可以发给你...我的建议就是你赶开始学C#,你就先用最基本的吧..你循环gridview中的行,然后用13楼哥们说的循环构造出你要写就数据库的Sql,循环完了以后,一次执行这些SQL..执行不成功就让他回滚,这样就不回出现有的数据被添加进去了,有的又没有写进去...倘若你一条一条想数据库写,可能有写数据符合数据库的定义能写就去,有些有写不进去,那你也不 知道哪些被写进去了,哪些没有写进去..