WPF datagrid in usercontrol - wpf

<DataGrid Width="auto" AutoGenerateColumns="False" Name="dgData" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding HODAppID}" Width="75" Header="HOD APPID" />
<DataGridTextColumn Binding="{Binding Req}" Header="CandidateID" Visibility="Hidden" />
<DataGridTextColumn Binding="{Binding Req}" Header="RequisitionNo" Visibility="Hidden" />
<DataGridTextColumn Binding="{Binding Candidate}" Width="1.3*" Header="CANDIDATE NAME" />
<DataGridTextColumn Binding="{Binding Req}" Width="*" MinWidth="200" Header="REQUIREMENT"/>
<DataGridTextColumn Binding="{Binding status}" Width="80" Header="Status" />
<DataGridTextColumn Binding="{Binding daysopen}" Width="120" x:Name="noDays" Header="# of Days Open" />
hi i have a datagrid , something like above, i was trying to get cell value in column noDays, and change the fore color based on the value in it.please help in this regard.Thanks in advance

Create a converter using IValueConverter and add it to your noDays column Foreground property. The converter should return a brush value based on the value of the daysopen property.
http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx
EDIT:
Apparently IValueConverter doesn't work straight on DataGridTextColumn. More info and an example can be found at http://social.msdn.microsoft.com/Forums/en/wpf/thread/77a9f317-d0be-4aed-9dc9-9917ad98ffc8.

Related

Red border surrounding cell overlaps row header when scrolling

I’m having a problem while using the DataGrid object with row headers.
Each row of the table contains an object that implements the iDataErrorInfo interface and when a cell has an error and a horizontal scroll happens, the red border of the cell overlaps the row header.
How can I solve the problem and avoid the overlapping?
<DataGrid ItemsSource="{Binding Customers}"
AutoGenerateColumns="False"
RowHeaderWidth="200"
CanUserResizeColumns="False"
HeadersVisibility="All">
<DataGrid.Columns>
<DataGridTextColumn Header="FirstName"
Binding="{Binding Path=FirstName, ValidatesOnDataErrors=True}"
Width="700" />
<DataGridTextColumn Header="LastName"
Binding="{Binding Path=LastName, ValidatesOnDataErrors=True}"
Width="700" />
<DataGridTextColumn Header="FullName"
Binding="{Binding Path=FullName, ValidatesOnDataErrors=True}"
Width="700" />
</DataGrid.Columns>
</DataGrid>

Typing Text into DataGridTextColumn doesn´t work

.. but pasting text does.
What could be the reason?
<DataGrid AutoGenerateColumns="false" Name="dataGrid" InitializingNewItem="dataGrid_InitializingNewItem" ItemsSource="{Binding SchadwagenVorgänge}">
<DataGrid.Columns>
<DataGridTextColumn Header="Bezeichnung" Width="*" Binding="{Binding Description}" />
<DataGridTextColumn Header="Sortierposition" Width="Auto" Binding="{Binding SortOrder}" />
<DataGridCheckBoxColumn Header="Umstellung" Width="Auto" Binding="{Binding ChangeOver }" />
</DataGrid.Columns>
</DataGrid>
Try something like this:
<DataGrid AutoGenerateColumns="false" Name="dataGrid"
InitializingNewItem="dataGrid_InitializingNewItem" ItemsSource=" {Binding SchadwagenVorgänge}">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="False" Header="Bezeichnung" Width="*" Binding="{Binding Description}" />
<DataGridTextColumn IsReadOnly="False" Header="Sortierposition" Width="Auto" Binding="{Binding SortOrder}" />
<DataGridCheckBoxColumn Header="Umstellung" Width="Auto" Binding="{Binding ChangeOver }" />
</DataGrid.Columns>
</DataGrid>

Daughter DataGrid.Columns fill all the available width

There is my layout:
<DataGrid x:Name="BooksGrid"
DataContext="{Binding WorkingBooksSet, Mode=TwoWay}"
ItemsSource="{Binding}"
RowDetailsVisibilityMode="VisibleWhenSelected"
AutoGenerateColumns="False"
VerticalAlignment="Top" HorizontalAlignment="Stretch"
ColumnWidth="*">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Title}" />
...
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Publications, Mode=TwoWay}"
AutoGenerateColumns="False"
VerticalAlignment="Top" HorizontalAlignment="Stretch"
ColumnWidth="*">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Publisher}" />
<DataGridTextColumn Binding="{Binding ReleaseYear}" />
...
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
And it's a window
As you can see, daughter DataGrid for some reason has not filled all the available space, instead made ​​them narrow. Moreover it is prohibited to manually resize the columns.
I do not understand this behavior, especially as the "parent" DataGrid displays correctly with the same code
Add * width to one of the columns in your child DataGrid.
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Publisher}" />
<DataGridTextColumn Binding="{Binding ReleaseYear}" Width="*" />
...
</DataGrid.Columns>

Why can I select a row in one data grid, but not the other?

I have two DataGrids:
<DataGrid Name="grdDailyOne"
AutoGenerateColumns="False"
CanUserResizeRows="False"
CanUserResizeColumns="False"
CanUserReorderColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="From"
Binding="{Binding From}"
MinWidth="100" />
<DataGridTextColumn Header="To"
Binding="{Binding To}"
MinWidth="100" />
<DataGridTextColumn Header="Elapsed Time"/>
<DataGridTextColumn Header="HDDC Codes"
Binding="{Binding HDDCCode}"
MinWidth="250" />
<DataGridTextColumn Header="Details of Operations in Sequence and Remarks"
Binding="{Binding Details}"
MinWidth="250" />
<DataGridTextColumn Header="Rot Ftg"
Binding="{Binding RotFtg}" />
<DataGridTextColumn Header="Slide Ftg"
Binding="{Binding SlideFtg}" />
</DataGrid.Columns>
</DataGrid>
And:
<DataGrid AutoGenerateColumns="False"
Name="grid1"
HorizontalAlignment="Center"
FontSize="25"
Margin="10,20,10,40"
CanUserResizeRows="False"
CanUserResizeColumns="False"
CanUserReorderColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Product"
Binding="{Binding Product}" />
<DataGridTextColumn Header="Amount"
Binding="{Binding Amount}" />
<DataGridTextColumn Header="Units"
Binding="{Binding Units}" />
</DataGrid.Columns>
</DataGrid>
Now, in the one on the bottom, I can select rows just fine, but the top one, I cannot. I've looked through, and all the properties are the same. I do know at one point, I was able to do so, but now I'm not. Is there a property that I'm missing that I need to set?
I have tried using SelectionMode="Single" SelectionUnit="FullRow", but still nothing.

Template for a datagrid

<DataGrid AutoGenerateColumns="False" Height="200" Margin="0,63,0,0" Name="dataGrid1" MouseDoubleClick="dataGrid1_MouseDoubleClick">
<DataGrid.Columns>
<DataGridTextColumn Header="Hospital Name" Width="100" Binding="{Binding hosp_name}"/>
<DataGridTextColumn Header="Order Number" Width="100" Binding="{Binding ordID}"/>
<DataGridTextColumn Header="Order Date" Width="*" Binding="{Binding placedDate}" />
</DataGrid.Columns>
</DataGrid>
Hey guys, I'm new with wpf, how can I make this into a template so I can render it each time I create a new tab programatically?
Wrap it in a DataTemplate and assign it as ContentTemplate of the TabControl.

Resources