Change cell colour on all cells with 1 - c#

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

Related

Can I apply operation to a range of cell indexes in GridView's RowDataBound Event?

I am setting GridView's layout features in the RowDataBound event based on the content of specific cells
private void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text == " ")
{
e.Row.Cells[4].BackColor = System.Drawing.Color.White;
e.Row.Cells[5].BackColor = System.Drawing.Color.White;
e.Row.Cells[6].BackColor = System.Drawing.Color.White;
Is there a way to specify the range (cell index 4 to 6) where this operation has to be applied to?
Something like:
private void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text == " " && e.Row.CellIndex >= 4 && e.Row.CellIndex <= 6)
{
e.Row.BackColor = System.Drawing.Color.White;
That should be what you need:
private void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
for (int i = 4; i <= 6; i++)
if(e.Row.Cells[1].Text == " ")
e.Row.Cells[i].BackColor = System.Drawing.Color.White;
}
If you really need to check if its a DataControlRowType.DataRow, you can insert it to the if in the for loop.

How to apply "Readonly" in gridview textbox using BoundField when edit mode

I want READONLY on text box using Boundsfield. So far enabled is working but it not carry value in rowupdating
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
var nopoject = ddlproject.SelectedValue.ToString();
Int32 curntMonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
int Mont1 = curntMonth - 1;
var Comtext = "Rst" + Mont1.ToString();
GridView1.EditIndex = e.NewEditIndex;
BindData();
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 0; i < GridView1.Columns.Count; i++)
{
String headertext = GridView1.Columns[i].HeaderText;
String cellText = row.Cells[i].Text;
if (Comtext == "Rst1")
{
GridView1.Rows[e.NewEditIndex].Cells[i].Enabled = true;
}}}
Below code not working :
GridView1.Rows[e.NewEditIndex].Cells[i].Attributes.Add("readonly", "readonly");
GridView1.Rows[e.NewEditIndex].Cells[i].CssClass = "read-only";
Please advise. I want readonly on boundsfield programically when inline edit
Thank you
You can use this snippet.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
TextBox textBox = e.Row.Cells[0].Controls[0] as TextBox;
textBox.Enabled = false;
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == DataControlRowState.Alternate)
{
//on you condition
TextBox txt = (TextBox)e.Row.FindControl("ControlID");
if(txt !=null)
{
txt.Attributes.Add("readonly", "readonly");
}
}
}
You can also make textbox readonly in row editing event as below.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
var nopoject = ddlproject.SelectedValue.ToString();
Int32 curntMonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
int Mont1 = curntMonth - 1;
var Comtext = "Rst" + Mont1.ToString();
GridView1.EditIndex = e.NewEditIndex;
BindData();
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 0; i < GridView1.Columns.Count; i++)
{
String headertext = GridView1.Columns[i].HeaderText;
String cellText = row.Cells[i].Text;
if (Comtext == "Rst1")
{
//GridView1.Rows[e.NewEditIndex].Cells[i].Enabled = true;
TextBox tx_chdets = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl(“TextBox1”);
if(tx_chdets!=null)
{
tx_chdets.Readonly=true;
}
}
}
}
}

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
}
}

change color of a cell in gridview

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.

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]

Categories