.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

How to assign a value to wpf datagrid cell

Posted By:      Posted Date: October 24, 2010    Points: 0   Category :WPF

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

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

Dynamically Assign Datagrid Column Binding from XAML


I am trying to build a generic user control that will serve as a quick list maintenance screen.

I have a base ViewModel that has two string properties IDColumnName and DisplayColumnName and then a collection of objects to be edited.  Inherited view models will be created for each object type that needs to be maintained.  The usercontrols datacontext is set to the viewmodel.

What I want to do is have the datagrid, who's itemssource is bound to the collection of objects in the viewmodel, have one column and have that one column bound to the property specified in the DisplayColumnName property of the viewmodel, and I'd like to do it all in XAML.  I've got the datagrids rowdetailstemplate being dynamically determined based on the object type but am struggling with dynamically setting which property the one column displays.

Changing Datagrid Cell foreground colour depending on previous value



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.


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">


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");




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

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

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.

edit DataGrid cell contents


I'm attempting to create a DataGrid where the user can edit the cells directly in the grid and have these changes updated in the underlying data source and reflected in the data grid.  Unfortunately I'm having trouble achieving this.

The xaml specification of my data grid is as follows:

      ItemsSource="{Binding Path=Parameters, Mode=TwoWay}"

Detecting cell validation errors from a DataGrid

Is there a way, from the DataGrid, to work out if any of it's rows have a validation error? I know you can use the Validation.HasError property at the level of an individual cell, but it seems that information doesn't filter back up to the DataGrid so you can test a simple property on the DataGrid to see if there are any errors (i.e. if you get Validation.HasError at the DataGrid level, it's always false regardless of whether or not there are any errors).

Multiline WPF Datagrid Cell



I am binding a data table with information about a "location" andwould like to understand how I can use the values from multiple columns into a single cell. In the sample code below I would like to  achive a single multiline row for each "location" so it looks something like:

W1 1

Any help appreciated


<Window x:Class="Window1"

How to disable WPF Toolkit DataGrid cell RenderTransform on Select



I have customized wpf toolkit datagrid in my project. When selected datagrid cell are RenderTransform.ScaleX and ScaleY transformed. I want to disable this .

Any hint. I couldn't find anything googling.

Thank you in advance.


Best regards, Ana Danilovic

Linking datagrid cell value to DeleteQuery Parameter


Using VS 2008 - 2.0 - VB

I am trying to use a stored procedure for the Delete Command, I don't see how to set the parameter values to link to the grid values.

When using text command, I would just use "WHERE Row_ID = @Row_ID"  (Row_ID being the first column of the grid, and the primary key of the table.)

How do I do this using a strored procedure?

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="usp_MA_Lookup_Callbacks" SelectCommandType="StoredProcedure"
        DeleteCommandType="StoredProcedure" DeleteCommand="usp_MA_Delete_Callback">
            <asp:Parameter Name="EMPLOYEE_ID" Type="Int32" />
            <asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />

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