DataGrid Scrolling not working - c#

I have a datagrid which is populated using data retrieved from a database. Unfortunately, when the number of rows retrieved is more than the height of the datagrid, I cannot scroll vertically or horizontally, even though both scrollbars are shown.
Here is my code:
<data:DataGrid x:Name="DataGrid_Transactions" AutoGenerateColumns="False" VerticalAlignment="Center" Height="300" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Background="#FF0F1111" FontSize="26" Foreground="RoyalBlue" HorizontalGridLinesBrush="AliceBlue" IsReadOnly="True" VerticalGridLinesBrush="AliceBlue" AlternatingRowBackground="AliceBlue" FontFamily="Segoe WP SemiLight" RowDetailsVisibilityMode="Visible" RowBackground="AliceBlue">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Transaction ID" Binding="{Binding TransactionID}" />
<data:DataGridTextColumn Header="Vendor Username" Binding="{Binding VendorUsername}" />
<data:DataGridTextColumn Header="Purchase Description" Binding="{Binding PurchaseDescription}" />
<data:DataGridTextColumn Header="Total Price" Binding="{Binding TotalPrice}" />
<data:DataGridTextColumn Header="Currency" Binding="{Binding Currency}" />
</data:DataGrid.Columns>
</data:DataGrid>
<Button Content="Main Page" Height="72" HorizontalAlignment="Center" Margin="0,540,0,43" Name="Button_MainPage" VerticalAlignment="Center" Width="456" Click="Button_MainPage_Click" />
</Grid>
</Grid>
Can someone please help me? Thank you :)

It seems attached property does not work directly for this type objects,
Even it works for ListBox(http://www.silverlightshow.net/items/Tip-How-to-specify-ScrollViewer-s-attached-properties-in-XAML.aspx)
You should put it in a ScrollViewer
<ScrollViewer ...>
content
</ScrollViewer>
http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer(v=vs.95).aspx
For another way, have a look this attached property coding tip
http://www.codeproject.com/Articles/95746/Exposing-and-Binding-to-a-Silverlight-ScrollViewer
You may also code your own attached property.(you shoud use RegisterAttached while dependency object registration,and obey attached property coding rules.)

I have working code :
<sdk:DataGrid x:Name="dgList" Margin="0" HeadersVisibility="All" AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Single" UseLayoutRounding="True" BorderThickness="1" BorderBrush="#FFA2B4D3">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding DOCTORNO}" Header="No" />
<sdk:DataGridTextColumn Binding="{Binding DOCTORNAME}" Header="Ad Soyad" />
<sdk:DataGridTextColumn Binding="{Binding UZMANLIKKODU}" Header="Branş" />
<sdk:DataGridTextColumn Binding="{Binding BRANSAD}" Header="Branş Adı" />
</sdk:DataGrid.Columns>

Related

DataGrid not showing in .Droid (Uno-platform)

I made DataGrid that correctly displays data in .Wasm and .UWP, but on .Droid app it doesn't show anything (header or body). The calling to my API works and data is correctly sent to .Droid. I store it in a list and set list as ItemsSource of my DataGrid.
This shows list filling:
list = await ApiService.GetList();
dataGrid1.ItemsSource = list;
UserPage.Xaml:
<Page
...
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<controls:DataGrid
x:Name="dataGrid1" IsReadOnly="True" Width="Auto"
Height="Auto" Margin="12"
AutoGenerateColumns="False"
HorizontalAlignment="Left" VerticalAlignment="Top">
<controls:DataGrid.Columns>
<controls:DataGridTextColumn
Width="SizeToCells"
Binding="{Binding id}"
FontSize="20" />
<controls:DataGridTextColumn
Width="SizeToCells"
Binding="{Binding name}"
FontSize="20" />
<controls:DataGridTextColumn
Width="SizeToCells"
Binding="{Binding role}"
FontSize="20" />
</controls:DataGrid.Columns>
</controls:DataGrid>
I have installed Uno.Microsoft.Toolkit.Uwp.UI.Controls and Uno.Microsoft.Toolkit.Uwp.UI.Controls.DataGrid on .Droid project. I followed documentation but can't seem to find the solution.

Resize columns not working in wpf DataGrid

I want to make data grid columns re-sizable based on it's content. I have tried setting CanUserResizeColumns to true but it's not working. If i put this in new wpf project then it would work perfectly but in my exisiting wpf project it's not working.
Here's what I am doing :
<UserControl>
<Grid>
<ScrollViewer
<DataGrid Name="dgAllJob" CanUserResizeColumns="True"
Width="{Binding RelativeSource={RelativeSource Self}, Path=Parent.ActualWidth}"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="False"
ColumnWidth="*"
HorizontalAlignment="Stretch"
CanUserAddRows="False"
AutoGenerateColumns="False"
CanUserSortColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Job No" Binding="{Binding JobNumber,Mode=OneTime}"/>
<DataGridTextColumn Visibility="Collapsed" Header="Start Date" Width="80" Binding="{Binding ActualStartDate,StringFormat=dd/MM/yyyy,Mode=OneTime}"/>
<DataGridTextColumn Header="Client Name" Binding="{Binding BillName,Mode=OneTime}" CanUserResize="True"/>
<DataGridTextColumn Header="Postal Address" Binding="{Binding BillAddress,Mode=OneTime}" CanUserResize="True"/>
<DataGridTextColumn Header="Suburb" CanUserResize="True" Binding="{Binding BillSuburb,Mode=OneTime}"/>
<DataGridTextColumn Header="Site Suburb" CanUserResize="True" Binding="{Binding SiteSuburb,Mode=OneTime}" />
<DataGridTextColumn Header="Contact Name" Binding="{Binding ContactName,Mode=OneTime}" CanUserResize="True"/>
<DataGridTextColumn Header="Estimator" Binding="{Binding Estimator,Mode=OneTime}" CanUserResize="True"/>
<DataGridTextColumn Header="Installer" Binding="{Binding Installer,Mode=OneTime}" CanUserResize="True"/>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</Grid>
</UserControl>
can anyone tell what I am missing or what is wrong here ???
Any custom styles in your exisiting project may disable the resize functionality.
You could try to for example set the ColumnHeaderStyle property to x:Null or simply remove the custom Style.
Since you are putting your DataGrid in a ScrollViewer, the DataGrid will be sized to the minimum. Try disabling the HorizontalScrollBarVisibility or take it out of the ScrollViewer.

I would like to dynamically set DataGridCell background color or style based on colors in a database for that specific cell only. How do I do this?

I have a DataTable that has a person's name and color in ARGB.
I have been able to create brushes and/or styles from the color individually but have trouble binding the cellStyle of DataGridTextColumn of WindowsToolkit DataGrid in a UWP project.
enter image description here
The Database is bound as ItemsSource to the DataGrid and shows correctly but unable to pass on the color/brush/style to cellStyle.
How do I achieve this?
<Custom:DataGrid x:Name="DataGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" HorizontalAlignment="Left" FrozenColumnCount="1" Margin="0,0,0,0" Width="1480" CanUserSortColumns="True" Grid.Row="1">
<Custom:DataGrid.Columns>
<Custom:DataGridTextColumn Header="ProvNum" Binding="{Binding [0]}" Tag="ProvNum" Visibility="Collapsed" />
<Custom:DataGridTextColumn Header="ProvStyle" Binding="{Binding [1]}" Tag="ProvStyle" />
<Custom:DataGridTextColumn Header="Provider" Binding="{Binding [2]}" Tag="Provider" />
<Custom:DataGridTextColumn Header="Production" Binding="{Binding [3], Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:C1}'}" Tag="Production" />
<Custom:DataGridTextColumn Header="Adjustments" Binding="{Binding [4], Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:C1}'}" Tag="Adjustments" />
<Custom:DataGridTextColumn Header="Writeoffs" Binding="{Binding [5], Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:C1}'}" Tag="Writeoffs" />
<Custom:DataGridTextColumn Header="Income" Binding="{Binding [6], Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:C1}'}" Tag="Income" />
<Custom:DataGridTextColumn Header="Net Production" Binding="{Binding [7], Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:C1}'}" CellStyle="{StaticResource ProductionColumn}" Tag="NetProduction" />
</Custom:DataGrid.Columns>
</Custom:DataGrid>
Are you using DataGrid from the community toolkit? If so, it seems no direct way for the DataGrid to do this. But you could use DataGridTemplateColumn type, it enables you to create your own column types by specifying the cell templates used to display values. Set the CellTemplate property to specify the contents of cells and you can bind background with the following Border.
<controls:DataGrid.Columns>
//just one column
<controls:DataGridTemplateColumn Header="Name">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate x:DataType="local:Person">
<Border Background="{x:Bind CellForColorInDataTable}">
<TextBlock Text="{x:Bind Name}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
// another colunm
<controls:DataGridTextColumn Header="ProvNum" ....../>
......
</controls:DataGrid.Columns>

binding to static property problem

I'm using the EntityFramework and working on an invoicing application. Below is the xaml code from myDataGrid. Now it compiles and everything works great, but the problem is, the static binding I have on the DataGridComboBoxColumn throws an exception when in designer mode because that static property actually loads from a MySql database using the entity framework.
<DataGrid x:Name="ItemsGrid" Grid.Row="2" Grid.ColumnSpan="3" ItemsSource="{Binding Items}" FontSize="11" AutoGenerateColumns="False" CanUserAddRows="False" RowHeaderWidth="0" GridLinesVisibility="None" CanUserResizeRows="False" CanUserResizeColumns="False" CanUserReorderColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Width="150" Header="Item No.">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="130" BorderThickness="0" Background="Transparent" Text="{Binding Number}" />
<Image MouseDown="ItemSelectionButton_Click" Margin="0,0,0,0" Width="12" Source="/Images/Icons/SearchBlack.png" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="70" Header="Quantity" Binding="{Binding Quantity}" />
<DataGridTextColumn Width="70" Header="Order" Binding="{Binding Order}" />
<DataGridTextColumn Width="70" Header="B/O" Binding="{Binding BackOrder}" />
<DataGridTextColumn Width="60" Header="Units" Binding="{Binding Units}" />
<DataGridTextColumn Width="200" Header="Description" Binding="{Binding Description}" />
<DataGridTextColumn Width="90" Header="Price" Binding="{Binding Price}" />
<DataGridComboBoxColumn Width="50" Header="Tax" ItemsSource="{x:Static app:Session.TaxCodes}" SelectedValueBinding="{Binding TaxCodeID}" DisplayMemberPath="Code" SelectedValuePath="ID" />
<DataGridTextColumn Width="90" Header="Amount" Binding="{Binding Amount}" />
<DataGridTextColumn Width="90" Header="Linked" Binding="{Binding SalesOrderID}" />
</DataGrid.Columns>
</DataGrid>
Here's the exception:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
at DtcInvoicer.Models.DatabaseEntities..ctor() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Models\DatabaseModel.Designer.cs:line 42
at DtcInvoicer.Models.Repository.get_GetContext() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Models\Repository.cs:line 14
at DtcInvoicer.Models.TaxCodesRepository.SelectAll() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Models\TaxCodesRepository.cs:line 54
at DtcInvoicer.Session..cctor() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Session.cs:line 18
I know trying to fix the root of the problem will be too much work, I'm wondering if there's a simple workaround like dynamically binding to a static property in another class. Anyone have any idea how to do this? Looking for a xaml only solution.
Here's what I already tried...
ItemsSource="{Binding Path=TaxCodes, Source={x:Static app:Session}}"
ItemsSource="{Binding Path=TaxCodes, Source={StaticResource app:Session}}"
ItemsSource="{Binding app:Session.TaxCodes}"
Any help is appreciated.
Thanks.
Could you use something like d:ItemsSource="{x:Null}", and define d in your Window or UserControl like mc:Ignorable="d"?
It should make the design-time property of ItemsSource equal to null
Edit
Forgot to add the definition for mc - It is
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

WPF Toolkit Datagrid - how do you turn selection off?

I have a datagrid in WPF that I am binding to an object.
I have a DataGridCheckBoxColumn on there which I want the users to be able to go through and tick the ones they want. Problem is they have to click twice, once for selection then again to check/uncheck. How on earth do you turn this off, I've been searching for way to long to find the answer to this. The datagrid has SelectionMode and SelectionUnit properties - neither of which accept 'none' or 'go away'
Any help is appreciated! My code is below for reference
<my:DataGrid Margin="15" Name="dgPreview"
AutoGenerateColumns="False" CanUserSortColumns="True"
CanUserDeleteRows="True"
Background="White"
ColumnHeaderHeight="20"
VerticalScrollBarVisibility="Visible"
RowDetailsVisibilityMode="Visible"
>
<my:DataGrid.Columns>
<my:DataGridCheckBoxColumn MinWidth="50" Width="Auto" Header="Include" Binding="{Binding Include}" />
<my:DataGridTextColumn MinWidth="50" Width="Auto" Header="Override #" Binding="{Binding OverrideNumber}" />
<my:DataGridTextColumn MinWidth="220" Width="*" Header="Name" Binding="{Binding Name}" />
<my:DataGridTextColumn MinWidth="50" Width="Auto" IsReadOnly="True" Header="Preview" Binding="{Binding Preview}" />
</my:DataGrid.Columns>
</my:DataGrid>
The first click puts the cell in edit mode then the second click allows you to modify the checkbox. You can change this behavior by using a DataGridTemplateColumn instead of a DataGridCheckBoxColumn. Replace your DataGridCheckBoxColumn with this:
<my:DataGridTemplateColumn MinWidth="50" Width="Auto" Header="Include" SortMemberPath="Include">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Style="{StaticResource DataGridCheckBoxStyle}" IsChecked="{Binding Path=Include}" />
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
DataGridCheckBoxStyle just makes the CheckBox look a little nicer in the DataGrid:
<Style x:Key="DataGridCheckBoxStyle" TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="8,0,3,0" />
</Style>
First of, I know this is a pretty old question but I still thought I'd try and answer it.
I had the same problem a couple of days ago and came across a surprisingly short solution for it (see this blog). Basically, all you need to do is replace the DataGridCheckBoxColumn definition in your XAML with the following:
<DataGridTemplateColumn Header="MyCheckBoxColumnHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding Path=MyViewModelProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The upside of this solution is obvious - it's XAML-only; thus it effectively refrains your from burdening your code-back with additional UI logic and helps you maintain your status in the eyes of MVVM zealots ;).

Categories