DataGridView新增数据,修改数据,删除数据

时间:2021-08-03 07:27:48

打开Vs ,创建一个DataGridViwe控件和contextMenuStrip右键快捷列表即可。

关于Insert新增一行数据 和 Update更新数据。

执行完添加数据库操作后,把公共变量i重新赋值,保存新增后的总行

        private void Newinsert()
{

string strda = "select * from FilTer";
string strin = "insert FilTer(id) values('" + dataGridView1.CurrentCell.Value.ToString() + "') ";

SqlConnection conn = connection();
conn.Open();

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strda, conn);
da.InsertCommand = new SqlCommand(strin, conn);
da.Fill(ds, "数据表");

DataRow dr = ds.Tables[0].NewRow();
dr[0] = strin;
ds.Tables[0].Rows.Add(dr);

da.Update(ds, ds.Tables[0].ToString());
conn.Close();
i = dataGridView1.Rows.Count;//保存添加后数据库已有的行数。重点


} //insert

上段代码只是新增一行数据的一部分,然而我们不仅要新增还需要更新或者修改部分数据。
下段代码以两种不同情况下刷新出来的行数,判断是新增或者修改。
一种情况:是新增后的行数。
另一个情况:是未新增的行数。

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{//单元格的值更改时发生

SqlConnection conn = connection();
try
{//怎么判断表格是否新建了一行?有一个属性是否添加新行

if (dataGridView1.Rows.Count > i || dataGridView1.Rows.Count == 0)//新增。重点
{
Newinsert();
}
else//更新。重点
{
conn.Open();
string strcolumn1 = dataGridView1.Columns[e.ColumnIndex].HeaderText;//得到列标题
string strvalue1 = dataGridView1.CurrentCell.Value.ToString();//得到数据
string strid = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();//所选行第一列的数据
string strinsert = "update FilTer set " + strcolumn1 + " = '" + strvalue1 + "' where id = '" + strid + "'";
//更新数据可以使用此代码。前提条件是必须符合更新条件
SqlCommand comm = new SqlCommand(strinsert, conn);
comm.ExecuteNonQuery();
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
conn.Close();
}
}//单元格的值更改时发生(update&&insert)

下段代码是初始化数据

        int i = 0;//保存第一次显示的行数,公共变量
private void GetDataGridView()
{
string strselect = "select * from FilTer";
SqlConnection conn = connection();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strselect, conn);
da.Fill(ds, "显示数据");
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.AutoGenerateColumns = true;//自动创建列
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;//单击单元格
i = dataGridView1.Rows.Count;//保存数据库已有的行数
}//datagridview初始化

关于Delete删除一行数据。

相对新增和更改,删除代码更简单了。删除主要关键代码:

 dataGridView1.Rows.RemoveAt(index);

则删除数据库主要关键代码:

        string strRow = dataGridView1.CurrentCell.Value.ToString();
string strdelete = "delete FilTer Where id = '" + strRow + "'";
conn.Open();
SqlCommand comm = new SqlCommand(strdelete, conn);
comm.ExecuteNonQuery();

下段是contextMenuStrip右键快捷列表代码的执行过程,也可以用button,下拉列表其他控件。根据现实需要去设置就好。

        int index = 0;//删除使用
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
dataGridView1.Rows[e.RowIndex].Selected = true;//选定一行
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[0];//每次只选定一行
contextMenuStrip1.Show(dataGridView1, e.Location);//右键列表显示在datagridview控件上
contextMenuStrip1.Show(Cursor.Position);//右键快捷列表显示在鼠标停留位置
index = e.RowIndex;
}
}//表格右键触发(delete)

private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
SqlConnection conn = connection();
try
{
string strRow = dataGridView1.CurrentCell.Value.ToString();
string strdelete = "delete FilTer Where id = '" + strRow + "'";
conn.Open();
SqlCommand comm = new SqlCommand(strdelete, conn);
comm.ExecuteNonQuery();

dataGridView1.Rows.RemoveAt(index);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
finally
{
conn.Close();
}
}//右键删除

下图是以上代码实现,关于筛选和背景色设置不包含在上面代码中。
DataGridView新增数据,修改数据,删除数据

数据库设置只有id是不为空,没检索,没唯一其他设置。只是为了研究直接在表格添加数据的功能实现。