在datagridview和数据库中删除和编辑行 [英] Delete and Edit row in datagridview and in database
问题描述
我有datagridview并有两个按钮DELETE和EDIT ...我想编码,就像我从datagridview中选择一行并单击Delete按钮一样,所选行也将在数据库中被删除和更新,对于编辑行也是如此. />
请为该按钮单击事件建议代码.
谢谢.
Hi,,
I have datagridview and have two buttons DELETE AND EDIT... i want to code like if i select one row from datagridview and click on delete button that selected row gets deleted and updated in database also, same for the edit row also.
Please suggest code for this button click event.
Thank you.
推荐答案
嗨Teena
希望对您有所帮助:
Hi Teena
I hope this can help you:
private void datagridview _CellClick(object sender, DataGridViewCellEventArgs e)
{
// Ignore others clicks
if (e.RowIndex < 0 || e.ColumnIndex = datagridview.Column"Col_DeButton"].Index)
return;
else
{
// Delete from database
int iRtnVal = 0;
SqlConnection sqlConn = new SqlConnection(Conexion);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = "DELETE FROM ...";
sqlCmd.Connection = sqlConn;
try
{
sqlCmd.Connection.Open();
iRtnVal = sqlCmd.ExecuteNonQuery();
}
catch (Exception Ex)
{
Thread.CurrentThread.Abort();
}
finally
{
sqlCmd.Connection.Close();
}
// Reload the datagridview
SqlDataAdapter sqlAdapt = new SqlDataAdapter("SELECT * FROM ...", sqlConn);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
try
{
sqlConn.Open();
sqlAdapt.Fill(ds, "Datas");
}
catch (Exception Ex)
{
Thread.CurrentThread.Abort();
}
finally
{
sqlConn.Close();
}
if (ds.Tables["Datos"] != null)
dt = ds.Tables["Data"];
else
dt = null;
datagridview.datasource = dt;
}
}
嗨Teena,
你去了:
通过分页在DataGridView中添加,编辑和删除
http://msdn.microsoft.com/en-us/library/system. windows.forms.datagridvieweditmode.aspx [ ^ ]
http://nareshkamuni.blogspot.in/2012/04/how-to-insert-edit-update-and-delete.html [此处 [ ^ ].
一切顺利.
Hi Teena,
here you go:
Add, Edit, and Delete in DataGridView with Paging
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridvieweditmode.aspx[^]
http://nareshkamuni.blogspot.in/2012/04/how-to-insert-edit-update-and-delete.html[^]
I replied to your comment also. See here[^].
All the best.
hello teena,
转到网格视图属性->事件->选择rowdelete/rowedit事件
然后转到C#代码
并在该函数中遵循滚滚示例.
hello teena,
go to grid-view properties->events-> select rowdelete /rowedit event
then go to c# code
and in that function follow the billow example.
void CustomersGridView_RowDeleting
(Object sender, GridViewDeleteEventArgs e)
{
TableCell cell = CustomersGridView.Rows[e.RowIndex].Cells[2];
if (cell.Text == "Beaver")
{
e.Cancel = true;
Message.Text = "You cannot delete customer Beaver.";
}
else
{
Message.Text = "";
}
}
有关更多参考,请参考此链接
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx [
for further reference refer this link
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx[^]
这篇关于在datagridview和数据库中删除和编辑行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!