Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi,
 
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.
 
Thanks,
 
Manish
Posted 22-Jun-11 12:55pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Was to find a solution posted on MSDN ( href="http://social.msdn.microsoft.com/Forums/en/wpf/thread/7fd1df22-03b9-408b-80a4-e0a642a3a857">).
 
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
<UserControl.Resources> 
<ResourceDictionary>
<CollectionViewSource x:Key="TestDataSource"/>
</UserControl.Resources&;gt; 
</ResourceDictionary>
 
// 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("Empty");
        return;
    }
    else
    {
        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));
 
    }
}</testdata>
 
Hope this help you all....wored for me.
  Permalink  
v2
Comments
DynamicIn at 23-Jun-11 12:43pm
   
Nice one +5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

i think we can use DataGrid.SelectedCells.
 
Also check this link[^].
  Permalink  
v4
Comments
Member 8008832 at 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
0 OriginalGriff 359
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 185
4 PIEBALDconsult 149
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 23 Jun 2011
Copyright © CodeProject, 1999-2014
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