Tharindu
Tharindu

Reputation: 81

Read each cell values in WPF gridview

I have a datagridview in wpf called datagrid1. I need to read value in each cells in datagrid. I know how to do it in windows form

string result = datagrid1.Rows[0].Cells[1].Value.ToString();

How do this in WPF?

Upvotes: 2

Views: 2246

Answers (1)

Bahman_Aries
Bahman_Aries

Reputation: 4798

There is no easy way to do this in WPF, however this tutorial can be of use to you.

Edit:

First of all I totally agree with the comment Nitin Joshi mentioned above. Second, According to this answer The WPF datagrid was built to bind to something like a DataTable. The majority of the time, you will modify the DataTable, and the Rows/Columns within the DataTable that is bound to the DataGrid, not the DataGrid it self so you don't need to use something like this datagrid1.Rows[0].Cells[1].Value. But if you still insist on getting the value that way, here is a solution :

Second EDIT: Since you asked only for a way to read call value, I'll make my answer shorter but also a little more concrete: GetCellValue method returns a string value representing cell content of a given DataGrid by column/row indices:

I wrote this method assuming column types are either TextBox, TextBlock or ComboBox. Other Types could be handled the same way.

    public string GetCellValue(DataGrid datagrid, int row, int column)
    {
        var cellInfo = new DataGridCellInfo(
            datagrid.Items[row], dataGrid.Columns[column]);

        DataGridCell cell = null;
        var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
        if (cellContent != null)
            cell = (DataGridCell)cellContent.Parent;

        if (cell == null) return string.Empty;

        // if DataGridTextColumn / DataGridComboBoxColumn is used 
        // or AutoGeneratedColumns is True
        if (cell.Content is TextBlock)
            return ((TextBlock)cell.Content).Text;
        else if (cell.Content is ComboBox)
            return ((ComboBox)cell.Content).Text;

        // if DataGridTemplateColumn is used 
        // assuming cells are either TextBox, TextBlock or ComboBox. Other Types could be handled the same way.
        else
        {
            var txtPresenter = FindVisualChild<TextBox>((ContentPresenter)cell.Content);
            if (txtPresenter != null) return txtPresenter.Text;
            var txbPresenter = FindVisualChild<TextBlock>((ContentPresenter)cell.Content);
            if (txbPresenter != null) return txbPresenter.Text;
            var cmbPresenter = FindVisualChild<ComboBox>((ContentPresenter)cell.Content);
            if (cmbPresenter != null) return cmbPresenter.Text;
        }
        return string.Empty;
    }

    public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is T)
                return (T)child;
            else
            {
                T childOfChild = FindVisualChild<T>(child);
                if (childOfChild != null)
                    return childOfChild;
            }
        }
        return null;
    }

Then calling string result = GetCellValue(dataGrid, 2, 1); (e.g. from a Button click event), will return the value of dataGrid[2,1].

*Note:

  1. The SelectionUnit of the DataGrid must be set to Cell.
  2. The DataGridmust fully generated otherwise ItemContainerGenerator returns null.
  3. GetCellValue method works for a few UIElements considered to be more common to be used as DataGridColumn Types.

Upvotes: 1

Related Questions