Home > front end >  WPF DataGrid get array values column column
WPF DataGrid get array values column column

Time:04-01

Please tell me how to get all the value of a column into an array.

<DataGrid x:Name="dgTodoList" AutoGenerateColumns="False" CanUserSortColumns="True">
     <DataGrid.Columns>
      <DataGridTemplateColumn Header="Date">
       <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
         <xctk:DateTimePicker Value="{Binding Path = ReminderDate}" IsReadOnly="False" CultureInfo="ru-RU" Format="Custom" FormatString="dd.MM.yyyy HH:mm:ss tt"  Width="150" TimeFormat="LongTime" />
        </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
     </DataGrid.Columns>
    </DataGrid>

I tried to do so

foreach (DataRowView rowView in dgTodoList.Rows)
{
 DataRow row = rowView.Row;
 MessageBox.Show(row);
}

CodePudding user response:

Bind the DataGrid's ItemsSource to a collection and that collection will have the view-models for all your rows in it, where you can iterate over them and do whatever you want with their ReminderDate. It is much harder to try and get them from the code behind by accessing the DataGrid directly.

CodePudding user response:

Okay. The code below turned out to take the date and time. Tell me more please:

  1. How to take the closest date and time to the current time in this column.
  2. How to correctly convert the date and time to a string (I plan to compare it with the current one later).
    var myDataObject = _todoDataList;

    Binding reminderDateBind = new Binding("ReminderDate");

    reminderDateBind.Source = myDataObject;

    textBind.Source = myDataObject;
  • Related