What happens in the OnDataBound Event of the Gridview? - c#

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)

Related

How to hide Rows In GridView

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

Changing the colour of a row based on a condition

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.

How to disable a row by selecting value from comboBox in datagridview?

Here I have a column name "Status" in which I am using ComboBox whose values are "Pending" and "Delivered" now I want that when a user selects the delivered from the ComboBox then the entire row or this control should be disabled so user could not change it again and by default its value should be "Pending" how can i do it? Is it possible to disable a single row in gridview?
You cannot disable an individual row. However you can make it readonly:
DataGridView1.Rows[rowIndex].ReadOnly = true;
(This makes row with index of rowIndex set to readonly).
For your specific example, you would want to handle the CellValueChanged event of the datagridview and have code along the lines of:
void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4 && DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "Delivered")
{
DataGridView1.Rows[e.RowIndex].ReadOnly = true;
}
}
You can handle the RowChanged event and do something like
if (Status == 'Delivered')
{
e.Row.Enabled = False;
}

Asp.net Gridview - Why Are DataRowBound changes Lost on sort?

I am making conditional formatting changes to the data in my gridview using a RowDataBound event:
void gvReg_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime lastUpdate DateTime.Parse(DataBinder.Eval (e.Row.DataItem, "LAST_UPDATE");
if (lastUpdate < DateTime.Today.AddMonths(-1))
{
Hyperlink hypLastUpdate = (Hyperlink)e.Row.FindControl("hypLastUpdate";
hypLastUpdate.CssClass = "Error";
hypLastUpdate.NavigateUrl = "http://www.someExampleErrorPage.com";
}
}
}
This works, and sets the proper CssClass to the hyperlink (which makes it a jarring shade of bold red), but once the gridview is sorted (via the user clicking a column heading) the css class is reset on hypLastUpdate and it loses both it's style and associated NavigateUrl property.
The control hypLastUpdate is contained in a template field in a gridview, and it's text value is databound to a field called "LAST_UPDATE".
Is this a planned behavior (is sorting supposed to break the conditional formatting done in RowDataBound events?) or is there something I can check to make sure I am not doing something incorrectly?
I am not using the DataBind method anywhere in the code behind, and viewstate is turned on for the gridview in question.
--EDIT--
It ended up being a mistake in event handling.
I was doing:
gvReg.Sorted += {SomeEventHandler}
Inside of the page load event, but only when it wasn't a postback. This function called gvReg.DataBind after the grid view was sorted. I removed the handler wire up and instead added the event handler function to the OnSorted event. I guess assigned delegates to a gridview are not saved in ViewState between callbacks?
Hi here is a quick example of what I meant on my comment. This is the only way i could think of it:
protected void gvReg_Sorting(object sender, GridViewSortEventArgs e)
{
GridView gridView = (GridView)sender;
if (e.SortExpression.Length > 0)
{
foreach (DataControlField field in gridView.Columns)
{
if (field.SortExpression == e.SortExpression)
{
cellIndex = gridView.Columns.IndexOf(field);
break;
}
}
if (pSortExpression != e.SortExpression)
{
pSortDirection = SortDirection.Ascending;
}
else
{
pSortDirection = (pSortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
}
pSortExpression = e.SortExpression;
}
//Retrieve the table from the database
pSortOrder = pSortDirection == SortDirection.Ascending ? "ASC" : "DESC";
List<Partners> partnerList = GetPartnerList();
gvReg.DataSource = partnerList;
gvReg.DataBind();
}

Applying styles to a GridView matching certain criteria

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.

Categories

Resources