C#中怎样将dataGridView中的新添加的一行数据添加到数据库中
已经连接到数据库,查询的表显示在dataGridView中,想要在最后一行插入一行数据,并点击“插入按钮”即可插入到数据库中,怎么弄呢?...
已经连接到数据库,查询的表显示在dataGridView中,想要在最后一行插入一行数据,并点击“插入按钮”即可插入到数据库中,怎么弄呢?
展开
展开全部
首先你要保证你的datagridview的AllowUserToAddRows属性为true,这样你的最慧渗后一行派亏始终是添加行,前羡脊添加完数据后 在你的插入按钮事件里获取最后一行的数据,
DataGridViewRow row = dataGridView1.Rows[dataGridView1.Rows.Count-2];
foreach (DataGridViewCell item in row.Cells)
{
item 就是每个单元格的值,你在这里可以拼接sql语句
}
之后执行就可以了
DataGridViewRow row = dataGridView1.Rows[dataGridView1.Rows.Count-2];
foreach (DataGridViewCell item in row.Cells)
{
item 就是每个单元格的值,你在这里可以拼接sql语句
}
之后执行就可以了
更多追问追答
追答
我上面已经都写出来了啊!
datagridview的AllowUserToAddRows属性为true不会么??
下面这些代码加到你的插入按钮事件里就行了啊!!
DataGridViewRow row = dataGridView1.Rows[dataGridView1.Rows.Count-2];
foreach (DataGridViewCell item in row.Cells)
{
item 就是每个单元格的值,你在这里可以拼接sql语句
}
这里的item没循环一个就是在循环你上面的列,
循环第一次item 的列名就是你是sno,
循环第二次item 的列名就是你是sname,
循环第三次item 的列名就是你是ssex,
循环第四次item 的列名就是你是sbarth,
………………
呢个弱弱的问一下,我上面写的你能否看懂,实在不行把你的项目发过来,我给你弄
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
private void tapein_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“deptDataSet.tdept”中。您可以根据需要移动或移除它。
this.tdeptTableAdapter.Fill(this.deptDataSet.tdept);
// TODO: 这行代码将数据加埋郑载到表“tapetypeDataSet.ttapetype”中。您可以根据需要移动或移除它。
this.ttapetypeTableAdapter.Fill(this.tapetypeDataSet.ttapetype);
// TODO: 这行代码将数据加载到表“progDataSet.tprogram”中。您可以根据需要移动或移除它。
this.tprogramTableAdapter.Fill(this.progDataSet.tprogram);
progcomboBox.Text = "";
tapetypecomboBox.Text = "";
deptcomboBox.Text = "";
//创建表中含态各个字段
initDataTable();
bindGrid();
}
//添加磁带详细节目弯老颂
private void addBtn_Click(object sender, EventArgs e)
{
if (progcomboBox.Text == "")
{
MessageBox.Show("请选择栏目", "友情提醒");
return;
}
if (tapecodetextBox.Text == "")
{
MessageBox.Show("请输入磁带编号", "友情提醒");
return;
}
using (SqlConnection cn = new SqlConnection(mysettings.Localsql))
{
cn.Open();
//创建一个DataRow对象
DataRow dr = dt.NewRow();
dr[0] = progcomboBox.Text;
dr[1] = tapecodetextBox.Text;
dr[2] = deptcomboBox.Text;
dr[3] = tapetypecomboBox.Text;
dr[4] = putdate.Text;
dr[5] = stacktextBox.Text;
dr[6] = shelftextBox.Text;
dr[7] = rowtextBox.Text;
dr[8] = columntextBox.Text;
dt.Rows.Add(dr);
bindingSource1.DataSource = dt;
}
}
//初始化datatable各字段
private void initDataTable()
{
DataColumn colprogram = dt.Columns.Add("program", System.Type.GetType("System.String"));
DataColumn coltapecode = dt.Columns.Add("tapecode", System.Type.GetType("System.String"));
DataColumn coldept = dt.Columns.Add("department", System.Type.GetType("System.String"));
DataColumn coltapetype = dt.Columns.Add("tapetype", System.Type.GetType("System.String"));
DataColumn colpubdate = dt.Columns.Add("pubdate", System.Type.GetType("System.String"));
DataColumn colstack = dt.Columns.Add("stack", System.Type.GetType("System.String"));
DataColumn colshelft = dt.Columns.Add("shelf", System.Type.GetType("System.String"));
DataColumn colrow = dt.Columns.Add("row", System.Type.GetType("System.String"));
DataColumn colcolumn = dt.Columns.Add("column", System.Type.GetType("System.String"));
}
//设计datagridview
private void bindGrid()
{
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
DataGridViewTextBoxColumn col_prog = new DataGridViewTextBoxColumn();
col_prog.DataPropertyName = "program";
col_prog.HeaderText = "栏目";
col_prog.Name = "program";
col_prog.ReadOnly = true;
col_prog.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_prog);
DataGridViewTextBoxColumn col_tapecode = new DataGridViewTextBoxColumn();
col_tapecode.DataPropertyName = "tapecode";
col_tapecode.HeaderText = "磁带编号";
col_tapecode.Name = "tapecode";
col_tapecode.ReadOnly = true;
col_tapecode.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_tapecode);
DataGridViewTextBoxColumn col_dept = new DataGridViewTextBoxColumn();
col_dept.DataPropertyName = "department";
col_dept.HeaderText = "制作部门";
col_dept.Name = "department";
col_dept.ReadOnly = true;
col_dept.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_dept);
DataGridViewTextBoxColumn col_tapetype = new DataGridViewTextBoxColumn();
col_tapetype.DataPropertyName = "tapetype";
col_tapetype.HeaderText = "磁带类型";
col_tapetype.Name = "tapetype";
col_tapetype.ReadOnly = true;
col_tapetype.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_tapetype);
DataGridViewTextBoxColumn col_pubdate = new DataGridViewTextBoxColumn();
col_pubdate.DataPropertyName = "pubdate";
col_pubdate.HeaderText = "播出日期";
col_pubdate.Name = "pubdate";
col_pubdate.ReadOnly = true;
col_pubdate.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_pubdate);
DataGridViewTextBoxColumn col_stack = new DataGridViewTextBoxColumn();
col_stack.DataPropertyName = "stack";
col_stack.HeaderText = "库号";
col_stack.Name = "stack";
col_stack.ReadOnly = true;
col_stack.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_stack);
DataGridViewTextBoxColumn col_shelf = new DataGridViewTextBoxColumn();
col_shelf.DataPropertyName = "shelf";
col_shelf.HeaderText = "架号";
col_shelf.Name = "shelf";
col_shelf.ReadOnly = true;
col_shelf.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_shelf);
DataGridViewTextBoxColumn col_row = new DataGridViewTextBoxColumn();
col_row.DataPropertyName = "row";
col_row.HeaderText = "行号";
col_row.Name = "row";
col_row.ReadOnly = true;
col_row.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_row);
DataGridViewTextBoxColumn col_column = new DataGridViewTextBoxColumn();
col_column.DataPropertyName = "column";
col_column.HeaderText = "列号";
col_column.Name = "column";
col_column.ReadOnly = true;
col_column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView1.Columns.Add(col_column);
}
其实这是一个非常简单的问题,首先在展示时对这个datagridview进行数据绑定,即datagridview1.datasource =dt; 因为这个是双向绑定的,所以我们只要对数据源的增删改(即对dt进行操作),会反映到datagridview中,
就是添加按钮时:DataRow dr=dt.NewRow();
dr["字段"]=值
...........
...........
dt.Rows.Add(dr);
然后更新 SqlDataAdapter1.Update(dt)更新到数据库即可,然后dt.acceptchanges()
{
// TODO: 这行代码将数据加载到表“deptDataSet.tdept”中。您可以根据需要移动或移除它。
this.tdeptTableAdapter.Fill(this.deptDataSet.tdept);
// TODO: 这行代码将数据加埋郑载到表“tapetypeDataSet.ttapetype”中。您可以根据需要移动或移除它。
this.ttapetypeTableAdapter.Fill(this.tapetypeDataSet.ttapetype);
// TODO: 这行代码将数据加载到表“progDataSet.tprogram”中。您可以根据需要移动或移除它。
this.tprogramTableAdapter.Fill(this.progDataSet.tprogram);
progcomboBox.Text = "";
tapetypecomboBox.Text = "";
deptcomboBox.Text = "";
//创建表中含态各个字段
initDataTable();
bindGrid();
}
//添加磁带详细节目弯老颂
private void addBtn_Click(object sender, EventArgs e)
{
if (progcomboBox.Text == "")
{
MessageBox.Show("请选择栏目", "友情提醒");
return;
}
if (tapecodetextBox.Text == "")
{
MessageBox.Show("请输入磁带编号", "友情提醒");
return;
}
using (SqlConnection cn = new SqlConnection(mysettings.Localsql))
{
cn.Open();
//创建一个DataRow对象
DataRow dr = dt.NewRow();
dr[0] = progcomboBox.Text;
dr[1] = tapecodetextBox.Text;
dr[2] = deptcomboBox.Text;
dr[3] = tapetypecomboBox.Text;
dr[4] = putdate.Text;
dr[5] = stacktextBox.Text;
dr[6] = shelftextBox.Text;
dr[7] = rowtextBox.Text;
dr[8] = columntextBox.Text;
dt.Rows.Add(dr);
bindingSource1.DataSource = dt;
}
}
//初始化datatable各字段
private void initDataTable()
{
DataColumn colprogram = dt.Columns.Add("program", System.Type.GetType("System.String"));
DataColumn coltapecode = dt.Columns.Add("tapecode", System.Type.GetType("System.String"));
DataColumn coldept = dt.Columns.Add("department", System.Type.GetType("System.String"));
DataColumn coltapetype = dt.Columns.Add("tapetype", System.Type.GetType("System.String"));
DataColumn colpubdate = dt.Columns.Add("pubdate", System.Type.GetType("System.String"));
DataColumn colstack = dt.Columns.Add("stack", System.Type.GetType("System.String"));
DataColumn colshelft = dt.Columns.Add("shelf", System.Type.GetType("System.String"));
DataColumn colrow = dt.Columns.Add("row", System.Type.GetType("System.String"));
DataColumn colcolumn = dt.Columns.Add("column", System.Type.GetType("System.String"));
}
//设计datagridview
private void bindGrid()
{
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
DataGridViewTextBoxColumn col_prog = new DataGridViewTextBoxColumn();
col_prog.DataPropertyName = "program";
col_prog.HeaderText = "栏目";
col_prog.Name = "program";
col_prog.ReadOnly = true;
col_prog.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_prog);
DataGridViewTextBoxColumn col_tapecode = new DataGridViewTextBoxColumn();
col_tapecode.DataPropertyName = "tapecode";
col_tapecode.HeaderText = "磁带编号";
col_tapecode.Name = "tapecode";
col_tapecode.ReadOnly = true;
col_tapecode.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_tapecode);
DataGridViewTextBoxColumn col_dept = new DataGridViewTextBoxColumn();
col_dept.DataPropertyName = "department";
col_dept.HeaderText = "制作部门";
col_dept.Name = "department";
col_dept.ReadOnly = true;
col_dept.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_dept);
DataGridViewTextBoxColumn col_tapetype = new DataGridViewTextBoxColumn();
col_tapetype.DataPropertyName = "tapetype";
col_tapetype.HeaderText = "磁带类型";
col_tapetype.Name = "tapetype";
col_tapetype.ReadOnly = true;
col_tapetype.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_tapetype);
DataGridViewTextBoxColumn col_pubdate = new DataGridViewTextBoxColumn();
col_pubdate.DataPropertyName = "pubdate";
col_pubdate.HeaderText = "播出日期";
col_pubdate.Name = "pubdate";
col_pubdate.ReadOnly = true;
col_pubdate.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_pubdate);
DataGridViewTextBoxColumn col_stack = new DataGridViewTextBoxColumn();
col_stack.DataPropertyName = "stack";
col_stack.HeaderText = "库号";
col_stack.Name = "stack";
col_stack.ReadOnly = true;
col_stack.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_stack);
DataGridViewTextBoxColumn col_shelf = new DataGridViewTextBoxColumn();
col_shelf.DataPropertyName = "shelf";
col_shelf.HeaderText = "架号";
col_shelf.Name = "shelf";
col_shelf.ReadOnly = true;
col_shelf.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_shelf);
DataGridViewTextBoxColumn col_row = new DataGridViewTextBoxColumn();
col_row.DataPropertyName = "row";
col_row.HeaderText = "行号";
col_row.Name = "row";
col_row.ReadOnly = true;
col_row.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(col_row);
DataGridViewTextBoxColumn col_column = new DataGridViewTextBoxColumn();
col_column.DataPropertyName = "column";
col_column.HeaderText = "列号";
col_column.Name = "column";
col_column.ReadOnly = true;
col_column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView1.Columns.Add(col_column);
}
其实这是一个非常简单的问题,首先在展示时对这个datagridview进行数据绑定,即datagridview1.datasource =dt; 因为这个是双向绑定的,所以我们只要对数据源的增删改(即对dt进行操作),会反映到datagridview中,
就是添加按钮时:DataRow dr=dt.NewRow();
dr["字段"]=值
...........
...........
dt.Rows.Add(dr);
然后更新 SqlDataAdapter1.Update(dt)更新到数据库即可,然后dt.acceptchanges()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询