Change text in specific row in gridview using checkbox

青春壹個敷衍的年華 提交于 2020-01-07 09:02:16

问题


I want to change a specific text in my gridview here's an image below:

Example if I click the checkbox button the specific row "Status" text is change to "Validated".

This is my aspx code behind:

   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using MSSQLConnector;

    namespace SoftwareAnalysisAndDesign.SAD
    {
        public partial class TeacherPage : System.Web.UI.Page
        {
            private MSConnector connector = new MSConnector();
            private DataSet SubjectlistData;
            private DataTable SubjectlistTable;
            string query = null;
            protected void Page_Load(object sender, EventArgs e)
            {

                 if (!IsPostBack)
                 {
                     //Populate The Select Tag with Subjects
                     SubjectList.DataSource = Session["TeacherSubjectList"];
                     SubjectList.DataTextField = "CourseNo";
                     SubjectList.DataValueField = "CourseNo";
                     SubjectList.DataBind();
                 }
        }
    }     
   protected void TeacherSubjects_Click(object sender, EventArgs e)
    {
        string getText = SubjectList.SelectedItem.Text;
        //Connection String
        connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";

        query = "select StudentID,CourseNo,CourseDescription,Units,Day,StartTime,EndTime,Room,Instructor,Amount,Status from assessmentform where CourseNo = '" + getText + "'";

        SubjectlistData = connector.ExecuteQuery(query);
        SubjectlistTable = SubjectlistData.Tables[0];

        //Add a colum check row
        SubjectlistTable.Columns.Add("Check", Type.GetType("System.Boolean"));

        Session["ValidateSubject"] = SubjectlistTable;

        Response.Redirect("ValidateSubjectTeacher.aspx");
    }

I add a checkbox in my row to my gridview using this:

        //Add a colum check row
        SubjectlistTable.Columns.Add("Check", Type.GetType("System.Boolean"));

        Session["ValidateSubject"] = SubjectlistTable;

I pass my gridview using session to another page, its aspx code behind ValidatePage:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class ValidateSubjectTeacher : System.Web.UI.Page
    {
        CheckBox check = new CheckBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ValidateSubject"] == null)
            {
                Response.Redirect("TeacherPage.aspx", true);
            }

            if (!IsPostBack)
            {
                ValidateSubject.DataSource = Session["ValidateSubject"];
                ValidateSubject.DataBind();
            }

            //I add my checkbox in the last row using this
            foreach (GridViewRow row in ValidateSubject.Rows)
            {
                check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox;
                check.Enabled = true;
            }
        }


        protected void ValidateSubject_Click(object sender, EventArgs e)
        {
            if(check.Checked)
            {
                //This condition here is I want to change the Status when check to validated.
            }
        }
    }

in my code behind using session I bind all the data in DataTable(FirstPage);

DataSet ds = connector.ExecuteQuery(query);
DataTable dt = dt.Table[0];
dt.DataBind();
Session["SubjectList"] = dt;

回答1:


You can use the NamingContainer property like this:-

protected void ValidateSubject_Click(object sender, EventArgs e)
{
    CheckBox chk= (CheckBox)sender;
    GridViewRow grvRow = (GridViewRow)chk.NamingContainer;//This will give you row
    grvRow.Cells[10].Text = "Validated"; //Updated the cell text in that row.

    //Or if Status is a control like label then //
    Label StatusLabel = (Label)grvRow.FindControl("StatusLabel");
    StatusLabel.Text = "Validated";
}

Alternatively, you can also use the RowDataBound event.

Update:

Since you are binding the grid with directly i.e. AutoGenerateColumns set as true, you will have to attach the event handler of checkbox programmatically as well like this:-

//I add my checkbox in the last row using this
foreach (GridViewRow row in ValidateSubject.Rows)
{
    check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox;
    check.Enabled = true;
    check.CheckedChanged += ValidateSubject_Click;  //Bind the event
    check.AutoPostBack = true;  //Set the AutoPostBack property to true
}

Now, in your event first find the row with the help of checkbox, then update the Status column like this:-

protected void ValidateSubject_Click(object sender, EventArgs e)
{
    CheckBox chk= (CheckBox)sender;  
    GridViewRow grvRow = (GridViewRow)chk.NamingContainer;//This will give you row 
    if(chk.Checked)
    {
        grvRow.Cells[10].Text = "Validated";
    }
}



回答2:


You can use an Extension Method like this:

public static class myExtensionsMethods
{
    public static void validateRow(this object sender)
    {
        int columnIndex = 10;
        GridViewRow myRow = ((Control)sender).Parent as GridViewRow;
        myRow.Cells[columnIndex].Text = "Validated";
    }
}

The extension method will allow you to call it in this way

protected void ValidateSubject_Click(object sender, EventArgs e)
{
    if (check.Checked)
    {
        sender.validateRow();
    }
}


来源:https://stackoverflow.com/questions/32828799/change-text-in-specific-row-in-gridview-using-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!