怎么删除dataGrid的数据 有谁知道怎么删除dataGrid的数据吗?语言是C#,是winform,不是在webform中呀,本人等啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我是想在界面中直接删除,就是说直接选中datagrid中的这个行,然后执行删除按钮操作啊, 从数据源中删除也要通过代码标识该datagrid列中的或者行啊,需要建变量啊,问题是找不到datagrid中什么样方法能将值赋给cmd.parameters[].value啊, private void Delbutton_Click(object sender, System.EventArgs e) { int i = dataGrid.CurrentRowIndex; if (i==-1){MessageBox.Show("请先在表中选择需要删除的数据行!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);} else { try { sqlConnection1.Open(); SqlCommandBuilder sqlCommandBuilder1 = new SqlCommandBuilder ( sqlDataAdapter1); ds.Tables["Cable_info"].Rows[i].Delete(); sqlDataAdapter1.Update(ds,"Cable_info"); ds.Tables["Cable_info"].AcceptChanges(); sqlConnection1.Close(); MessageBox.Show("已经成功删除数据","删除成功",MessageBoxButtons.OK,MessageBoxIcon.Information); } catch (System.Exception e1) {MessageBox.Show("" ,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);} } }这段代码删除dataGrid中当前选中行,并且更新所关联的数据库(不用更新数据库的可以去掉),ds是绑定在dataGrid上的DataSet int rowi;private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){System.Drawing.Point pt= new Point(e.X,e.Y); DataGrid.HitTestInfo hti= this.dataGrid1.HitTest(pt); if(hti.Type==DataGrid.HitTestType.Cell) { //this.dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column);rowi=hti.Row;this.dataGrid1.Select(hti.Row);} }private void button1_Click(object sender, System.EventArgs e){ds.Tables[0].Rows[rowi].Delete();//已经得到了行标,你可以在这里执行sql删除数据库里的数据了this.dataGrid1.DataSource=ds.Tables[0].DefaultView; } SqlConnection con = new Sqlcon("server = .;database = (你的数据库);user = (你的用户名);password = (你的密码)";con.Open();SqlCommand cmd = new SqlCommand("delete (你的表) where (表的id)= @id",con);pt = new SqlParameter("@id",SqlDbType.VarChar,6);pt.Value = e.Item.Cells[0].Text;cmd.Parameters.Add(pt);cmd.ExecuteNonQuery();DataGrid1.EditItemIndex = -1;DataGrid1.DataBind(); //DataGrid1绑定 在womform中根本主不支持e.Item.Cells[0].Text这种方法啊,是不是我没有using什么呢?请指教!!楼上的这位同志,那里不是表的id吧,是表里某个字段的id吧,呵呵,我就是找不到这种方法呀, 用C#软件做POST时,发现抓包数据存在换行,该怎么办? 怎么做啊 c# 怎么剪切按钮的时候,同时把代码也复制过去? 求助高手 Click事件先判断再触发Html的function 怎么写? 面试时的考题 问两个进度条方面的问题! 原谅最近穷的没分:有关隐藏桢和Ajax实现无刷新问题? postmessage()问题 C# 数据采集 编码问题 一个简单的访问数据库的例子居然错了
{
int i = dataGrid.CurrentRowIndex;
if (i==-1){MessageBox.Show("请先在表中选择需要删除的数据行!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);}
else
{
try
{
sqlConnection1.Open();
SqlCommandBuilder sqlCommandBuilder1 = new SqlCommandBuilder ( sqlDataAdapter1);
ds.Tables["Cable_info"].Rows[i].Delete();
sqlDataAdapter1.Update(ds,"Cable_info");
ds.Tables["Cable_info"].AcceptChanges();
sqlConnection1.Close();
MessageBox.Show("已经成功删除数据","删除成功",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch (System.Exception e1) {MessageBox.Show("" ,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);}
} }
这段代码删除dataGrid中当前选中行,并且更新所关联的数据库(不用更新数据库的可以去掉),ds是绑定在dataGrid上的DataSet
private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
System.Drawing.Point pt= new Point(e.X,e.Y);
DataGrid.HitTestInfo hti= this.dataGrid1.HitTest(pt);
if(hti.Type==DataGrid.HitTestType.Cell)
{
//this.dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column);
rowi=hti.Row;
this.dataGrid1.Select(hti.Row);
}
}private void button1_Click(object sender, System.EventArgs e)
{
ds.Tables[0].Rows[rowi].Delete();
//已经得到了行标,你可以在这里执行sql删除数据库里的数据了
this.dataGrid1.DataSource=ds.Tables[0].DefaultView;
}
con.Open();
SqlCommand cmd = new SqlCommand("delete (你的表) where (表的id)= @id",con);
pt = new SqlParameter("@id",SqlDbType.VarChar,6);
pt.Value = e.Item.Cells[0].Text;
cmd.Parameters.Add(pt);cmd.ExecuteNonQuery();DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind(); //DataGrid1绑定
楼上的这位同志,那里不是表的id吧,是表里某个字段的id吧,呵呵,
我就是找不到这种方法呀,