如何从asp.net gridview中删除选定的行 [英] How to delete selected row from asp.net gridview

查看:285
本文介绍了如何从asp.net gridview中删除选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个员工录音计划,

所以每次员工进来时,都会被记录下来并存储在数据库中



这里是场景。



PERSON A上午7点进来--->数据将存储在数据库中并显示在数据网格视图中



PERSONA A出现在下午2:00 ---->时间将插入gridview的TIME OUT列。 (数据在GRIDVIEW上仍然可见)



PERSON A在下午2:30进来---->数据将存储在数据库中并显示在数据网格视图中



..... n次。





我从记录中删除以下代码:



I have an employee recording program,
so every time an employee comes in, it will be recorded and will be stored in the database

here is the scenario.

PERSON A comes in at 7:00 AM ---> Data will be stored at the database and displayed at the data gridview

PERSONA A comes out at 2:00 PM ----> A time will be inserted in the TIME OUT column of the gridview. (DATA IS STILL VISIBLE AT THE GRIDVIEW)

PERSON A comes in at 2:30 PM ----> Data will be stored at the database and displayed at the data grid view

..... n times.


I have the following code to delete from the records:

protected void EMPLOYEERECORD_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {

           Label lblEmpName = (Label)EMPLOYEERECORD.Rows[e.RowIndex].FindControl("lblEmpName");
           DatabaseModule.SQcon.Open();
           string cmdstr = "delete from dbo.RecordEmployee where EMPLOYEE_NAME=@empname";
           SqlCommand cmd = new SqlCommand(cmdstr, DatabaseModule.SQcon);
           cmd.Parameters.AddWithValue("@empname", lblEmpName.Text);
           cmd.ExecuteNonQuery();
           DatabaseModule.SQcon.Close();
           BindData();

       }







但是它也会删除其他记录,因为我放置了




however it also deletes the other records, because I placed

EMPLOYEE_NAME=@empname"





但当我尝试更新我的WHERE CLAUSE TO:



but when I tried updating my WHERE CLAUSE TO:

EMPLOYEE_NAME=@empname" and TIME_OUT is NULL





它不是工作要么。任何人都可以给我一个想法如何实现这一点..



it is not working either. can anyone give me an idea how to accomplish this..

推荐答案

First get row details 
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];
Then Delete use your delete query as per row.Cells[0].Text 





获取更多详细信息此链接


将RecordID之类的列作为数据库中的唯一/主键,并对该ID执行删除检查,因为这将允许您处理单个员工的多个记录。您也可以编辑更新并删除打孔。
Take a column like RecordID as unique/primary key in the database and perform the delete check on that id bases as this will allow you to handle multiple record of a single employee. Also you can edit update and delete the punches.


在这里使用empname删除记录,使用empid,将Empid设置为gridview的数据键。或者将emp Id设置为commmand参数删除gridview中的按钮。动态绑定gridview,即使用模板字段和项模板。
Here you delete the record using empname,in place use empid ,set Empid as datakey of gridview .Or set emp Id as the commmand argument of delete button in the gridview. Bind gridview Dynamically i.e,using Template field and Item Template.


这篇关于如何从asp.net gridview中删除选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆