繁体   English   中英

直接从数据库中删除网格中的选定记录

[英]Directly delete selected record in gird from database

我想直接从数据库中删除选定的行。 我写了这段代码,但是无法正常工作。

这是我的代码,但是它不会从SQL Server本地数据库中删除记录。

MyDataSet.Tables["ProductTable"].Rows[gridView1.GetFocusedDataSourceRowIndex()].Delete();

试试这个解决方案:

myDataSet = myWS.LookUp(sessionUserID, "UserID", "Users"); // Adapt this to the way you populate the dataset
            myDataTable = Tables["ProductTable"];
            DataRow drCurrent =MyDataSet.Tables["ProductTable"].Rows[gridView1.GetFocusedDataSourceRowIndex()]
            myDataSet.AcceptChanges() 
            drCurrent.Delete();
            myWS.UpdateDB(myDataSet);

双击DataGridView; 您会得到一个名为CellContentClick的事件。

try
{
    int x = int.Parse(datagridview.Rows[e.RowIndex].Cells[UserID.Index].Value.ToString());
    string query = "delete from productable where UserID=" + x;
    // now delete execute procedure
    MessageBox.Show("Deleted Successfully");
    // write code to bring record again.
}
catch(Exception ex)
{
    MessageBox.Show(ex.StackTrace);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM