1

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

enter image description here

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;

2 Answers 2

2

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";
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

What is the [n] in the grvRow.Cells[n].Text?
@laurencekeithalbano - n is the cell position of your Status column. But if you have defined Status as a control like label, better find it insread of using cell index.
If I want to change my gridview column using this sir, how do I do it?
@laurencekeithalbano - Hey I got you now, You are binding the grid directly so you will have to bind the event dynamically as well. Check my update, it should work.
Thanks for this one Sir @Rahl Singh, this is the one I've been looking for, now I need to update my query in my database when I click the button validate, thank you so much! :)
|
1

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();
    }
}

2 Comments

The code sender.validateRow() gets an error 'object' does not contain a definition for 'validateRow' and no extension method 'validateRow'
You have to add the method with the class to get it work. Post updated code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.