Monday, September 22, 2014

SQL Delete All Records Except Top N Records

SQL : Delete All Records Except Top N Records 

-- Test Count Before Delete  :

Select count(Id) from [MyTable]

-- Delete All Records Except Top N Records :

DELETE FROM [MyTable] WHERE [Id] NOT IN (SELECT TOP 500 [Id] FROM [MyTable])

-- Test Count After Delete
Select count(Id) from [MyTable]

Tuesday, September 9, 2014

WPF ListView , GridView Select All CheckBox Operations Full Example (Multi Select)

WPF ListView , GridView Select All CheckBox Operations Full Example

Lately i'm working on developing tool to improve my work productivity using WPF (Windows Presentation Foundation)

And i encounter a problem that i want to use GridView with  Check Box on the Header to Select All Rows "Items"





XAML Code :


 <ListView  Name="lstgrd" SelectionMode="Multiple" Margin="10,10,10,0" SelectionChanged="lstgrd_SelectionChanged" MouseDoubleClick="lstgrd_MouseDoubleClick" ScrollViewer.VerticalScrollBarVisibility="Visible"  ScrollViewer.HorizontalScrollBarVisibility="Visible"  ScrollViewer.CanContentScroll="True"  HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="119.263">
            <ListView.View>
                <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Authors">
                    <!-- Add a ContextMenu to GridView Header -->
                    <GridView.ColumnHeaderContextMenu>
                        <ContextMenu >
                            <MenuItem Header="Ascending" Click="MenuItem_Click" />
                            <MenuItem Header="Descending" />
                        </ContextMenu>
                    </GridView.ColumnHeaderContextMenu>
                    <!-- Add GridVeiw Columns -->
                    <GridViewColumn  Width="25">
                        <GridViewColumn.Header>
                            <CheckBox Name="chkWspSelectAll" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="chkWspSelectAll_Checked"  Unchecked="chkWspSelectAll_Unchecked"  IsThreeState="False"/>
                        </GridViewColumn.Header>
                     
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Name="chkWspSelect" IsChecked="{Binding Path=IsSelected}" HorizontalContentAlignment="Center"  HorizontalAlignment="Center" VerticalAlignment="Center" Checked="chkWspSelect_Checked"  Unchecked="chkWspSelect_Unchecked"  IsThreeState="False"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Path=Age}" />
                    <GridViewColumn Header="Mail" Width="150">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>                  
                </GridView>
            </ListView.View>
        </ListView>