Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting datagridrow from a datarowview WPF

Tags:

c#

wpf

datagrid

I am trying to loop through each row in a datagrid, pull out a column value, pass this value to a method and style that row based on the result of the method.

After finding out I cant just loop through the rows of a datagrid I found this post detailing how it was possible.

I have modified slightly so that I am working with datarowview objects.

The issue I now have is that

var dgRow = grid.ItemContainerGenerator.ContainerFromItem(r) as DataGridRow;

is always returning null.

Please could someone advise as to why this is happening in my case and if they can see an easier way of doing it.

Please let me know if you need anymore info.

Heres my code:

private void colorArchived( DataGrid grid , GX3MaterialSelectionData data)
    {
        var row = GetDataGridRows(grid);
        foreach (DataRowView r in row)
        {
            var dgRow = grid.ItemContainerGenerator.ContainerFromItem(r) as DataGridRow;
            int val = int.Parse(r.Row[0].ToString());
            if ( data.IsArchived(val) )
            {
                // style will be defined in xaml
                dgRow.Style = mystyle;
            }


        }

    }

    public IEnumerable<DataRowView> GetDataGridRows(DataGrid grid)
    {
        var itemsSource = grid.ItemsSource as IEnumerable;
        if (null == itemsSource) yield return null;
        foreach (var item in itemsSource)
        {
            var row = item;
            if (null != row) yield return (DataRowView)row;
        }
    }
like image 605
user589195 Avatar asked Nov 12 '22 17:11

user589195


2 Answers

As per your Question, i have just updated the StyleSelector Class described above:

public class RowStyle : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        var dgRow = item as DataGridRow;
        int val = int.Parse(dgRow.Row[0].ToString());
        if ( data.IsArchived(val) )
        {
            return Mystyle;
        }
        return base.SelectStyle(item, container);
    }

    // style will be defined in xaml
    public Style Mystyle
    {
        get;
        set;
    }
}

NOTE: Describe "GX3MaterialSelectionData data" as Static for the Class, so that the above class could access it directly.

like image 196
D_Learning Avatar answered Nov 15 '22 13:11

D_Learning


METHOD 1:

//simple way using SelectionChanged or RowEditEnding event without DataRowView
        DataRowView row = (DataRowView)t_DataGrid.SelectedItems[0];

        DataGridRow row1 = e.Row;

METHOD 2:

    //convert  datagridrow to datarowview (it works sometimes only databinding problems)
    DataGridRow dgr = FindVisualParent<DataGridRow>(txtPcode);
    DataRowView drv = dgr.DataContext as DataRowView;

METHOD 3:

//it needs improvements
DataRowView dataRow = (DataRowView)t_DataGrid.SelectedItem;
//subitem
int i_cell = t_DataGrid.CurrentCell.Column.DisplayIndex;
int i= int.Parse(dataRow.Row.ItemArray[id_cell].ToString());
//get datagrid by index
DataGridRow dgr = DG.GetRow(t_DataGrid_temp, i);

//create this special Datagrid class for getting full access over datagrid
public static class DG
{
    /* Get the Cell using Row Index and Column Index */
    public static DataGridCell GetCell(DataGrid grid, int row, int column)
    {
        DataGridRow rowContainer = GetRow(grid, row);
        return GetCell(grid, rowContainer, column);
    }

    /* Get the DataGridRow using Row Index */
    public static DataGridRow GetRow(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;
    }

    /* Get the Cell using DataGridRow Object and Column Index */
    public static DataGridCell GetCell(DataGrid grid, DataGridRow row, int column)
    {
        if (row != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

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

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

    /* Get the Child Element Based on Type */
    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;
    }

    /* Get the Child by name */
    public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
    {
        if (parent == null) return null;

        T foundChild = null;

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            T childType = child as T;
            if (childType == null)
            {
                foundChild = FindChild<T>(child, childName);
                if (foundChild != null) break;
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    foundChild = (T)child;
                    break;
                }
            }
            else
            {
                foundChild = (T)child;
                break;
            }
        }
        return foundChild;
    }
    /*get parent*/
    public static T GetParent<T>(DependencyObject d) where T : class
    {
        while (d != null && !(d is T))
        {
            d = VisualTreeHelper.GetParent(d);
        }
        return d as T;
    }

    //new
    public static Visual GetChildrenByType(Visual visualElement, Type typeElement, string nameElement)
    {
        if (visualElement == null) return null;
        if (visualElement.GetType() == typeElement)
        {
            FrameworkElement fe = visualElement as FrameworkElement;
            if (fe != null)
            {
                if (fe.Name == nameElement)
                {
                    return fe;
                }
            }
        }
        Visual foundElement = null;
        if (visualElement is FrameworkElement)
            (visualElement as FrameworkElement).ApplyTemplate();
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visualElement); i++)
        {
            Visual visual = VisualTreeHelper.GetChild(visualElement, i) as Visual;
            foundElement = GetChildrenByType(visual, typeElement, nameElement);
            if (foundElement != null)
                break;
        }
        return foundElement;
    }
}
like image 33
Prashant Manjule Avatar answered Nov 15 '22 12:11

Prashant Manjule