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;
}
}
}
Related
I'm having a problem with onDataRowBound. It seems that it put color on the whole column of cell[0]. The other cells has values on it but still it colors my cell. Here's my code.
protected void GridUserMessage_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (String.IsNullOrWhiteSpace(e.Row.Cells[0].Text))
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
What's wrong with this?
Add else block where you set cell's color to white (or some other color that your grid has by default)
protected void GridUserMessage_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (String.IsNullOrWhiteSpace(e.Row.Cells[0].Text))
{
e.Row.BackColor = System.Drawing.Color.Red;
}
else
{
e.Row.BackColor = System.Drawing.Color.White;
}
}
}
EDIT:
I think CSS styling would be better approach to do this.
In your markup set CSS class to your rows and on RowDataBound event set another class as needed. Something like this:
define "normal" style in markup
<asp:GridView runat="server" RowStyle="normal-row" ...>
and in your RowDataBound method set another class if cell values is empty string, like this:
e.Row.CssClass = "red-row";
You are checking Cell[0] values, what if you are changing in your backend database stored procedure and rearrange column name in select list, for example, you are having
BEFORE
SELECT Id,FirstName,LastName FROM EMPLOYEE
AFTER you/someone else change it to
SELECT Id,LastName,FirstName FROM EMPLOYEE
your code will be easily collapse, so its better to check DataItem of that row for values with name and based on that you can take decision. See code rewritten from your code.
protected void GridUserMessage_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Check database column has value or not.
if(String.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ColumnNameOfDataYouWantToCheck")))
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
What I want to do is to change value of row's forth cell if cell number 3 is changed. I have an EditEnding method for my grid. That's my method below. I don't really know how to finish it
that's the grid definition:
<DataGrid x:Name="dataGrid1"... CellEditEnding="dataGrid1_EditEnding">
and the method:
private void dataGrid1_EditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
// initializing DataRowView from my datagrid
DataRowView drv = (DataRowView)dataGrid1.CurrentItem;
// checking if there were any changes
if (drv.Row[3, DataRowVersion.Original] != drv.Row[3])
{
//set value to cell
}
}
Well, i did my stuff, just forget to post it here.
First I did it with EditEnding event, it looked like that:
private void dataGrid1_EditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataRowView drv = (DataRowView)dataGrid1.CurrentItem;
if (drv.Row[3, DataRowVersion.Original] != drv.Row[3])
{
rowView.Row.SetField(4, /* my logic here */);
}
}
The problem was it was adding the value only on second edit. Then I changed my idea and added a RowChanged event to my DataTable, which was like that:
static void dtSP_RowChanged(object sender, DataRowChangeEventArgs e)
{
bool temp = false;
try
{
temp = e.Row[4, DataRowVersion.Original] == e.Row[4];
}
catch { }
if (temp && int.Parse(e.Row[3].ToString()) != -1)
{
e.Row[4] = (/* my logic */);
}
}
The method was going into infinity loop (it was noticing, that fourth row had changed).
And then i saw this:
http://www.windowsdevcenter.com/pub/a/dotnet/2003/05/26/datacolumn_expressions.html
I've ended with one line long code:
dtSP.Columns[4].Expression = "expression";
#blindmeis, I forgott to mention I use ADO.NET, sorry
do not edit the datagridrow - edit the underlying object in wpf!
this mean when the bound property to cell 3 is changed then do your changes to the property bound to cell 4. INotifyPropertyChanged will notify your grid and will show your changes
If you already have logic to calculate cell4 value when cell3 is changed, then when property binded to column 3 is changed you should call INotifyPropertyChanged of property binded to column 3 & 4.
I hide my columns using the solution in following link
How to hide a TemplateField column in a GridView
However this causes problems with update operations, as gridview acts as hidden rows has null value. So how to hide columns after databind?
protected void begv_OrderDetail_RowCreated(object sender, GridViewRowEventArgs e)
{
((DataControlField)begv_OrderDetail.Columns.Cast<DataControlField>().Where(fld => fld.HeaderText == "FileNo").SingleOrDefault()).Visible = "False";
}
Try this,
grid1.Columns[columnIndex].Visible= false;
Edit based on comment of questioner, for getting values of hidden columns
You can use hidden fields to store column wise values. This article has example that will help how to use hidden fields.
Instead of hiding column you can put the data of columns in datakeynames and later access those values. This will be useful in grabbing how to use DataKeyNames. By this method you may need to pass the id from data key names and get the record.
try this example, (i don't speak english)
into RowDataBound ...
protected void gvTeste_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
teste listaTeste = e.Row.DataItem as ListaTeste;
if (listaTeste.ID == 0)
{
e.Row.Cells[2].Text = "Não Atribuido";
}
if (e.Row.Cells[7].Text == "01/01/0001" || e.Row.Cells[8].Text == "01/01/0001")
{
**e.Row.Visible = false;** // disable row
}
}
}
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();
}
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.