.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Changing Datagrid Cell foreground colour depending on previous value

Posted By:      Posted Date: September 27, 2010    Points: 0   Category :WPF


I have a datagrid bound to a observable collection and a background thread is regularly updating the collection when an event is fired.( subscribed to a market data service) . I need to change the foreground of the cell value if (Previousvalue > NewValue) . I guess I need a conveter but how can I get access to the previous value ? Or is there a smarter way to do this. Performace is a concern as well.

Thanks v much.


View Complete Post

More Related Resource Links

Get Cell Value of DataGridComboBox Column for WPF DataGrid

hello Friends I am using WPF Datagrid (WPFToolKit Control) in my appliction I Have a DataGridComboBox Column which is filled with some records.  <WpfToolkit:DataGridComboBoxColumn ScrollViewer.CanContentScroll="True" CellStyle="{StaticResource LeftAlignedCellStyle}" Header="Item Code" x:Name="CmbItems" Width="250" SelectedValueBinding="{Binding ITEMCODE}" DisplayMemberPath="ITEMDESC" SelectedValuePath="ITEMCODE"  > </WpfToolkit:DataGridComboBoxColumn> Now how to get selectedText of that combobox for current row. my current focus on other cell  Please Help Me regards Vipul

how to sum the rating value in a grid view row with previous value of that cell?


hi all 

 i have grid view and in item template i use a ajax rating out side the grid view rating work well but inside no.

i want to sum the value that client vote with the previous votes i read many tuterials but i cant sum.here is my code:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
        AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
            <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" 
                SortExpression="id" />
            <asp:BoundField DataField="tozihat" HeaderText="tozihat" 
                SortExpression="tozihat" />
            <asp:BoundField DataField="url" HeaderText="url" SortExpression="url" />
            <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
            <asp:TemplateField HeaderText="mark" SortExpression="mark">
                    <asp:TextBox I

How to change the datagrid cell background color linear to the value



I have a WPF 4.0 Datagrid displaying order values and I need to colour the backgroud of the high value orders so the user will notice high value orders easily. Is it possible to set the colour from white to red but increasing in steps say every 10,000 and increase the colour intensity according to the value.

Many Thanks.


How to wrap the Cell in DataGrid? PLS HELP ME


Dear all,

Are Anyone know how to wrap the cells in the Datagrid (in WPF) to show long texts wrapped? I tried all the way, but due to lack of experience and knowledge in WPF, I am not able to make it run

I declared following style in the Resources as like below

 <Style x:Key="myGridCellStyle" TargetType="DataGridcell">
 <Setter Property="ContentTemplate">


Changing Background Color Of DataGrid Row WPF 4

In this post I will show you the way you can change the background color of the data grid row depending on the binding value. Let us start with the code of how to highlight or change the background color of the data grid row.

changing Asp.net Gridview's cell from Label to edit mode in code behined file


 Hi all,

In my asp.net application i'm using gridview to which i'm binding data dynamically by writing a select query in code behined file (all column's are autogenerated) now i want to change gridview cell to edit mode from label to textbox or dropdown on clicking of gridview's cell.How can i achive this in code behined file.

thanks in advance

Create controls inside the datagrid cell based on the datatype uisng WPF


I have a requirement that I need to develop a component which should display variable types of data inside a datagrid. The datagrid will have two coulmns. Fisrt column will display the list Captions (like: FirstName, DateOfBirth, IsMarried, Age) and the second column will display the list of data based on their data type, like:

> As FirstName is in string format it should display the value inside a TextBox control on right side of FirstName caption

> As DateOfBirth is in Date format it should display the Date value inside a DatePicker control on right side of DateOfBirth caption

> As IsMarried is in Boolean format it should display the CheckBox control with check state on right side of IsMarried caption

> As Age is in integer format it should display the integr value inside a TextBox Control on right side of Age caption.

If any body has any idea please guide me the way to develop. I request you to kindly help on this for creating this control



KeyUp event on DataGrid cell or column




I've a DataGrid in my wpf, and I'm creating my columns dynamically via code,

i want to handle the key event on the dataGrid column.

how can i handle event on dataGrid column (something like dataGridView has in winforms).

here is an example:

col = new DataGridTextColumn();
            col.Header = "sum";
            col.Width = new DataGridLength(15, DataGridLengthUnitType.Star);
            col.Binding = new Binding("sum");




Changing crid cell content at runtime


In a cell in a grid, I have a TreeView. When the parsing of the data behind the treeview yields an error, I would like to replace the treeview with a textblock with information on the error details.

I'm thinking about using a datatemplateselector, but I can't figure out how, or if this is the best approach. Any suggestions?

Alternatively I will place a ContentControl in the grid cell and populate it with the correct content from the code behind file, I just think there could be a more elegant solution in pure XAML.

how to get a cell control in DataGrid


Dear everyone

i have tried to get a cell control in datagrid, but i can't get it.



        void SimPle_Loaded(object sender, RoutedEventArgs e)

        Microsoft.Windows.Controls.DataGridCell cell = GetCell(1, 1);
            TextBlock tb = cell.Content as TextBlock;   // i can't get the textblock, the cell.content have a binding object, not control
            if (tb != null)


          //<Output:OutputIO x:Name="WriteOutput" BorderThickness="6" Height="38" Width="150" IO="{Binding Mode=OneTime, Path=IOHandle}"  />

         // because i want to get my usercontro

Gridview column changing visibility depending on a row.text

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 if (e.Row.Cells[1].Text == null)
                GridView1.Columns[1].Visible = false;


and im not getting the result i expected =\


Setting focus on cell in datagrid after CollectionChanged event.




Having trouble setting the focus on a cell after the collection recieves a new row. This is my code:

 sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
			DataGridRowObjects<My_DataGridRow> oc = (DataGridRowObjects<My_DataGridRow>)sender;
 (e.Action == NotifyCollectionChangedAction.Add)
				//set focus to column 2 of the new row

				DataGridCell cell = my_datagrid.GetCell(e.NewStartingIndex, 2);
 (cell != null
				 DataGridCellInfo info = new
				 my_datagrid.CurrentCell = info;

Is there any event that fires after the CollectionChanged? My DataGridRow doesn't have a CellPresenter att this stage and GetCell fails. If I do the same with a button event instead, it works fine.

I just tried to add a dispatcher message instead but that did nothing:

Dispatcher.BeginInvoke(new Action(delegate()
  //set focus to column 2 of the new row
  DataGridCell c

Datagrid Validation depending on whole collection - enforcing unique values


I have a situation where I have an IList<T> collection of objects bound to a datagrid. I have one property, name, bound to a DataGridTextColumn. I want to enforce the rule that name should be unique within the collection and was wondering what might be the best way to do it.

Most of the validation examples I've seen either validate a single cell or else validate a row, allowing you to compare several values within the same row. But I need to validate the value of a single cell based on the values in the entire collection.

How to assign a value to wpf datagrid cell


Let say I have a wpf datagrid with 2 columns (ProductID and Description).  ProductID column is a combobox and the Description is a Textbox.  On the SelectionChanged event of the ProductID, I want to assign a value to the Description column.  I need to know how to assign the value to the Description textbox for the row of the combobox that fired the SelectionChanged event.  Can someone please provide a sample code?  Thanks

Focused cell in WPF DataGrid



Hello guys

We have two WPF DataGrids in two TabItems(in same TabControl) when we travel between two TabItems, grids did not store focused Cell  and move focused cell to first cell. Is it a true behaviour?


Thank you.

How do I make a DataGrid fire an event when a cell is changed?


I tried to figure this out a while ago but gave up - now I'm in a situation where I need to try again.

I have a DataGrid (using .NET 4) and I'm trying to fire an event when a cell is changed.  However, I cannot generate any events until a different row is selected.  So, for example, I have a datagrid with a checkbox column and I want an event to occur when the user changes one of the checkboxes.  As it is right now, I can't generate an event until the user selects another row after changing the checkbox.

I've tried every type of event I can find - RowEditEnding, CellEditEnding, CurrentCellChanged, MouseLeftButtonUp, MouseLeftButtonDown, etc.  None is ever fired when the checkbox is changed - they don't fire until another row is selected.

So, I can click away on a checkbox hundreds of times and no events will be generated.  As soon as I change rows, though, the events occur.

Any help with this one?  This is such an elementary problem I'm sure there's something basic I'm missing.  Suffice it to say, I don't use WPF very often :)  I've seen the same question asked elsewhere and there are supposed answers but they are extremely complicated and I can't get them to work.

Thanks in advance,


DataGrid Cell Background Color


Can someone show me how to change the background color of a DataGrid cell.  I have tried several examples and still having a time getting it to work.   I have a datagrid that is 10 by 11.  Each cell has a number (number will never change, just the color).  The number represents a particuliar alarm from a test stand.  I have to analyze the data from a data source and then set the background color. I am not binding to the data source.  I am really new to WPF and could use a complete example.  I do not have the toolkit install and I am using MS Visual C# express.  Thanks in advance.

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend