I'm fairly new to ASP.Net so it's probably just me being a bit stupid, but I just can't figure out why this isn't working.
Basically, I have a GridView control (GridView1) on a page which is reading from a database. I already have a CSS style applied to the GridView and all I want to do is change the background image applied in the style depending on if a certain cell has data in it or not.
The way I'm trying to handle this change is updating the CSS class applied to each row through C#. I have the code below doing this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
string s = row.Cells[7].Text;
if (s.Length > 0)
{
row.CssClass = "newRowBackground";
}
else
{
row.CssClass = "oldRowBackground";
}
}
In theory, the data from Cell[7] will either be null or be a string (in this case, likely a person's name).
The problem is that when the page loads, every row in the GridView has the new style applied to it, whether it's empty or not. However, when I change it to use hard coded examples, it works fine. So for example, the below would work exactly how I want it to:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
string s = row.Cells[7].Text;
if (s == "Smith") //Matching a name in one of the rows
{
row.CssClass = "newRowBackground";
}
else
{
row.CssClass = "oldRowBackground";
}
}
It seems as if the top piece of code is always returning the string with a value greater than 0, but when I check the database the fields are all null (except for my test record of "Smith").
I'm probably doing something very simple that's wrong here, but I can't see what. Like I said, I'm still very new to this. One thing I have tried is changing the argument in the if statement to things like: if (s != null), if (s != "") and if (s == string.empty) all with no luck.
Any help is greatly appreciated and don't hesitate to tell me if I'm just being stupid here. :)
You might want to try the code below.
if(String.IsNullOrEmpty(s.trim()))
{
}
Hope this helps.
Also make sure that you only set the CssClass on DataRows.
Check for
if(e.Row.RowType == DataControlRowType.DataRow)
{
}
You might also take a look at scartags answer.
If nothing of the above metioned works, set a breakpoint into the row where you match s against the string and have a look at the actual value
Try this:
if((e.Row.RowType != DataControlRowType.DataRow) || String.IsNullOrEmpty(s.trim()))
{
return;
}
row.CssClass = "newRowBackground";
This assumes that the oldRowBackground CSS class would be applied by default.
Related
I have came across the we and check different web pages but I didnt find the one I was looking for. I have a gridview and all I want is to hide one of the rows based on the value in the cell.
What I need to happen is something like in the logic of this :
if (row = "someValue")
{
row.Visible = false;
}
for the record, I have tried this but no luck:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
string oRoleName = row.Field<string> ("SVal");
if (oRoleName.Equals ("someValue")) {
e.Row.Visible = false;
}
}
It is not base weather the row is the first the 2nd or third (like: e.row[1], e.row[2], etc.) I need to filter the data base on the value in the row. Can anyone teach me how could this be done ?
Would appreciate any help.
In a RowDataBound event add something along the lines of this logic
if (e.Row.Cells[5].Text == "foo") {
e.Row.Visible = false;
}
EDIT:
If youre looking to check the value of each row as its entered (unless im understanding incorrectly, you should probably expand on your question a bit.)
Then you may want to use the event "CellValueChanged"
Check to see if the cell is null beforehand and then do the check for your value and apply logic accordingly below that.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell != null) {
if (dataGridView1.CurrentCell.Value.ToString() == "foo")
{
// do your stuff here.
}
}
}
What you need to do is get the value of the column within the GridViewRow. You are on the right track. In RowDataBound, find the column you are after. Then check its value.
Here is an example. Use FindControl() to get the control in the specified column for the current row. If the control in that column is a Label, check the text of the label to see if it is the value you want hidden. If so, hide the row.
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = e.Row.FindControl("MyLabel");
if (lbl.Text == "MyValue")
{
e.Row.Visible = false;
}
}
}
I have a Gridview where I check the data of some determined cells during the OnDataBound event in order to trigger some action.
public void PaintRows_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[0].Text == "0")
{
//first condition
}
else if (e.Row.Cells[0].Text == "1" && e.Row.Cells[12].Text.Length != 6)
{
//second condition
}
else
{
//launch the action
}
}
Even though all conditions are fulfilled, the action that is triggered by the else statement is always fired. I don't see any logic that explains that. I learned that looping through the rows the event binds the headers too and therefore I check this case in the conditions. But are there any other invisible rows that I am missing and that lead to the fact that the else condition is reached? I hope I did made my point clear. Martin
You mention that you’re checking to exclude the row if it's a header row, but haven’t provided what your actual conditional test is for this..
To pre-filter for only data rows, you would apply this condition:
if(e.Row.RowType == DataControlRowType.DataRow)
I want to change a particular row color of gridview based on some condition, i am using ASP.NET with c#.
I know i can use the HTMLCellPrepared method, but in my method i want to look at the values of other grids as well? Is this possible?
protected void GVResults_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e)
{
if (e.DataColumn.FieldName == "CarrierId")
if ( Convert.ToInt32(e.CellValue) > 0)
e.Cell.ForeColor = System.Drawing.Color.Red;
}
This is the first part of the method, but i want to look at values from other grids in order to make visual changes to this grid. Problem is I dont know how to access values from other grids....
I would recommend you to use the htmlrowprepared event for the conditional coloring of row.
According to the code you have written, below example can help you :
protected void GVResults_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
{
if (e.RowType != GridViewRowType.Data) return;
int value = (int)e.GetValue("CarrierId");
if (value > 0)
e.Row.ForeColor = System.Drawing.Color.Red;
}
Reference:
Changing ASPxGridView Cell and Row Color on Condition
You can use RowDataBound event of GridView to check a condition if your styling depends on data and set a style for that condition.
Here is an example of this.
I have a Gridview that has editable fields in it am calculating the sum of every column and displaying it in the footer columns my gridview column looks like
This is not updating the value can anyone please help me where i should correct my code?
if (i == objGrid.rows.length)
{
objGrid.rows[i].cells[2].children[0].innerText = ClsSum;
objGrid.rows[i].cells[3].children[0].innerText = NonSaleSum;
objGrid.rows[i].cells[7].children[0].innerText = SecSum;
}
You can check for the rows.length -1.
And make sure that the code snippet will get executed after the postback.
I hope this code executes after you bind your gridview . So please check if you have applied
(!IsPostBack) in your GridviewBind method .
Also check does if your code reaches this method when reloads or updates your values ?
You can try this also, put below code out side of loop
//create css class called "footerClass" and apply on column fotter template
var objGrid= document.getElementById('<%=YourGrid.ClientID%>');
var cells = objGrid.getElementsByClassName('footerClass')
cells[0].innerText = ClsSum;
cells[1].innerText = NonSaleSum;
cells[2].innerText = SecSum;
I hope you are using Grid_RowDataBound() event and accordingly that you can try this
protected void Grid_RowDataBound(Object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Footer)
{
e.Row.cells[2].children[0].innerText = ClsSum;
e.Row.cells[3].children[0].innerText = NonSaleSum;
e.Row.cells[7].children[0].innerText = SecSum;
}
}
Using MS Visual Studio and C#.net 4.0.
So, just completed my other part of my program that checks for duplicates which finally works "many thanks to all that helped". Showed my boss who liked it but then asked "if he was to select the results of the datagridview that shows the part numbers with duplicate values, is it then possible to highlight the maindatagridview that is equal to the result selected".
Now first of all I understand what he means, but wording this has been rather difficult and as such searching for some examples to get me started has been very difficult.
Now although I don't have any code I can show the code I currently have.
The first thing I did was to identify an event handler on the datagrid that could detect what row is selected, I'm going to use "selectionchanged".
UPDATE:: ok I though I would show you what I meant by re-using my code.
Please note that the code is very similar but is only a starting point, I may incorporate the previous method in with the new one.
private void MyErrorGrid_SelectionChanged(object sender, EventArgs e)
{
string getPartSelected;
getPartSelected = MyErrorGrid.CurrentCell.Value.ToString();
foreach (DataGridViewRow row in ParetoGrid.Rows)
{
var cellValue = row.Cells["Keycode"].Value;
if (cellValue != null && cellValue.ToString() == getPartSelected)
{
ParetoGrid.Rows[row.Index].DefaultCellStyle.BackColor = Color.Red;
}
}
}
As you can see this works, but there are some problems. It highlights But doesnt un-highlight, so I think I need to store the previous selected? (not sure this is the best way).
Also need to Add navigation, as highlighting is not good enough for the user. At the moment ive added in selected = true but again when the selection changes i need to use the previous value.
Ok this feels like I'm cheating, but here's what I did to solve the problem.
private void MyErrorGrid_SelectionChanged(object sender, EventArgs e)
{
string getPartSelected;
getPartSelected = MyErrorGrid.CurrentCell.Value.ToString();
foreach(DataGridViewRow allrow in ParetoGrid.Rows)
{
ParetoGrid.Rows[allrow.Index].DefaultCellStyle.BackColor = Color.Empty;
ParetoGrid.Rows[allrow.Index].Selected = false;
}
//might need a do while
foreach (DataGridViewRow row in ParetoGrid.Rows)
{
var cellValue = row.Cells["Keycode"].Value;
if (cellValue != null && cellValue.ToString() == getPartSelected)
{
ParetoGrid.Rows[row.Index].DefaultCellStyle.BackColor = Color.Red;
ParetoGrid.Rows[row.Index].Selected = true;
}
}
}
As you can see it basically clears all before entering the next change, so it works but without using a previous value.
If anyone has a better solution feel free to answer
The only thing is when there is lots of rows, although it selects and highlights it doesnt scroll the datagridview is there a similar method to do this?