I need to highlight gridview rows based on values from datatable.
I have highlighted values like if any particular cell values has met some conditions then I can highlight using this code.
if(int.Parse(DataBinder.Eval(e.Row.DataItem,"Risk").ToString()) > 100)
{
e.Row.BackColor = Color.FromName("#FAF7DA");
}
Now my questions is, in my rowdatabound event I want to check values in datatable and I need to highlight values in the gridview.
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = DataRepository.highlightRow();
string[] strInactive = dt.AsEnumerable().Select(row => row.Field<string>("product_id")).ToArray();
foreach (GridViewRow row in gvProducts.Rows)
{
for (int i = 0; i < gvProducts.Columns.Count; i++)
{
if (gvProducts.Rows[0].Cells[0].Text.Contains("how to pass array values"))
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
}
}
For ex:
Here the datatable will return only one column values like this,10, 20, 20. Then I need to highlight rows of these values in the gridview.
you should be able to check e.Row.DataItem for values that meet your criteria
You can do something like this in the RowDataBound event:
if (e.Row.RowType == DataControlRowType.DataRow) {
DataRowView drv = (DataRowView) e.Row.DataItem;
if( drv("Risk") == <some condition> ) {
e.Row.BackColor = Drawing.Color.Black // :)
}
}
Perhaps this is what you are looking for:
private const System.Drawing.Color HIGHLIGHT = System.Drawing.Color.Yellow;
private const System.Drawing.Color NORMAL = System.Drawing.Color.White;
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = DataRepository.highlightRow();
string[] strInactive = dt.AsEnumerable().Select(row => row.Field<string>("product_id")).ToArray();
foreach (var value in strInactive)
{
e.Row.BackColor = (e.Row.Cells[0].Text == value) ? HIGHLIGHT : NORMAL;
}
}
}
I do not know what is in your DataTable, so I don't know what you want to test for.
Related
Ok so i have 4 columns named actual and 4 columns named target, i need to sum all of the actual columns, and pass the total to a label... Can anybody help?
What i have so far but i know its incorrect..
public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) //checking the index of cells and changing the font colour
{
System.Data.DataRowView drv = (System.Data.DataRowView) e.Row.DataItem;
DataTable dt = drv.Row.Table;
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (dt.Columns[i].ColumnName.Contains("Actual"))
{
e.Row.Cells[i].ForeColor = Color.Red;
}
dt.Columns.Add("Total", typeof(Double));
foreach (DataRow row in dt.Rows)
{
int sum = row.Table.Columns<DataColumn>().Sum(dc => (int)row[dc]);
row.SetField("Total", sum);
labeltotal.Text = sum.ToString();
}
}
You can simply do that like this
Int32 total = 0;
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total = total + Convert.ToInt32(e.Row.Cells[1].Text);
//here you can give the column no that you want to total like e.Row.Cells[1] for column 1
lblTotal.Text = total.ToString();
}
}
I have a gridview with first columns is same as headers i.e. same value with autogeneratecolumns as true, what i need to do in RowDatabound if HeaderText Equals to Intersect of the first Column text, Change the color to Yellow. Please see the attached image of the Desireed Gridview Output.
GridViewDesiredOutput
HTML
<asp:GridView ID="GvSamples" OnRowDataBound="GvSamples_RowDataBound" runat="server" AutoGenerateColumns="True">
C#
public void BindSamplesGrid()
{
DataTable _dt = new DataTable();
_dt = BL.GetSample(_conn);
GvSamples.DataSource = _dt;
GvSamples.DataBind();
}
protected void GvSamples_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text == e.Row.Cells[i].Text)
e.Row.Cells[i].Text = e.Row.Cells[i].Text.Replace("_", " ");
}
}
}
I am using asp.net C# Gridview.
Thank you
There are a couple of issues with your code. First of all you can only access the header row in DataControlRowType.Header, so coloring the cells as desired must be done in DataControlRowType.DataRow.
Next you are evaluating exactly the same value, so it will always be true: e.Row.Cells[i].Text == e.Row.Cells[i].Text, although this does not seem to do much except replace a _, which has nothing to do with getting the cell color to be yellow.
The below snippet does what you need. It gets the current row number and colors the correct cell yellow.
protected void GvSamples_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//get the current row number
int rowIndex = e.Row.RowIndex;
//check if the rownumber does not exceed the column count
if (rowIndex < e.Row.Cells.Count - 1)
{
//make the cell yellow
e.Row.Cells[rowIndex + 1].BackColor = Color.Yellow;
}
}
}
I have put a code to COLOR the background of GRIDVIEW Cell# 14 if cell's text != "nbsp;" and it does work except for the last row. It doesn't color the last row even it isn't equal to "nbsp;"
protected void grdviewCases_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (GridViewRow gr in grdviewCases.Rows)
{
if (gr.Cells[14].Text != " ")
{
gr.Cells[14].BackColor = Color.Red; ;
gr.Cells[14].ForeColor = Color.WhiteSmoke;
}
}
}
}
You need not loop rows in RowDataBound event, you may just use e object to reference each row
protected void grdviewCases_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[14].Text != " ")
{
e.Row.Cells[14].BackColor = Color.Red; ;
e.Row.Cells[14].ForeColor = Color.WhiteSmoke;
}
}
}
For more details check system.web.ui.webcontrols.gridview.rowdatabound
I am using RowDataBound Event to calculate the sum of a column data. The variable in which i am getting the sum of column values is becoming zero at the end of the rowdatabound event, because its initial value is zero. How can I store the sum of values in a variable to use its value outside the event. Thanks
int totSubTot = 0;
public double TotalAmount;
protected void gvShowOrder_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
totSubTot += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "SubTotal"));
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[2].Text = "Grand Total";
e.Row.Cells[2].Font.Bold = true;
e.Row.Cells[3].Text = totSubTot.ToString();
e.Row.Cells[3].Font.Bold = true;
TotalAmount = Convert.ToDouble(e.Row.Cells[3].Text);
}
}
Why not get the sum from data source?
var gridView = sender as GridView;
var dataSource = gridView.DataSource as IEnumerable<YourDataObject>;
e.Row.Cells[3].Text = dataSource.Sum(item => item.YourProperty).ToString();
Your logic seems to be off.
In your first if-statement you assign totSubTot to the DataItem, but in your else you are not assigning anything to it.
It probably only goes into one of your if/else if and thats why. Try this
if (e.Row.RowType == DataControlRowType.DataRow)
{
totSubTot += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "SubTotal"));
TotalAmount =Convert.ToDouble(totSubTot);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
totSubTot += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "SubTotal"));
TotalAmount =Convert.ToDouble(totSubTot);
e.Row.Cells[2].Text = "Grand Total";
e.Row.Cells[2].Font.Bold = true;
e.Row.Cells[3].Text = totSubTot.ToString();
e.Row.Cells[3].Font.Bold = true;
}
please try this
static int totSubTot = 0;
static double TotalAmount;
protected void gvShowOrder_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
totSubTot += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "SubTotal"));
} else if (e.Row.RowType == DataControlRowType.Footer) {
e.Row.Cells[2].Text = "Grand Total";
e.Row.Cells[2].Font.Bold = true;
e.Row.Cells[3].Text = totSubTot.ToString();
e.Row.Cells[3].Font.Bold = true;
TotalAmount = Convert.ToDouble(e.Row.Cells[3].Text);
}
}
I have a gridview where I format a specific number of cells for specific rows:
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 1 || e.Row.RowIndex == 2)
{
for (int i = 0; i < 14; i++)
{
e.Row.Cells[i].ForeColor = Color.Black;
e.Row.Cells[i].BackColor = ColorTranslator.FromHtml("#EAFDB3");
e.Row.Cells[i].Font.Bold = true;
}
}
}
}
Right now, the rows that get changed are static (row 1 and 2).
I've added functionality to the gridview and now need a dynamic number of rows formatted (between 2 and 10). The rows will always be next to eachother.
How can choose the rows dynamically?