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();
}
}
anuj kumarPosted Feb 27, 2014, 8:55 AM
This is very useful information shared here. I am really thankful for this.
http://www.99th.co.in
packers moversPosted Jun 20, 2013, 8:37 AM
thanks
99th
Abhishek KumarPosted Oct 13, 2012, 8:41 AM
thanks
http://www.packersmoversdirectory.net/
TulasiPosted Nov 8, 2011, 5:14 AM
is it my code working for u ?
AshwiniPosted Nov 8, 2011, 4:26 AM
TulasiPosted Nov 7, 2011, 5:44 AM
chk the below javascript code
AshwiniPosted Nov 7, 2011, 5:10 AM
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.
TulasiPosted Nov 7, 2011, 4:11 AM
http://www.daniweb.com/web-development/aspnet/threads/202708
http://www.c-sharpcorner.com/Forums/Thread/143518/gridview-row-selection.aspx
Suthish NairPosted Nov 7, 2011, 2:59 AM
AshwiniPosted Nov 7, 2011, 1:36 AM
please send me any related examples for my problem.
Suthish NairPosted Nov 6, 2011, 9:37 AM
Javeed M ShaikhPosted Nov 4, 2011, 1:18 PM