I am developing an application in that grid view with checkbox and textbox controls are there. after selecting checkbox the corresponding row will activated and after click on update button it will updates the recordes in database if click on delete button the records in that row get deleted.
Now i have a change color button, after selecting ckeckbox and click on change color button the color of the selected row should change. iam not getting how to do this. update and delete process is happening but color changing not happening. please help me how to change the color of the gridview row after clicking a button.
my .aspx code is
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
my .aspx.cs code is
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Text;
using System.Web.UI.WebControls;
public partial class Default6 : System.Web.UI.Page
{
string strConnection = ConfigurationManager.ConnectionStrings["MBOMexcelConnectionString4"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
private bool tableCopied = false;
private DataTable originalTable;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (!tableCopied)
{
originalTable = ((System.Data.DataRowView)e.Row.DataItem).Row.Table.Copy();
ViewState["originalValues"] = originalTable;
tableCopied = true;
}
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
originalTable = (DataTable)ViewState["originalValues"];
foreach (GridViewRow row in GridView1.Rows)
if (IsRowModified(row))
{
GridView1.UpdateRow(row.RowIndex, false);
}
tableCopied = false;
GridView1.DataBind();
}
protected bool IsRowModified(GridViewRow row)
{
int currentID;
string currentName;
string currentLocation;
currentID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
currentName = ((TextBox)row.FindControl("txtName")).Text;
currentLocation = ((TextBox)row.FindControl("txtLocation")).Text;
System.Data.DataRow newRow = originalTable.Select(String.Format("ID = {0}", currentID))[0];
if (!currentName.Equals(newRow["Name"].ToString())) { return true; }
if (!currentLocation.Equals(newRow["Location"].ToString())) { return true; }
return false;
}
protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
TextBox txtname = (TextBox)grdRow.FindControl("txtName");
TextBox txtlocation = (TextBox)grdRow.FindControl("txtLocation");
if (chkTest.Checked)
{
txtname.ReadOnly = false;
txtlocation.ReadOnly = false;
txtname.ForeColor = System.Drawing.Color.Black;
txtlocation.ForeColor = System.Drawing.Color.Black;
}
else
{
txtname.ReadOnly = true;
txtlocation.ReadOnly = true;
txtname.ForeColor = System.Drawing.Color.Blue;
txtlocation.ForeColor = System.Drawing.Color.Blue;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
originalTable = (DataTable)ViewState["originalValues"];
foreach (GridViewRow row in GridView1.Rows)
{
bool result = ((CheckBox)row.FindControl("chkSelect")).Checked;
//int choreID = Convert.ToInt32(row.Cells[0].Text);
//UpdateChores(choreID, result);
CheckBox chkDelete = (CheckBox)row.FindControl("chkSelect");
if (chkDelete.Checked)
{
GridView1.DeleteRow(row.RowIndex);
}
}
tableCopied = false;
GridView1.DataBind();
}
}
Javeed M ShaikhRank #839 · 2.7K reputationPosted Nov 4, 2011, 9:18 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Nov 6, 2011, 4:37 AM EST
Ashwini124 reputationPosted Nov 6, 2011, 8:36 PM EST
please send me any related examples for my problem.
Suthish NairRank #92 · 22.8K reputationPosted Nov 6, 2011, 9:59 PM EST
TulasiRank #2,544 · 549 reputationPosted Nov 6, 2011, 11:11 PM EST
http://www.daniweb.com/web-development/aspnet/threads/202708
http://www.c-sharpcorner.com/Forums/Thread/143518/gridview-row-selection.aspx
Ashwini124 reputationPosted Nov 7, 2011, 12:10 AM EST
This is ok but my requirement is different. In this code they did after clicking a link button
but in my case i have ckeckboxes in one row of gridview, user will click on ckeckboxes may be one or
more and he will click on change color button then it should change the color of the gridview rows depending
upon the checkbox selection. if user selected two checkboxes two rows corresponding to that checkbox
should change the color. please help me.
TulasiRank #2,544 · 549 reputationPosted Nov 7, 2011, 12:44 AM EST
chk the below javascript code
Ashwini124 reputationPosted Nov 7, 2011, 11:26 PM EST
TulasiRank #2,544 · 549 reputationPosted Nov 8, 2011, 12:14 AM EST
is it my code working for u ?
Abhishek Kumar2 reputationPosted Oct 13, 2012, 4:41 AM EDT
thanks
http://www.packersmoversdirectory.net/
packers movers2 reputationPosted Jun 20, 2013, 4:37 AM EDT
thanks
99th
anuj kumar2 reputationPosted Feb 27, 2014, 3:55 AM EST
This is very useful information shared here. I am really thankful for this.
http://www.99th.co.in
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts