且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从datagridview和datasource表中删除选定的行

更新时间:2023-02-06 23:30:54

在将数据从数据库填充到datagridview的同时,您也可以包含 ID 字段,但是在显示数据时,您可以把它藏起来.然后,当您要删除数据时,您将具有 ID 以便执行此操作.

While you are populating the data from the DB to datagridview, you can include the ID field too, but while showing data you can hide it. Then when you want to delete data you will have the ID in order to do that.

例如,假设我们具有以下内容:

For example, suppose that we have as below:

sqlcommand command = new sqlcommand("SELECT IDHuman,HumanName,... FROM tblHuman",connection)

现在,如您所见,我们也有ID,我们应该按以下方式隐藏它:

Now as you see, we have the ID as well and we should hide it as follows:

datagridview1.cells[0].visible=false;

然后,当您要删除它时,可以在 WHERE 子句中包含 ID .

Then when you want to delete it, you can include ID in your WHERE clause.