User can not insert into datagrid? - c#

I want to use a three-column datagrid so that the user can enter the data into it and then store the data from the datagrid into the database.
But my problem is that I created DataGrid but I can't enter any data.
<DataGrid VerticalAlignment="Center" VerticalContentAlignment="Center"
HorizontalContentAlignment="Center" HorizontalAlignment="Center"
IsEnabled="{Binding ElementName=chkitems, Path=IsChecked}"
Height="266" x:Name="dgvitems" AutoGenerateColumns="False"
CanUserAddRows="True" CanUserDeleteRows="True"
CanUserReorderColumns="False" CanUserResizeColumns="False"
CanUserSortColumns="False" IsReadOnly="False" Width="451">
<DataGrid.Columns>
<DataGridTextColumn Header="نام کالا" Width="250" FontSize="14" Binding="{Binding name}"/>
<DataGridTextColumn Header="تعداد" Width="80" FontSize="14" Binding="{Binding number}"/>
<DataGridTemplateColumn Header=" ">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btndelete" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="{x:Null}" Click="Btndelete_Click" ToolTip="حذف رکورد" Background="{x:Null}">
<materialDesign:PackIcon Kind="CloseCircle" Foreground="Red" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

WPF not Support Direct Binding to DataColumns first Learn MVVM Architecture And Used MVVM
pattern ,used ObservableCollection then enter data Datagrid Columns Accept Automatically

Related

Bind to an ObservableCollection which is inside another ObservableCollection

]3
So i have an ObservableCollection with movies in my ViewModel, and inside that ObservableCollection there's another ObservableCollection with all the actors that participate in the movie. I wanted to show that Actors ObservableCollection in my DataGrid, but i don't know how to do it, do you have any idea? Thanks
<controls:DataGrid
Grid.Row="1"
x:Name="viewFilmes"
Margin="12"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"
AlternatingRowBackground="Transparent"
AlternatingRowForeground="Gray"
AreRowDetailsFrozen="False"
AreRowGroupHeadersFrozen="True"
AutoGenerateColumns="False"
CanUserSortColumns="False"
CanUserReorderColumns="True"
CanUserResizeColumns="False"
ColumnHeaderHeight="32"
MaxColumnWidth="400"
FrozenColumnCount="0"
GridLinesVisibility="None"
HeadersVisibility="Column"
RowDetailsVisibilityMode="Collapsed"
SelectionChanged="viewFilmes_SelectionChanged"
SelectionMode="Single"
RowEditEnded="viewFilmes_RowEditEnded"
RowGroupHeaderPropertyNameAlternative="Range"
ItemsSource="{x:Bind GestaoDeFilmesViewModel.Filmes}">
<controls:DataGrid.Columns>
<controls:DataGridTextColumn Header="Id" IsReadOnly="True" Binding="{Binding Idfilme, Mode=OneWay}" Tag="Id"/>
<controls:DataGridTextColumn Header="Nome" Binding="{Binding Nome, Mode=TwoWay}" Tag="Nome" />
<controls:DataGridTextColumn Header="Ano" Binding="{Binding Ano, Mode=TwoWay}" Tag="Ano" />
<controls:DataGridTextColumn Header="Duracao" Binding="{Binding Duracao, Mode=TwoWay}" Tag="Duracao" />
<controls:DataGridTemplateColumn Header="Foto" Tag="Foto" >
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<control:FotoControl ImageSource="{Binding Foto, Mode=TwoWay}"></control:FotoControl>
<Button Click="Foto_Button_Click">Alterar Foto</Button>
</StackPanel>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTextColumn Header="Genero" Tag="Genero"/>
<controls:DataGridTextColumn Header="Diretor" Binding="{Binding Diretor.Nome, Mode=TwoWay}" Tag="Diretor"/>
<controls:DataGridTextColumn Header="Escritor" Binding="{Binding Escritor.Nome, Mode=TwoWay}" Tag="Escritor"/>
<controls:DataGridTemplateColumn Header="Elenco" Tag="Elenco">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Ver Elenco">
<Button.Flyout>
<Flyout>
<StackPanel>
<TextBlock Style="{ThemeResource BaseTextBlockStyle}" Text="Elenco" Margin="0,0,0,12" />
<!--<TextBox Text="{Binding Atores.Idator, Mode=TwoWay}"/>-->
</StackPanel>
</Flyout>
</Button.Flyout>
</Button>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
</controls:DataGrid.Columns>
</controls:DataGrid>
If you plan to display collection content, UWP provides GridView and ListView controls.
like this:
xaml.cs
public ObservableCollection<string> TestCollection {get; set;}
xaml
<GridView ItemsSource="{x:Bind TestCollection}"/>
If you plan to display a ListView or GridView in the DataGrid, you can try to put the control in RowDetail. like this:
<controls:DataGrid ...>
...
<controls:DataGrid.RowDetailsTemplate>
<DataTemplate>
<Grid>
<GridView ItemsSource="{Binding Atores}">
</GridView>
</Grid>
</DataTemplate>
</controls:DataGrid.RowDetailsTemplate>
...
</controls:DataGrid>
Here are some documents that might help you:
ListView and GridView
How to: Display and Configure Row Details in the DataGrid Control
Best regards.

C# WPF DataGrid virtualization is slow

I have a DataGrid with 10-15 columns which can have around 100-200 rows. The datagrid is placed within a Tab control ( which is not in focus by default ).
I tried to virtualize the DataGrid but when I click the tab containing the DataGrid, the program freezes for like 4-5 seconds, and then the tab opens displaying the datagrid. After that the rows seem to scroll fast which is good, but the columns still behave slow like un-virtualized.
When I remove the code to virtualize (last 4 options in DataGrid tag), the grid displays immediately but scrolls very slow and lags.
The following is my datagrid code:
<DataGrid Name="xDataGridFieldConfig"
FrozenColumnCount ="1"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalAlignment="Stretch"
Style="{DynamicResource FieldConfigDataGridHeaderStyle}"
AutoGenerateColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserReorderColumns="False"
SelectionMode="Single"
GridLinesVisibility="Horizontal"
HorizontalGridLinesBrush="#cbcaca"
HeadersVisibility="Column" ItemsSource="{Binding FieldConfigCollection}"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Visible"
VirtualizingPanel.IsVirtualizingWhenGrouping="True"
VirtualizingPanel.VirtualizationMode="Standard"
VirtualizingPanel.IsVirtualizing="True"
ScrollViewer.CanContentScroll="False">
<DataGrid.Columns>
<DataGridTextColumn Header="S No." Binding="{Binding Path=ID}" IsReadOnly="True" Width="80"/>
<!-- using template for custom checkbox -->
<DataGridTemplateColumn Header="EN" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<flatcheckbox:FlatCheckBox x:Name="xFlatCheckBoxFieldConfigEN" Margin="0" IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<flatcheckbox:FlatCheckBox x:Name="xFlatCheckBoxFieldConfigEN" Margin="0" IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Group" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Name="xTextBlockFieldConfigGroup" Text="{Binding Path=Group, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Name="xTextBlockFieldConfigGroup" IsEnabled="{Binding Enabled}" Text="{Binding Path=Group, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Padding="0" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<!-- 10 similar text-only editable rows -->
</DataGrid.Columns>
</DataGrid>
Is there anything wrong I'm doing ? How to make the datagrid to be displayed immediately as soon as I open the tab.

Showing images with data in datagridview wpf

Can we bind the image column from a different source and the other columns from the datatable?
<DataGrid x:Name="GridViewProducts" Margin="80,30,0,0"
AutoGenerateColumns="True" BorderThickness="12"
BorderBrush="PaleTurquoise" ItemsSource="{Binding}" Height="200"
Width="745" HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
VerticalAlignment="Center" MouseEnter="GridViewProducts_MouseEnter" >
<DataGrid.Columns>
<DataGridTemplateColumn DisplayIndex="0"
x:Name="imageColumn">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image x:Name="image_Grid_Column"
Source="{Binding ImageSource}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

Fit an Picture in a DataGrid

I'm trying to put a picture in my DataGrid. So I used a DataGridTemplateColumn but the Result is "glitchy".
Here is my wpf extract code :
<DataGrid MinRowHeight="24" x:Name="ListFamily" Grid.Column="1"
Margin="2" VerticalContentAlignment="Stretch"
HorizontalContentAlignment="Stretch"
AutoGenerateColumns="False" IsReadOnly="True"
SelectionChanged="ListFamily_SelectionChanged"
CanUserReorderColumns="False"
CanUserSortColumns="False" Grid.Row="1">
<DataGrid.Columns>
<!-- some datagridcolumn -->
<DataGridTemplateColumn Width="Auto" CanUserResize="False" IsReadOnly="True">
<DataGridTemplateColumn.Header>
<Label Content="{x:Static res:StringResources.language}" ToolTip="{x:Static tt:TooltipStrings.language}" HorizontalContentAlignment="Stretch"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Language, Converter={StaticResource StringToPicture}}" Height="{Binding RowHeight, ElementName=ListFamily}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Uniform"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style>
<Setter Property="FrameworkElement.VerticalAlignment" Value="Center"/>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
and here is what I get :
I triend to bind RowHeight of the DataGrid but without luck. Any ideas ?
Thanks
Thibaud
Using this code is working :
<DataGridTemplateColumn Width="Auto" CanUserResize="False" IsReadOnly="True">
<DataGridTemplateColumn.Header>
<Label Content="{x:Static res:StringResources.language}" ToolTip="{x:Static tt:TooltipStrings.language}" HorizontalContentAlignment="Stretch"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Language, Converter={StaticResource StringToPicture}}" Stretch="Uniform"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

MVVM WPF properties

In my WPF project a DataGrid is bound to a collection. When DataGrid row selection changes the corresponding TextBox, should get updated. The DataGrid selected item is bound to a property
SelectedItem="{Binding CardType.SelectedCard}"
and corresponding Text box is bound
Text="{Binding CardType.SelectedCard.LimitBalance}"
so when a selected item is changed, my text box will get updated.
DataGrid
<DataGrid
x:Name="dtGridSearch"
Margin="0,0,8,0"
AutoGenerateColumns="False"
GridLinesVisibility="None"
SelectionMode="Single"
CanUserAddRows="False"
BorderThickness="0"
RowHeaderWidth="0"
RowStyle="{StaticResource TCAS_RowStyle}"
ItemsSource="{Binding CardType.Cards}"
SelectedItem="{Binding CardType.SelectedCard}"
SelectionChanged="dtGridSearchSelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn x:Name="CardCode" Binding="{Binding CardId}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" IsReadOnly="True" Width="150" CanUserResize="False" >
<DataGridTextColumn.Header>
<TextBlock x:Name="tbCardCode" Text=""/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn x:Name="CardName" Binding="{Binding CardDescription}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" IsReadOnly="True" Width="250" CanUserResize="False" >
<DataGridTextColumn.Header>
<TextBlock x:Name="tbCardName" Text="" HorizontalAlignment="Center"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn x:Name="ProductCode" Binding="{Binding ProductTypeCode}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" IsReadOnly="True" Width="150" CanUserResize="False" >
<DataGridTextColumn.Header>
<TextBlock x:Name="tbProductCode" Text=""/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn x:Name="Limitbalance" Binding="{Binding LimitBalance}" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" IsReadOnly="True" Width="100*" CanUserResize="False" >
<DataGridTextColumn.Header>
<TextBlock x:Name="tbLimitbalance" Text="" HorizontalAlignment="Center"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
TextBox
<TextBox
x:Name="txtLimitbalance"
Grid.Row="3"
Grid.Column="1"
Margin="16,0,0,0"
Style="{StaticResource TMS_TextBox_Base1}"
Text="{Binding CardType.SelectedCard.LimitBalance, Mode=TwoWay}"
MaxLength="45"
Height="24"
Width="210"
TabIndex="4"
PreviewKeyDown="txtLimitbalancePreviewKeyDown"/>
<CheckBox
x:Name="chkused"
Grid.Row="3"
Grid.Column="2"
Margin="16,4,0,8"
Content="{Binding Resources.NPUsed}"
IsChecked="{Binding CardType.SelectedCard.ActiveStatus, Mode=TwoWay}"
TabIndex="5"/>
User update the text box to a different value from original value , so one property of row1 is updated ,now user selected the different row i.e rowenter code here2 from datagrid. again user selected the row1 so it is dispalying updated value not original value. I want to display original value only.

Categories

Resources