Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I successfully get the row selected and the cell info I want.

But it looks like this "System.Windows.Controls.DataGridCell: ABCDEFG"

I only need the ABCDEFG part to use as a string variable.

I’ve tried all the casting and conversion methods with no success…
Error 1 Cannot implicitly convert type 'System.Windows.Controls.DataGridCell' to 'string'

can anyone assist please

Extension methods used from main class:

private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          var selectedRow = dataGrid1.GetSelectedRow();
          var columnCellSC = dataGrid1.GetCell(selectedRow, 0);
            //THIS IS WHERE I NEED TO BE ABLE TO USE THE RETURNED VALUES AS STRINGS IN THE REST OF MY CODE




}

ADDITIONAL CLASS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Threading.Tasks;
using System.Data;


namespace DataGridHelpers
{
    /// <summary>
    /// Extension methods for DataGrid
    /// </summary>
    public static class DataGridHelper
    {
        /// <summary>
        /// Gets the visual child of an element
        /// </summary>
        /// <typeparam name="T">Expected type</typeparam>
        /// <param name="parent">The parent of the expected element</param>
        /// <returns>A visual child</returns>
        public static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }
        /// <summary>
        /// Gets the specified cell of the DataGrid
        /// </summary>
        /// <param name="dataGrid1">The DataGrid instance</param>
        /// <param name="row">The row of the cell</param>
        /// <param name="column">The column index of the cell</param>
        /// <returns>A cell of the DataGrid</returns>
        public static DataGridCell GetCell(this DataGrid dataGrid1, DataGridRow row, int column)
        {
            if (row != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

                if (presenter == null)
                {
                    dataGrid1.ScrollIntoView(row, dataGrid1.Columns[column]);
                    presenter = GetVisualChild<DataGridCellsPresenter>(row);
                }

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

                return cell;
            }
            return null;
        }
        /// <summary>
        /// Gets the specified cell of the DataGrid
        /// </summary>
        /// <param name="grid">The DataGrid instance</param>
        /// <param name="row">The row index of the cell</param>
        /// <param name="column">The column index of the cell</param>
        /// <returns>A cell of the DataGrid</returns>
        public static DataGridCell GetCell(this DataGrid grid, int row, int column)
        {
            DataGridRow rowContainer = grid.GetRow(row);
            return grid.GetCell(rowContainer, column);
        }
        /// <summary>
        /// Gets the specified row of the DataGrid
        /// </summary>
        /// <param name="grid">The DataGrid instance</param>
        /// <param name="index">The index of the row</param>
        /// <returns>A row of the DataGrid</returns>
        public static DataGridRow GetRow(this DataGrid grid, int index)
        {
            DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
            if (row == null)
            {
                // May be virtualized, bring into view and try again.
                grid.UpdateLayout();
                grid.ScrollIntoView(grid.Items[index]);
                row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
            }
            return row;
        }
        /// <summary>
        /// Gets the selected row of the DataGrid
        /// </summary>
        /// <param name="grid">The DataGrid instance</param>
        /// <returns></returns>
        public static DataGridRow GetSelectedRow(this DataGrid grid)
        {
            return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
        }
    }
}
Posted
Updated 10-Feb-11 20:01pm
v2
Comments
JF2015 11-Feb-11 1:45am    
Please show us your code!
shakil0304003 11-Feb-11 2:01am    
Please, share your code...

Hi, I'm working on a similar problem but I can't get the GetSelectedRow() function to return anything other than null. Can you confirm what version of .net you are using, I'm trying with wpf in .net 4.5 ?

C#
private void dgPrimary1_CopyClick(object sender, RoutedEventArgs e)
       {
           var selectedRow = dgPrimary1.GetSelectedRow();
           var columnCell = dgPrimary1.GetCell(selectedRow, 0);


       }



Regards,
Tim.
 
Share this answer
 
Try this one..
C#
var cellcontent=(TextBlock)columnCellSC.Content;
string cellvalue=cellcontent.Text;
 
Share this answer
 
Comments
CHill60 30-Mar-14 20:06pm    
3 years too late!
string myString = dataGrid1.CurrentItem.ToString();


it give row value.,

and use the below code to get cell value,.

http://www.codeproject.com/Questions/162989/How-to-cut-a-particular-part-from-a-string.aspx
 
Share this answer
 
Try following
C#
dataGridView1.SelectedRows[0].Cells[0].Value;


Thanks,
Imdadhusen
 
Share this answer
 
Comments
Unforgiv3n 11-Feb-11 2:24am    
im using a datagrid. not a datagridview
Sunasara Imdadhusen 11-Feb-11 2:31am    
Please try!!
Mr.VoGiaCu 5-Nov-15 1:28am    
datagrid don't have SelectedRows option
Let me know how to get dataGrid1_SelectionChanged event ,

In my DataGrid can't find these event


Theingi
 
Share this answer
 

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