Changing the colour of a row based on a condition - c#

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.

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

How to update the value of a Label in footer template of a gridview?

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

Can I make row cell value readOnly on XtraGrid just for one row?

How can I make a specific row cell readonly(not editable) on XtraGrid? For example just for row[0] but not all rows.
You can use the GridView.CustomRowCellEdit event:
//...
var repositoryItemTextEditReadOnly = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
repositoryItemTextEditReadOnly.Name = "repositoryItemTextEditReadOnly";
repositoryItemTextEditReadOnly.ReadOnly = true;
//...
void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) {
if(e.RowHandle == 0)
e.RepositoryItem = repositoryItemTextEditReadOnly;
}
You can use the ColumnView.ShownEditor event:
void gridView1_ShownEditor(object sender, EventArgs e)
{
ColumnView view = (ColumnView)sender;
view.ActiveEditor.Properties.ReadOnly = view.FocusedRowHandle == 0;
}
Source: How to Conditionally Prevent Editing for Individual Grid Cells
When you need to make a grid cell read-only based on a condition, the
best approach is to use the ShowingEditor event of the
GridView and prevent editing via the e.Cancel parameter passed to the event. Simply set it to True when it is necessary to prevent
editing.
// disable editing
private void gridView1_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e) {
GridView view = sender as GridView;
e.Cancel = view.FocusedRowHandle == 0;
}
Source - How to display disabled buttons for particular cells within a ButtonEdit column
Another approach is that assign a read only repository editor control as #DmitryG suggested and I have also implement that way some times when there was a column which contains a button.
In your case you should create two TextEdit repository items. One with
the enabled button and another with the disabled button. Then handle
the GridView.CustomRowCellEdit event and pass the necessary
repository item to the e.RepositoryItem parameter according to a
specific condition. Please see the Assigning Editors to Individual
Cells help topic for additional information.
private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
if (e.Column.Caption == "Any2")
{
if (e.RowHandle == 0)
e.RepositoryItem = columnReadOnlyTextEdit;
else
e.RepositoryItem = columnTextEdit;
}
}
References:
How to customize the Look-And-Feel of my grid cells
How to make my grid columns read-only

Hiding a particular cell based on row and column in flexgrid

I have a flexgrid (flex component grid), how do i hide a cell.
For ex: 2nd row and 5th column - i need to hide/remove based on some condition.
for say
if(C1FlexGrid1.Rows[2][5].ToString().Length <0)
{
//I want this to be invisible.
C1FlexGrid1.Rows[2][5].isVisible=false;
}
There is no propery that supports isVisible the way i have used. Any way i can achieve this ? Thanks.
Finally i figured it out:
Create a ownerdrawcell event for your winforms component grid:
componentGrid.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw;
componentGrid.OwnerDrawCell += componentGrid_OwnerDrawCell;
Method
void componentGrid_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e)
{
var value = componentGrid.GetCellCheck(e.Row,e.Col);
//Your custom condition
if (value is bool)
{
//Will hide the cell
e.Style.Display = DisplayEnum.None;
}
else
{
//Will show the cell
e.Style.Display = DisplayEnum.Stack;
}
}

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

Categories

Resources