Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey all,

In my WPF application I have a datagrid and now I want to get the value of a cell.
I have tried dozens of possible solutions but none of them are working.

Can you guys help me out?

Greetz, A
Posted

Try following helper function

C#
public static DataGridCell GetCell(DataGrid dataGrid, int row, int column)
        {
            DataGridRow rowContainer = GetRow(dataGrid, row);
            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<datagridcellspresenter>(rowContainer);

                // try to get the cell but it may possibly be virtualized
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                if (cell == null)
                {
                    // now try to bring into view and retreive the cell
                    dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);

                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                }

                return cell;
            }

            return null;
}</datagridcellspresenter>


You can search more from Vincent SDibal's blog
 
Share this answer
 
Hi,
I've been struggling on this one too.

You can try this (it works for me):

Dim drv As System.Data.DataRowView = CType(Me.myDataGrid.SelectedItem, System.Data.DataRowView)
MsgBox(drv.Item(0).ToString())


Works fine even with Option Strict On, hope it helps!
 
Share this answer
 
Colin Eberhardt did a nice article on the WPF datagrid WPF DataGrid Practical Examples[^] - I would certainly look at databinding before working directly with the cells.

Best regards
Espen Harlinn
 
Share this answer
 
Wow, according to http://social.msdn.microsoft.com/Forums/en/wpf/thread/74332b78-6bfd-4ac9-af85-dfd9bec87a29[^], that's not as trivial as it should be.
 
Share this answer
 
Comments
Alex Bouma 7-Jul-11 3:46am    
Im struggling with the VisualChild class where can I find that?
This discussion[^] could help you out.
 
Share this answer
 
Comments
Alex Bouma 7-Jul-11 3:53am    
Im struggling with the VisualChild class where can I find that?
Espen Harlinn 7-Jul-11 14:19pm    
My 5

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