C#中怎样将dataGridView中的新添加的一行数据添加到数据库中

2024-11-15 16:57:03
推荐回答(4个)
回答1:

1、新建一个Form做测试,包含一个DataGridView,DataGridView有三列,分别是学号,姓名,分数表示一个期末考试分数表。

2、双击添加数据按钮,填写添加数据代码。

3、点击运行,填写数据。

4、点击添加即可添加一行数据。

注意事项:

C语言是仅产生少量的机器语言以及不需要任何运行环境支持便能运行的高效率程序设计语言。尽管C语言提供了许多低级处理的功能,但仍然保持着跨平台的特性。

回答2:

首先你要保证你的datagridview的AllowUserToAddRows属性为true,这样你的最后一行始终是添加行,添加完数据后 在你的插入按钮事件里获取最后一行的数据,

DataGridViewRow row = dataGridView1.Rows[dataGridView1.Rows.Count-2];
foreach (DataGridViewCell item in row.Cells)
{
item 就是每个单元格的值,你在这里可以拼接sql语句
}
之后执行就可以了

回答3:

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()

回答4:

f爽爽爽爽爽爽爽爽爽