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

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

KeyUp event on DataGrid cell or column

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



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




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

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

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,


Cell value changed event(?) in DataGrid


What would be a good way to capture a change in a cell value and act on it (e.g. flash the background)?

The only thing that I could come up with was using Binding.TargetUpdated.  It works fine with the exception that when you scroll a virtualized grid TargetUpdated is obviously also fired. There has to be a better way to do something simple like this ...

Multiple Column Dropdownlist for the ASP.NET DataGrid

Based on my previous control "Multiple Column DropDownList for ASP.NET", I received many emails asking for the same control to be used in the DataGrid for web applications. Here we go.. This control can be used as the regular MS DropDownList in the DataGrid and also as a regular dropdownlist. It has all the properties, like DataTextField, DataValueField, DataSource, SelectedIndex etc. The download file contains the samples both in VB.NET and C#. In this sample, I have used the Northwind database of SQL Server.

DataGrid: Tailor Your DataGrid Apps Using Table Style and Custom Column Style Objects


One of the most enduring challenges in writing user interfaces is figuring out how to display large amounts of data efficiently and intuitively without bewildering the user. The problem becomes particularly thorny when the interface must reflect hierarchical relationships within the data that the user needs to modify. The Windows Forms DataGrid control gives developers a powerful and flexible tool to meet this challenge. This article explains its basic operations and shows how to extend the DataGrid to display columns of data in an application-appropriate manner.

Kristy Saunders

MSDN Magazine August 2003

WPF Datagrid Combobox column

Hi, I am creating a WPF Datagrid with the combobox column in it. All the datagrid binding and combobox column binding works fine without any problem. But, when I select a value in the combobox column and move to the next column or row, the value is reset to the previous empty selection. Am I missing anything here? Please find the code snippet that I have used below. <dg:DataGridComboBoxColumn x:Name="technicianname" Header="Assigned To" ItemsSource="{Binding}"/> technicianname.ItemsSource = dtTechnicianInfo.DefaultView; technicianname.DisplayMemberPath = "TechnicianName"; technicianname.SelectedValuePath = "TechnicianEid"; Thanks in Advance!!! Best Regards, Subalakshmi Vijayarajan.

How to get the combobox value inside Datagrid on Edit button click event

Hi     I have Datagrid template column like<DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn>DataGridTemplateColumn Header="Edit" x:Name="Edit" ><DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn.CellEditingTemplate></DataGridTemplateColumn>SelectionChanged="cmbDGTeam_SelectionChanged" IsTabStop="True" /></DataTemplate><ComboBox Height="22" IsEditable="True" IsReadOnly="True" ItemsSource="{Binding}" DisplayMemberPath="Title" SelectedValuePath="TeamID" x:Name="cmbDGTeam"  SelectedValue="{Binding Path=TeamID,Mode=TwoWay}"<DataGridTemplateColumn.CellEditingTemplate><DataTemplate><DataTemplate><TextBlock Text="{Binding Title}"/></DataTemplate></DataGridTemplateColumn.CellTemplate><DataGridTemplateColumn Header="Team" Width="75"><DataGridTemplateColumn.CellTemplate> and Edit button Template column <DataGridTemplateColumn Header="Edit" x:Name="Edit" > </DataGridTemplateColumn.CellTemplate></DataTemplate><Button Content="Edit" IsEnabled="False" x:Name="btnEdit" Click="btnEdit_Click" ></Button>&l

How to scroll the DataGrid (horizontally) when user drags a column header far left or right?

We're working with the WPF DataGrid and I have a question about  dragging column headers: The grid lets the user rearrange the columns, by dragging the column header left or right.  So far, so good. But, sometimes we have many columns, and only the first few columns are visible; so there is a horizontal scrollbar.  In such case, if the user drags a column header to the right edge of the DataGrid window, I want the DataGrid to scroll, automatically, one column at a time.  Is there an easy way to do that? (To be clear: yes, the user can already scroll horizontally by interacting with the horizontal scrollbar, but that isn't what I'm asking about.  I'm asking if the "drag a column header left or right" gesture can cause the DataGrid to scroll left or right.) I guess I could handle various low-level events, but it seems like it shouldn't be necessary -- DataGrid has so much functionality built-in, that I'm guessing it already has the ability to scroll when the user drags the column header to the edge.  So, is there either (a) a property that I can set, to tell the DataGrid to scroll when the user drags a column header to the edge of the window;  or (b) a code example showing how to achieve this effect myself?  Thanks!

WPF Datagrid with multiple row selection/checkbox column template

Hello, I am using the WPF Toolkit Datagrid with a checkbox defined in a template column/template cell. We are using the checkbox template so that selecting the checkbox will also highlight the row in the datagrid. Here is the template definition: <dg:DataGridTemplateColumn MinWidth="50" Width="Auto" Header="" CanUserReorder="False"> <dg:DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding Path=IsCheckedm, Mode=TwoWay}" /> </DataTemplate> </dg:DataGridTemplateColumn.CellTemplate> </dg:DataGridTemplateColumn> We are allowing multiple row selection in the datagrid and using MVVM (using MVVM Light). We would like to keep a list in the View Model of selected rows as they are selected/unselected. Any suggestions on how we can leverage MVVM to keep the list of selected/unselected elements current. Many Thanks --Sam

can do rowspan+columnspan in DataGrid Column Headers?

i wanna do something like this: 4 column headers: Header Font, Header Size, Cell Font, Cell Size you can see they are 2 grps, with a pair of each. now i wanna create the column headers like:     Header  |     Cell Font | Size  | Font | Size is it possible? and how to do that?

Windows Keyup event is routed to next form even if the current form is closed

In a form (say FormA) i have 2 events "Keyup" for the form and button_click event for the button. I call FormA from the main form with a showDialog, after this i call another form (say FormB), it also has the 2 events. In both the form the button_click event has the logic to close the form (this.close()). Now when the FormA is opened the focus is on button, if the user presses "enter" button the button_click event is called which closes the form, the logic returns to the main form and the next line i.e. to open the FormB is executed. Now here is the wierd part, in FormB because there is a KeyUp event handler; just after instantiation of the form directly this event handler is called. I guess this is being routed from FormA. Shouldnt the FormA's keyup event be closed as soon as the form is closed?

Accessing Combobox inside WPF Datagrid Template Column

Hi, I am creating a WPF datagrid template column with combobox. The problem that I face now is, I am not able to bind a table to the combobox inside the WPF datagrid template column. Here is the code snippet. <dg:DataGrid x:Name="Students" AutoGenerateColumns="False" Loaded="OnLoad"> <dg:DataGrid.Columns> <dg:DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="75"/> <dg:DataGridTemplateColumn x:Name="Dept"> <dg:DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox x:Name="DeptId" ItemsSource="{Binding}" SelectionChanged="Dept_SelectionChanged" DisplayMemberPath="{Binding Path=DeptName}" SelectedValuePath="{Binding Path=DeptId}" SelectedItem="{Binding Path=Belongs}"/> </DataTemplate> </dg:DataGridTemplateColumn.CellTemplate> </dg:DataGridTemplateColumn> </dg:DataGrid.Columns> </dg:DataGrid> Please help me to find out the way to assign a dynamic source to the combobox through code behind file. Thanks in Advance!!! Best Regards, Subalakshmi Vijayarajan.

How to make a DataGridView Cell/Column not enterable / selecatble

Hi, I am trying to make a Column in a DataGridView non-entaerable (the user should not be able to set focus to the cell neither using the mouse nor the keyboard). I already tried to change the CurrentCell property in the CellStateChanged. But in this event-handler the CurrenCell is the "old" one; setting it to any value doesn't help, because the value is set after calling this procedure. In Delphi there is an event-handler for entering a cell, where you can tell whether the user is allowed to enter a cell. Is there anything comparable in C#? Any other suggestions?

DataGrid Button + event in Footer, event not getting fire!

hi.First Time i am trying this...I have one Datagrid with Footer. Footer Row Contains Input fields with one Button to add New Values. I have button click event but not getting fires. my code follows:protected void dgPrice_ItemCreated(object sender, DataGridItemEventArgs e)    {            Button btnAdd = new Button();            btnAdd.Text = "Add";            btnAdd.Width = 80;            btnAdd.ID = "btnAdd";           // btnAdd.Attributes.Add("onClick", "return validateRates()");            btnAdd.CommandName = "add";            btnAdd.CommandArgument = e.Item.ItemIndex.ToString();            btnAdd.Command +=new CommandEventHandler (PriceNew);            e.Item.Cells[6].Controls.Add(btnAdd);}protected void PriceNew(object sender, EventArgs e)    {}btnAdd.command/ btnAdd.click event not getting fired I also tried to

WPF DataGrid Column Width issue

I have a simple window with 2 tabs, and 4 datagrids.  I set up each one with similar, if not identical settings.  They all get their own DataSet based binding, and have standard Text columns, and combo box columns.  Recently (past week) of development, while I was adding filtering/sorting/grouping features to my program, the column widths have begun to ingore my settings.  By collapsed I mean approx 20 pixels wide (just enough for the sorting Icon). Contacts datagrid, works great in run time, but in design time the columns are all collapsed. ContactsToClients data grid has no abnormal issues Clients datagrid AND Clients to Contacts datagrid look great in design time, but show collapsed columns in run time. How do I hunt down the problem?  I've tried removing all the temporary code, and rebuilding, closing Visual Studio and reopening.  I've remmed out the sorting code.  None of these have any affect.  It appears that my settings are all being ignored.  Here is the XAML code:   <Window x:Class="wdContacts" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:my
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