本文介绍了带有删除链接按钮的C#网格视图,当检索要删除的值时,值返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除所选行,其中有一个隐藏列activityID和taskID,我将其设置为false,因为我需要将它们的值从数据库中删除。

我必须从中删除数据库使用QuestionNo,activityID和taskID。



隐藏了ActivityID和TaskID,GridView实际上是这样的:



QuestionNo,ActivtiyID,TaskID, QuestionContent,deletebutton



我的aspx代码,我已经从boundfield将列转换为templatefield,并且在尝试检索列的值时我不断获取空值(例如:QuestionNo,ActivityID和TaskID。究竟是什么方法才能正确执行此操作?我正在使用Entitiy Framework来执行此操作。



我的代码如下所示:



I need to delete the selected row where there is a hidden column activityID and taskID which I set visible to false because I need their value to delete it from the database.
I have to delete from database using QuestionNo , activityID and taskID .

ActivityID and TaskID is hidden , GridView actually looks like this :

QuestionNo,ActivtiyID,TaskID,QuestionContent , deletebutton

My aspx codes , i have converted the columns to templatefield from boundfield and i keep getting null values when trying to retrieve the values of the columns ( for example: QuestionNo , ActivityID and TaskID .What exactly is the correct way to do this? i am using Entitiy Framework to do this.

My Code looks like this :

protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               BindQuestions();


           }
       }

       protected void BindQuestions()
       {
            gvQuestion.DataSource = daoQuestion.GetList();
            gvQuestion.DataBind();
       }

       protected void LinkButton1_Click(object sender, EventArgs e)
       {


       }

       protected void gvQuestion_RowDeleting1(object sender, GridViewDeleteEventArgs e)
       {
           gvQuestion.DeleteRow(e.RowIndex);

            Model.question del = new Model.question();

           int q = Convert.ToInt32(gvQuestion.Rows[e.RowIndex].Cells[0].ToString());
           int a = Convert.ToInt32(gvQuestion.Rows[e.RowIndex].Cells[1].ToString()); // Hidden Column
           int t = Convert.ToInt32(gvQuestion.Rows[e.RowIndex].Cells[2].ToString()); // Hidden Column

           del.QuestionNo = q;
           del.ActivityID = a;// Value of ActivityID column in GV
           del.TaskID = t; // Value of TaskID column in GV


           daoQuestion.Delete(del);
           daoQuestion.Save();
       }`

推荐答案

daoQuestion.Attach(del);///Firstly, Attach the entity object to context
DeleteObject(del); ///specifies the object to delete
SaveChanges();


<asp:LinkButton ID="l1" runat="server" Text="Remove" %>'  CommandName="Remove"  CommandArgument='<%#Eval("id") %>'></asp:LinkButton>





和关于rowcommand事件



and on rowcommand event

void GVDetail_RowCommand_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Remove")
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            int index = row.RowIndex;
            string id=row.Cells[0].Text;//this will retrive column 1 value     
//var id = Int32.Parse(e.CommandArgument);
     GVDetail.DeleteRow(int.Parse(id));
GVDetail.DataBind();


 }
}


这篇关于带有删除链接按钮的C#网格视图,当检索要删除的值时,值返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 04:14