I'm working on small WPF app, I'm fetching data from a database and it looks like this:
public List<BillItemInSerie> GetSerialNumbers(BillItemsTemp stavka)
{
List<BillItemInSerie> serialNumbers = new List<BillItemInSerie>();
//serialNumbers = Controller.GetSerialFromDatabase(); // this is currently not working because I don't have any data in db
for(int i = 0;i<10;i++)
{
BillItemInSerie serialNumber = new BillItemInSerie();
serialNumber.ArticleId = i;
serialNumber.ExpireDate = DateTime.Now;
serialNumber.Lot = "Warehouse" + " " + i;
serialNumber.Serial = "135" + DateTime.Now.Minute.ToString() + "/x";
serialNumbers.Add(serialNumber);
}
dtgSerialNumbers.ItemsSource = serialNumbers;
return serialNumbers;
}
As you can see I'm actually not getting it from a db because I don't have any rows in my tables, so I created by myself 10 objects to work with.
Here is my XAML:
<DataGrid Name="dtgSerialNumbers" SelectionUnit="FullRow" EnableColumnVirtualization = "True" EnableRowVirtualization ="True" MaxWidth="4000" MaxHeight="2000" Background="White" Margin="5,5,5,0" AutoGenerateColumns="False" RowHeaderWidth="0" HorizontalGridLinesBrush="#0091EA" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False" RowHeight="35" Grid.ColumnSpan="2" Grid.Row="2">
<DataGrid.CellStyle>
<StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Black"/>
<Setter Property="Opacity" Value="1"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{x:Static local:Globals.dataGridfontSizeHeader}"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Height" Value="40"/>
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="LightBlue"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Serial}" Header="Serial" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="20*" />
<DataGridTextColumn Binding="{Binding Lot}" Header="Desc." Foreground="Black" FontSize="15" FontFamily="Verdana" Width="40*" />
<DataGridTextColumn Binding="{Binding ExpireDate, StringFormat ={}{0:dd.MM.yyyy HH:mm:ss}}" FontSize="15" Header="Date" FontFamily="Verdana" Foreground="Black" Width="25*" />
<DataGridTextColumn Binding="{Binding IsSelected}" Header="Select" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="15*" />
</DataGrid.Columns>
</DataGrid>
Now I'm wondering how can I select corresponding ROWS, maybe somehow generate checkboxes next to each row and select a row by that?
Thanks EDIT:
After suggestions I've changed column to < DataGridCheckBoxColumn
> instead of DataGridTextColumn
and I've wrote this:
foreach (BillItemInSerie item in dtgSerialNumbers.ItemsSource)
{
if (((CheckBox)colSelektiraj.GetCellContent(item)).IsChecked == true)
{
MessageBox.Show(item.Lot.ToString());
}
}
It's basically for each selected row get value..
I'm not sure if this is right approach but I think it works.
You could set the SelectionMode
property of the DataGrid
to Extended
and add a DataGridTemplateColumn
:
<DataGrid Name="dtgSerialNumbers" SelectionUnit="FullRow" SelectionMode="Extended" ...>
<DataGrid.Columns>
...
<DataGridTemplateColumn Header="Select">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}, Mode=TwoWay}" IsHitTestVisible="False" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Then you should be able to select several rows by pressing the CTRL and click.
please take a look on this post . in my eyes just put a checkbox inside of your datagdrid an bind the dataset ID to a command parameter like in this example
<CheckBox CommandParameter="{Binding Path=Id}"
Command="{Binding DataContext.AddRemovePresetAssignmentCommand,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}}"
Content="{Binding Path=Name}"
>
then you have all possibilities in your viewmodel
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.