-1

I have a WPF application I am working on. I am using a DataGrid control to display a fields list with various information. I cannot find the DatGridView control in my Visual Studio Community toolbox. Majority of my Google searches only bring back info for DataGridView, very frustrating. I would use DataGridView if I could find, but I digress. What I want to be able to do is capture the current row when a user selects a cell, so I can get a value from the adjacent cell. I am having no luck finding out how to do this. Here is how I create my DataGrid:

 private void DisplayFieldLengths(string strFLFileName, Int32 intTotalRowSize, int[] intFieldLengths)
    {
        int intDisplayCnt, colCnt = 0;
        string strData = "";


        lblFLInfo.Content = "File: " + strFLFileName;
        DataTable dt = new DataTable();
        dt.Columns.Add("Field", typeof(string));
        dt.Columns.Add("Size", typeof(string));
        dt.Columns.Add(" New Size", typeof(string));
        dt.Columns[0].ReadOnly = true;
        dt.Columns[1].ReadOnly = true;


        for (intDisplayCnt = 0; intDisplayCnt < intFieldLengths.Length; intDisplayCnt++)
        {
            strData = "Field" + (intDisplayCnt + 1) + "|" + intFieldLengths[intDisplayCnt].ToString() + "|1";
            dt.Rows.Add(strData.Split('|'));
        }

        dtGrid.ItemsSource = dt.DefaultView;
        lblRowSize.Content = "Total row length: " + intTotalRowSize;

    }

Here is the xaml for the DataGrid.

<DataGrid IsReadOnly="False" Name="dtGrid" Loaded="GridLoaded" CurrentCellChanged="NewFieldSizeChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Height="365" Margin="48,53,0,0" Width="272" BorderThickness="1" BorderBrush="Black">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                    <Trigger Property="DataGridCell.IsSelected" Value="True">
                        <Setter Property="Background" Value="#FF9DF3D6" />
                        <Setter Property="Foreground" Value="#000000" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>
    </DataGrid>
Cass
  • 537
  • 1
  • 7
  • 24
  • 6
    This is already answered here: http://stackoverflow.com/questions/3913580/get-selected-row-item-in-datagrid-wpf – Kishan Shah Jul 19 '16 at 13:31

1 Answers1

0

It looks like you're using events and code-behind to implement what you want to achieve. I suggest you embrace the MVVM paradigm and implement data binding. Your code will no longer be in events and code-behind (ie not in the View) but in the view model. For example, to track row changes you bind the SelectedItem property to a property in your view model. Responding to row changes then becomes as trivial as putting code in the view model property's setter. This topic is too long to put as an SO answer. Best of luck.

jp2g
  • 682
  • 4
  • 8
  • #jp2g - I would love to learn Binding, just not sure how it works. Can you point me to an example that's geared towards what I am trying? I saw this example: Customer customer = (Customer)myDataGrid.SelectedItem; But I don't know how to access the code when I select a row in my datagrid. This came from http://stackoverflow.com/questions/3913580/get-selected-row-item-in-datagrid-wpf. Thank you – Cass Jul 19 '16 at 16:58
  • @Cass: have a look at this [example](http://www.codeproject.com/Articles/332615/WPF-Master-Details-MVVM-Application). As explained in the SO post you linked to, your view model needs to have a read/write property, say 'SelectedCustomer'. In that property's setter you write the code to respond to row changes. It needs to be in the setter because the WPF binding plumbing will set that property for you whenever the DataGrid's selected item changes. – jp2g Jul 19 '16 at 21:13