Click here to Skip to main content
12,400,183 members (46,900 online)
Rate this:
Please Sign up or sign in to vote.

I am new to C# and WPF and needs some help.
I have a DataGrid for which I have 3 columns (A, B, C).

Column A = DataGridTextColumn (Binding = NAME)
Column B = DataGridTemplateColumn (Binding = Value1)
Column C = DataGridTemplateColumn (Binding = Value2)

The DataGrid is populated by a CollectionViewSource when initially loaded.

The data for the Grid for example is as follows

Col A, Col B, Col C
Sec1 , 25.4 , 36. 3
Sec2 , 12.4 , 6. 3
Sec3 , 50.7 , 3. 8

I need to achieve the following;

1. When I double click on Col B, Row 0 (25.4), I need to get some historical data for Sec1 for Col B.
2. When I double click on Col B, Row 1 (12.4), I need to get some historical data for Sec2 for Col B.
(n) When I click on Col C, Row 2 (3.8), I need to get some historical data for Sec3, Col C.

..Therefore, I believe I need to do the following:
1. Create a MouseEvent or DoubleClick event when I click in the DataGridTemplateColumns for Col B and Col C.
2. In my c# code, I need to be able to determine that the MouseEvent/Click came for the respective cell, so I can then look for the historical data based on the Col/Row where the mouse was double clicked (I need to know value in Col A (Sec1...Sec3) and the Col where the double click was initiated.
I hope the above makes sense, given the scenario stated.

Would appreciate any help on how I can achieve this. I'm thinking via a Style declaration for the respective DataGridTemplateColumn, but not sure......was not able to find much on this when searched via google or bing.


Posted 22-Jun-11 11:55am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Was to find a solution posted on MSDN ( href="">).

Basically I did the following:

1. Add a MouseDoubleClick in my main DataGrid (this works even though you have DataGridTextColumn or DataGridTemplateColumns as long as your CollectionViewSource is defined and used in the DataGrid).

// Define this in the XAML
<CollectionViewSource x:Key="TestDataSource"/>

// In your DataGrid Declaration
<DataGrid Name="DG"
ItemsSource="{Binding Source={StaticResource TestDataSource}}" MouseDoubleClick="CHART_DISPLAY">

Note: TestDataSource was declared and populated in the c# code using the following:

// Get a handle on the Data Source in the WPF
CollectionViewSource dataViewSource = (CollectionViewSource)FindResource("TestDataSource");
//Populate the WPF Data
dataViewSource.Source = dataList;

// In your C# code:
2. Define the following method in my mainview/window:

// Clicking on the CHART_DISPLAY to generate the on-the-run graphs
private void CHART_DISPLAY(object sender, MouseButtonEventArgs e)
    if (DG.SelectedItem == null)
        Debug.WriteLine("Not Empty");
        TestData selectedRow = DG.SelectedItem as TestData;
        // Here, you can access any data item / member in the List<testdata> Object
        Debug.WriteLine(string.Format("Data = Name={0}, Spread={1}",selectedRow.NAME, selectedRow.SPREAD));

Hope this help you all....wored for me.
DynamicIn 23-Jun-11 12:43pm
Nice one +5
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

i think we can use DataGrid.SelectedCells.

Also check this link[^].
Member 8008832 23-Jun-11 10:41am
Thanks, but it would help if you posted a very simple solution using examples in both XAML and the corresponding C# code.....I'm relatively new (only 3 weeks) to C# and WPF so it helps tremendously when people respond by posting simple examples with explanations....see my posted solution below.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 23 Jun 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100