如何删除DataGridView中选中的一行(vs 2008 c# winform)

new_sky_hony 2009-12-07 08:02:41
rt(注:在删除gridview的那行同时也删除数据库中对应的项)
...全文
2631 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
henry-qu 2011-10-09
  • 打赏
  • 举报
回复
http://www.codeproject.com/KB/tree/dropdowntree.aspx
guobihan 2010-08-05
  • 打赏
  • 举报
回复
if (MessageBox.Show("确定删除选中的数据吗?", "操作警示", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign) == DialogResult.Yes)
{
int i=0;
foreach (System.Windows.Forms.DataGridViewRow myrow in dvgShangPinXinXi.Rows)
{
i++;
if ((bool)myrow.Cells["xuanze"].FormattedValue)
{
BLL.ShangPinWeiHu.Bll_Delete_ShangPinXinXi(myrow.Cells["shangpindaima"].Value.ToString());
i--;
}
}
if (i == dvgShangPinXinXi.Rows.Count)
{
MessageBox.Show("没有可以删除的商品", "系统提示");
}
路遥迢 2009-12-08
  • 打赏
  • 举报
回复
this.Validate();
this.xGTPBindingSource.EndEdit();

foreach (DataGridViewRow r in xGTPDataGridViewX.SelectedRows)//选中的行
{
this.xGTPDataGridViewX.Rows.Remove(r);
}

this.xGTPTableAdapter.Update(this.wZ_DBDataSet.XGTP);
new_sky_hony 2009-12-08
  • 打赏
  • 举报
回复
已经找到一个笨的方法,但是,目的达到了:先获取要删除行在数据库中的主键信息,将其保存,然后对数据库执行删除操作(将获得的主键信息作为删除的条件),然后对datagridview重新填充!
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
string cnnstr = "server=.;database=txl;user id=sa;password=sa;";
SqlConnection cnn = new SqlConnection(cnnstr);
cnn.Open();
int n = dataGridView1.SelectedRows.Count;//选中的行数
if (n > 0)
{
for (int i = 0; i < n; i++)//逐行删除数据库中的对应项
{
string str = dataGridView1.SelectedRows[i].Cells[1].Value.ToString();//选中行的主键信息,cells【1】是我数据库表中的主键
string deletestr = "delete from UsersInfo where 姓名='" + str + "'";
SqlCommand delete = new SqlCommand(deletestr, cnn);
delete.ExecuteNonQuery();//删除数据库中的项
}
string selectstr = "select * from UsersInfo ";
SqlCommand select = new SqlCommand(selectstr, cnn);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = select;
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];//重新填充datagridaview
adapter.Dispose();
cnn.Close();
MessageBox.Show("恭喜,删除成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("请选中要删除的好友!", "出错提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
new_sky_hony 2009-12-08
  • 打赏
  • 举报
回复
已经找到一个笨的方法,但是,目的达到了:先获取要删除行在数据库中的主键信息,将其保存,然后对数据库执行删除操作(将获得的主键信息作为删除的条件),然后对datagridview重新填充!
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
string cnnstr = "server=.;database=txl;user id=sa;password=sa;";
SqlConnection cnn = new SqlConnection(cnnstr);
cnn.Open();
int n = dataGridView1.SelectedRows.Count;//选中的行数
if (n > 0)
{
for (int i = 0; i < n; i++)//逐行删除数据库中的对应项
{
string str = dataGridView1.SelectedRows[i].Cells[1].Value.ToString();//选中行的主键信息,cells【1】是我数据库表中的主键
string deletestr = "delete from UsersInfo where 姓名='" + str + "'";
SqlCommand delete = new SqlCommand(deletestr, cnn);
delete.ExecuteNonQuery();//删除数据库中的项
}
string selectstr = "select * from UsersInfo ";
SqlCommand select = new SqlCommand(selectstr, cnn);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = select;
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];//重新填充datagridaview
adapter.Dispose();
cnn.Close();
MessageBox.Show("恭喜,删除成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("请选中要删除的好友!", "出错提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
孤芳-自赏 2009-12-08
  • 打赏
  • 举报
回复
 int rowindex = this.dataGridViewExampaper.CurrentCell.RowIndex; //选中所选行
string code = this.dataGridViewExampaper[1, rowindex].Value.ToString();//获取选中行的第二个单元格值

然后根据把Code作为条件,执行你的删除SQL语句就行了
new_sky_hony 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hudingwen 的回复:]
                    this.Validate();
                    this.xGTPBindingSource.EndEdit();

                    foreach (DataGridViewRow r in xGTPDataGridViewX.SelectedRows)//选中的行
                    {
                        this.xGTPDataGridViewX.Rows.Remove(r);
                    }

                    this.xGTPTableAdapter.Update(this.wZ_DBDataSet.XGTP);
[/Quote]
这个操作仅仅删除了datagridview中的数据,并没有删除数据库中的对应项,刷新后“删除项”依然存在!
new_sky_hony 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hudingwen 的回复:]
                    this.Validate();
                    this.xGTPBindingSource.EndEdit();

                    foreach (DataGridViewRow r in xGTPDataGridViewX.SelectedRows)//选中的行
                    {
                        this.xGTPDataGridViewX.Rows.Remove(r);
                    }

                    this.xGTPTableAdapter.Update(this.wZ_DBDataSet.XGTP);
[/Quote]
这个仅仅是删除了datagridview中的数据,并没有删除数据库中的对应项,刷新后“删除项”依然存在!
new_sky_hony 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hudingwen 的回复:]
                    this.Validate();
                    this.xGTPBindingSource.EndEdit();

                    foreach (DataGridViewRow r in xGTPDataGridViewX.SelectedRows)//选中的行
                    {
                        this.xGTPDataGridViewX.Rows.Remove(r);
                    }

                    this.xGTPTableAdapter.Update(this.wZ_DBDataSet.XGTP);
[/Quote]
这个仅仅是删除了datagridview中的数据,并没有删除数据库中的对应项,刷新后“删除项”依然存在!
cc_net 2009-12-07
  • 打赏
  • 举报
回复
SqlDataAdapter.Update的话还要用CommandBuilder
红街咖啡 2009-12-07
  • 打赏
  • 举报
回复
dataset.Customers.Rows[this.dataGridView1.CurrentRow.Index].Delete();
SqlDataAdapter.Update(dataset.Customers);

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧