Reputation: 195
I am having a gridview with databound values.I have 5 rows in "ReportMain" table with fields (Id,Date,Time,Name,Area,Work,Description,Priority,status) and i am showing all 5 rows in gridview (gridview name "gridview1" at run-time using linq query in c# asp.net and at run-time in those 5 rows (3 row status is "completed" and other 2 rows status is "Pending").Now i want to display the rows with the status field as "completed" in green color and the other 2 rows without any color during load. All row cell values have values and they are not null..
My Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var query = from row in db.ReportMains select row;
GridView1.DataSource = query;
GridView1.DataBind();
}
}
protected void chkRow_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
GridViewRow gdrow = (GridViewRow)checkbox.NamingContainer;
if (checkbox.Checked)
{
gdrow.BackColor = System.Drawing.Color.LimeGreen;
int rowindex = gdrow.RowIndex;
int i = Convert.ToInt32( gdrow.Cells[0].Text);
string a = gdrow.Cells[3].Text.ToString();
var Query = (from row in db.ReportMains where row.EmployeeName==a && row.Id==i
select new
{
row.EmployeeName,row.Id
}).Distinct();
if (Query.Count() > 0)
{
var Q1 = from row in db.ReportMains where row.Id == Query.Single().Id select row;
Q1.Single().Status = "Completed";
db.SubmitChanges();
}
}
else
{
gdrow.BackColor = System.Drawing.Color.MintCream;
int rowindex = gdrow.RowIndex;
int i = Convert.ToInt32(gdrow.Cells[0].Text);
string a = gdrow.Cells[3].Text.ToString();
var Query = (from row in db.ReportMains
where row.EmployeeName == a && row.Id == i
select new
{
row.EmployeeName,
row.Id
}).Distinct();
var Q1 = from row in db.ReportMains where row.Id == Query.Single().Id select row;
Q1.Single().Status = null;
db.SubmitChanges();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
ReportMain rm = (ReportMain)e.Row.DataItem;
string st = rm.Status;
bool isCompleted = st == "Completed";
e.Row.BackColor = isCompleted ? Color.Green : Color.White;
}
Please help...
Upvotes: 3
Views: 1273
Reputation: 460238
You could always use the rows DataItem
to get the underlying DataSource
:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
string name = row.Field<string>("Name");
string dept = row.Field<string>("Dept");
string work = row.Field<string>("Work");
string status = row.Field<string>("Status");
bool isCompleted = status == "Completed"; // or status.Equals("Completed", StringComparison.CurrentCultureIgnoreCase)
e.Row.BackColor = isCompleted ? Color.Green : Color.White;
}
}
I'd use the RowDataBound
event since that is triggered for every row in the GridView
only during data-inding(after gridView1.DataBind()
), so not necessarily on every postback.
The following error is shown. Unable to cast object of type 'ReportMain' to type 'System.Data.DataRowView'.
What is ReportMain
? If it's a custom class cast e.Row.DataItem
to it. Then you can access the properties like i did with the DataRow
above.
For example:
// ....
ReportMain rm = (ReportMain) e.Row.DataItem;
string name = rm.Name;
string dept = rm.Dept;
string work = rm.Work;
string status = rm.Status;
bool isCompleted = status == "Completed"; // or status.Equals("Completed", StringComparison.CurrentCultureIgnoreCase)
e.Row.BackColor = isCompleted ? Color.Green : Color.White;
Upvotes: 3