Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
4.00/5 (1 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

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:

MIDL
// 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:

C++
// 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.
 
Share this answer
 
v2
Comments
Sanjay J Patolia 23-Jun-11 12:43pm    
Nice one +5
i think we can use DataGrid.SelectedCells.

Also check this link[^].
 
Share this answer
 
v4
Comments
Manish V Mahajan 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900