user2990406
user2990406

Reputation: 109

Clearing data from rows in Datagrid

I am working on some old C# code that utilizes a Datagrid (not a DataGridView). Without changing everything, I just want to be able to clear the data from an entire row of the Datagrid.

I set the ItemsSource like so:

this.dg.ItemsSource = Data;

Data is defined like so:

public ObservableCollection<DataCollector> Data
    { 
        get { return _Data; } 
    }

and then the code uses some recursive methods like FindChild to populate it like this:

TextBlock tb;
Binding b = new Binding("val1");
b.Mode = BindingMode.OneWay;
DGTC1.Header = "A";
DGTC1.Binding = b;
tb = FindChild<TextBlock>(dg, "C1TB");
if (tb != null)
    tb.Text = "A";



public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
    {
        T foundChild = FindChildRecusive<T>(parent, childName);
        if (foundChild == null)
        {
            Log.logItem(LogType.Error, "ViewRunData", MethodBase.GetCurrentMethod().Name,
                "Did not find child: " + childName);
        }
        return foundChild;
    }

    public static T FindChildRecusive<T>(DependencyObject parent, string childName) where T : DependencyObject
    {
        if (parent == null)
        {
            Log.logItem(LogType.Error, "ViewRunData", MethodBase.GetCurrentMethod().Name,
                "Did not find parent for child: " + childName);
            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 = FindChildRecusive<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 = FindChildRecusive<T>(child, childName);
                        if (foundChild != null)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    foundChild = (T)child;
                    break;
                }
            }
        }
        return foundChild;
  }

Here's a snippit of the xaml file

<Custom:DataGrid x:Name="dg" Visibility="Hidden" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" Height="176" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" Width="785" Canvas.Top="18" AutoGenerateColumns="False" ItemsSource="{Binding Data}" > <Custom:DataGrid.Columns> <Custom:DataGridTextColumn x:Name="sampleIdColumn" Header="Sample ID" CanUserReorder="False" CanUserSort="False" Width="360" Binding="{Binding Suspect_ID, Mode=OneWay}" FontSize="12"> <Custom:DataGridTextColumn.HeaderTemplate> <HierarchicalDataTemplate> <TextBlock Text="Sample ID" HorizontalAlignment="Center"> <TextBlock.LayoutTransform> <RotateTransform Angle="270"/> </TextBlock.LayoutTransform> </TextBlock> </HierarchicalDataTemplate> </Custom:DataGridTextColumn.HeaderTemplate> </Custom:DataGridTextColumn> <Custom:DataGridTextColumn x:Name="DGTC1" Width="35" Binding="{Binding D3S1358Val1, Mode=OneWay}" CanUserReorder="False" CanUserSort="False"> <Custom:DataGridTextColumn.HeaderTemplate> <HierarchicalDataTemplate> <TextBlock x:Name="C1TB" Text=""> <TextBlock.LayoutTransform> <RotateTransform Angle="270"/> </TextBlock.LayoutTransform> </TextBlock> </HierarchicalDataTemplate> </Custom:DataGridTextColumn.HeaderTemplate> </Custom:DataGridTextColumn> <Custom:DataGridTextColumn x:Name="DGTC2" Width="35" Binding="{Binding D3S1358Val2, Mode=OneWay}" CanUserReorder="False" CanUserSort="False"> <Custom:DataGridTextColumn.HeaderTemplate> <HierarchicalDataTemplate> <TextBlock Name="C2TB" Text=""> <TextBlock.LayoutTransform> <RotateTransform Angle="270"/> </TextBlock.LayoutTransform> </TextBlock> </HierarchicalDataTemplate> </Custom:DataGridTextColumn.HeaderTemplate> </Custom:DataGridTextColumn>

How can I clear data from a row after setting the ItemsSource? I looked at the CurrentItem method but can't figure out how I would implement it.

Upvotes: 0

Views: 67

Answers (1)

mm8
mm8

Reputation: 169390

You clear a row by either removing the corresponding Data item from the ObservableCollection<Data>:

Data.RemoveAt(0); // 0 is the index of the first row

...or by setting all properties of the corresponding Data object to some blank values:

var row = Data[0];
row.D3S1358Val1 = "";
//...

For the latter to work, the Data class must implement the INotifyPropertyChanged interface and raise change notifications: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

Upvotes: 1

Related Questions