Search This Blog

Showing posts with label DataGrid. Show all posts
Showing posts with label DataGrid. Show all posts

Monday, March 13, 2017

ScrollToTop DataGrid WPF

WPF> DataGrid > Scroll To Top


var border = VisualTreeHelper.GetChild(myDataGrid, 0) as Decorator;
if (border != null && border.Child !=null)
{
  var scrollViewer = border.Child as ScrollViewer;
  if (scrollViewer != null)
  {
    scrollViewer.ScrollToTop();
  }

}

Tuesday, February 21, 2017

Disable row selection datagrid WPF


How to disable row selection in datagrid WPF?

Answer:

myDataGrid.IsHitTestVisible = false;

Wednesday, November 16, 2016

Clear Sort WPF DataGrid

WPF > Controls > ItemsControl > DataGrid > Clear Sort

_collectionView = (CollectionView)CollectionViewSource.GetDefaultView(dataGrid.Items);

_collectionView.SortDescriptions.Clear();

Friday, June 3, 2016

DataGrid WPF show row number

WPF > Controls > ItemsControl > DataGrid > Show row number

XAML


<DataGrid 
        x:Name="dataGrid"
        LoadingRow="dataGrid_LoadingRow"

Code

private void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
  // show row number
    e.Row.Header = (e.Row.GetIndex() + 1).ToString();

}

Monday, May 23, 2016

DataGridRow change color of selected row

WPF > DataGrid Color of selected row


  <DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFB8AEAE"/>
  </DataGrid.Resources>


Last column width in WPF datagrid to take all the left space

WPF > DataGrid > Last column width


XAML

<DataGridTemplateColumn Header="Range" Width="100'*">

Or Code behind:

 // make the last column in WPF data grid take all the left space, always

dataGrid.Columns[dataGrid.Columns.Count - 1].Width = new DataGridLength(1, DataGridLengthUnitType.Star);


Friday, May 20, 2016

WPF datagrid edit cell on single click

WPF > DataGrid > Edit cell on single click

<Window.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <EventSetter Event="PreviewMouseLeftButtonDown"    Handler="DataGridCell_PreviewMouseLeftButtonDown" />
        </Style>
    </Window.Resources>

private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DataGridCell cell = sender as DataGridCell;
    EditCell(cell, e);
}

private void EditCell(DataGridCell cell, RoutedEventArgs e)
{
    if (cell == null || cell.IsEditing || cell.IsReadOnly)
       return;

    if (!cell.IsFocused)
    {
                cell.Focus();
    }
    DGrid.BeginEdit(e);
    cell.Dispatcher.Invoke(
           DispatcherPriority.Background,
           new Action(delegate { })
    );

}

Wednesday, March 2, 2016

Binding image in DataGrid WPF example

XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <DataGrid x:Name="dataGrid1" AutoGenerateColumns="False" SelectionChanged="dataGrid1_SelectionChanged">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding PictureName}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding PictureFilePath}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

</Window>



C#

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WpfApplication1
{
    public class PictureObject
    {
        public string PictureName { get; set; }
        public Uri PictureFilePath { get; set; }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<PictureObject> list = new List<PictureObject>();
            list.Add(new PictureObject() { PictureFilePath = new Uri("file:///c:\\Windows\\System32\\PerfCenterCpl.ico"), PictureName="Picture1" });
            dataGrid1.ItemsSource = list;
        }
    }
}

Wednesday, November 13, 2013

Master detail DataGrid example with code behind

WPF > ControlsItemControls > DataGridMaster detail 2 grids

In this example we will use two DataGrid controls to display master detail data from code behind using SelectionChanged event.


XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        Title="MainWindow" Height="447.91" Width="514.871" Loaded="Window_Loaded">
    <Grid Margin="0,0,2,81">
        <DataGrid x:Name="dg" HorizontalAlignment="Left" VerticalAlignment="Top" Height="196" Width="507" SelectionChanged="dg_SelectionChanged"/>
        <DataGrid x:Name="dg1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="214" Width="505" Margin="0,201,0,-78"/>
    </Grid>
</Window>

C# Code:



namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    ///
    public class Skill
    {
        public int Years { get; set; }
        public string Name { get; set; }
    }
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Position { get; set; }
        public List<Skill> SkillList { get; set; } 
    }
    public class Persons : ObservableCollection<Person>
    {
        public Persons()
        {
            Add(new Person() { Id = 1, Name = "Person1", Position = "Manager", SkillList = new List<Skill>() });
            Add(new Person() { Id = 2, Name = "Person2", Position = "Programmer", SkillList = new List<Skill>() });
            Add(new Person() { Id = 3, Name = "Person3", Position = "Programmer", SkillList = new List<Skill>() });
            Add(new Person() { Id = 4, Name = "Person4", Position = "Admin", SkillList = new List<Skill>() });
            Add(new Person() { Id = 5, Name = "Person5", Position = "Tester", SkillList = new List<Skill>() });
        }
    }
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
         {
            Persons pList = new Persons();
            pList[1].SkillList.Add(new Skill() {Years = 5, Name="C++"});
            pList[1].SkillList.Add(new Skill() {Years = 15, Name = "Visual Basic" });
            dg.ItemsSource = pList;
        }
        private void dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            dg1.ItemsSource = ((Person)e.AddedItems[0]).SkillList;
        }
    }
}