change color of a cell in gridview - c#

protected void gvSC_RowDataBound(object sender, GridViewRowEventArgs e)
{
string stext = TextBox1.Text.ToString();
stext = stext.Trim();
if (e.Row.RowType == DataControlRowType.DataRow)
{
int i;
for (i = 0; i < e.Row.Cells.Count; i++)
{
if ((e.Row.Cells[i].Text).ToString() == stext)
{
e.Row.Cells[i].ForeColor = System.Drawing.Color.Green;
}
}
}
}
here even though if condition satisfies, the program control is not going inside and executing forecolor statement

Try (e.Row.Cells[i].Text).Trim() in case any white spaces at last which are getting unnoticed.

Related

Change cell colour on all cells with 1

I'm trying to change cell colour on all cells with value = "1"
It is on gridview with databound columns
Looks like this
The code i have tried is this, but thos changes cells with more numbers then only "1"
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text.ToLower().IndexOf("1") > -1)
{
e.Row.Cells[i].ForeColor = System.Drawing.Color.Red;
}
}
}
}
Protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text.Equals("1"))
{
e.Row.Cells[i].ForeColor = System.Drawing.Color.Red;
}
}
}
}

changing gridview row color on condition within button click function

I have a loop that goes through my gridview and counts the instances where gender field is set to 'M' when a button is clicked.
I would like to also change that color of that row with the same condition, i.e. if the gender is 'M'.
Here is my loop so far and any help would be very appreciated!
protected void Button2_Click(object sender, EventArgs e)
{
int intfemdelegates = 0;
foreach(GridViewRow oItem in GridView1.Rows)
{
if (oItem.Cells[6].Text.Contains('F'))
{
intfemdelegates = intfemdelegates + 1;
GridView1.RowStyle.BackColor = System.Drawing.Color.Red;
}
Label2.Text = Convert.ToString(intfemdelegates);
}
}
You are changing RowColor on whole Grid1. change to this
protected void Button2_Click(object sender, EventArgs e) {
int intfemdelegates = 0;
foreach(GridViewRow oItem in GridView1.Rows) {
if (oItem.Cells[6].Text.Contains('F')) {
intfemdelegates = intfemdelegates + 1;
oItem.BackColor = System.Drawing.Color.Red;
}
Label2.Text = Convert.ToString(intfemdelegates);
}
This code may be useful... good luck!
int intfemdelegates = 0;
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
if (dataGridView1[1, i].Value.ToString() == "F")
{
intfemdelegates = intfemdelegates + 1;
dataGridView1[1, i].Style.BackColor = Color.Red;
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
}
}
lblcount.Text = intfemdelegates.ToString();

Change the fore color of gridview row base on row text

There is a row with "Correct" text in my database. However the if condition is never true.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblremark = (Label)e.Row.FindControl("lblremark");
if (lblremark.Text == "Correct")
{
e.Row.ForeColor = System.Drawing.Color.Black;
e.Row.BackColor = System.Drawing.Color.Cyan;
}
else
{
e.Row.ForeColor = System.Drawing.Color.Black;
e.Row.BackColor = System.Drawing.Color.Orange;
}
}
}
Try this if you are finding label correctly
if (lblremark.Text.Trim().ToLower().Equals("correct"))
note: this was just off the top of my head
//number of rows
int rowNum = GridView1.Rows.Count;
//go through each row
for (int i = 0; i < rowNum; i++)
{
//get the cell text
string corr= GridView1.Rows[0].Cells[0].ToString();
//set color based on the text in the cell
if (corr == "Correct")
{
GridView1.SelectRow(i);
GridView1.SelectedRow.ForeColor = Color.Black;
GridView1.SelectedRow.BackColor = Color.Cyan;
}
else
{
//do watever
}
}

How to tooltip the headers of the GridView using the list of items in ListBox

I have a GridView and a list of items in a ListBox. Excluding the first column in the GridView.
I want to tooltip each and every header with the corresponding item in the ListBox. I mean the tooltip for GridView2.Columns[i].HeaderText = ListBox.Items[i].Tostring().
Here is what I have tried:
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < GridView2.Columns.Count; i++)
{
String header = GridView2.Columns[i].HeaderText;
if (header.Length != 0)
{
e.Row.Cells[i].ToolTip = ListBox4.Items[i].ToString().Trim();
}
}
}
}
This is giving me an exception error: Index was out of range. Must be non-negative and less than the size of the collection.
Kindly help. Thank you.
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < GridView2.Columns.Count; i++)
{
String header = GridView2.Columns[i].HeaderText;
if (header.Length != 0)
{
e.Row.Cells[i+1].ToolTip = ListBox4.Items[i].ToString().Trim();
}
}
}
}
Just suppose to use Cells[i+1] instead of Cells[i]

Reusable Method to change gridview cell colour

I need to change the colour of gridview cells based on the data value. I can quite easily do this using a datarow view in the Gridviews RowDataBound Event and an if statement (see below), however I need to do this on 30 columns which will be rather long winded and a pain to change if the business rules change. How can I encapsulate the following into a reusable method that I can call and just pass in the data column and the cell index ?
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
int A = Int32.Parse(drv["A"].ToString());
if (A <= 74)
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
}
public void SetColor(DataGridViewRow row, string columnName, int cellIndex)
{
var data = (GridViewRow)row.DataItem;
int number = Convert.ToInt32(data[columnName]);
if (number > 74) return;
row.Cells[cellIndex].BackColor = Color.Red;
}
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataRowType.DataRow) return;
SetColor(e, "A", 2);
}
Figured it out - Mr Meckley put me on the right track and my working (if inelegant) solution is:
public void SetColor2(GridViewRow row, string columnName, int cellIndex)
{
if (row.RowType == DataControlRowType.DataRow)
{
int number = Convert.ToInt32(columnName);
if (number == 0)
{
row.Cells[cellIndex].Text = "";
return;
}
else if ((number > 0) && (number <= 74))
{
row.Cells[cellIndex].BackColor = System.Drawing.Color.Red;
row.Cells[cellIndex].ForeColor = System.Drawing.Color.Black;
return;
}
}
}
Usage:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
SetColor2(e.Row, drv["A"].ToString(), 2);
SetColor2(e.Row, drv["B"].ToString(), 3);
etc...
}
}

Categories