Monday, April 29, 2013

Create Blank Data Grid According to Main Grid

Add Custom Control

public class BlankDataGrid : DataGrid
    {
        //Parent Data Grid
        private static DataGrid PdataGrid;
        //Blank Data Grid
        private static DataGrid BdataGrid;
        //how many row we will create
        private static int NoRow = 4;
        //if this value is 1 then no delete first recode,this is used for parent and child grid row color matching.
        private static int RemoveFirstRow;
        //Zero to null value converter into blank data grid.
        private static ZeroToEmptyConverter converter = new ZeroToEmptyConverter();
        //when paren data grid column hide then blank data grid column hide by this variable
        private static bool isParenDGColumnHide;
 
        //Constructor
        public BlankDataGrid()
        {
            try
            {
                this.Loaded += new RoutedEventHandler(BlankDataGrid_Loaded);
                this.LoadingRow += new System.EventHandler<DataGridRowEventArgs>(BlankDataGrid_LoadingRow);
            }
            catch (Exception)
            { throw; }
        }
 
        //when grid is loaded then it is called
        private void BlankDataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            //this.HeadersVisibility = DataGridHeadersVisibility.None;
            this.AutoGenerateColumns = false;
            this.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
            this.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
        }
 
        //when every row loaded then this method is called
        private void BlankDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            try
            {
                int dgridrow = e.Row.GetIndex();
                if (dgridrow == 0)
                    if (RemoveFirstRow == 1)
                        e.Row.Visibility = Visibility.Collapsed;
            }
            catch (Exception)
            { throw; }
        }
 
        #region Paren Data Grid Properties
 
        public DataGrid ParentDataGrid
        {
            get { return (DataGrid)GetValue(ParentDataGridProperty); }
            set { SetValue(ParentDataGridProperty, value); }
        }
 
        public static readonly DependencyProperty ParentDataGridProperty =
            DependencyProperty.Register("ParentDataGrid"typeof(DataGrid), typeof(BlankDataGrid),
            new PropertyMetadata(nullnew PropertyChangedCallback(OnParentDataGridChanged)));
 
        private static void OnParentDataGridChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                //clear parent grid temporary list
                PdataGrid = null;
                //get parent data grid
                PdataGrid = ((BlankDataGrid)d).ParentDataGrid as DataGrid;
                if (PdataGrid == nullreturn;
                if (PdataGrid.ItemsSource == nullreturn;
 
                //Parent Grid Sized Changed Event
                PdataGrid.SizeChanged += new SizeChangedEventHandler(dataGrid_SizeChanged);
                //Parent LayoutUpdated Event fire when parent grid column hide
                PdataGrid.LayoutUpdated += new EventHandler(PdataGrid_LayoutUpdated);
 
                //Get Parent Object Type
 
                Type t;
                if (PdataGrid.ItemsSource.GetType() == typeof(PagedCollectionView))
                {
                    var pageCollectionData = PdataGrid.ItemsSource as PagedCollectionView;
                    t = (pageCollectionData.SourceCollection as IEnumerable).AsQueryable().ElementType;
                }
                else
                {
                    t = (PdataGrid.ItemsSource as IEnumerable).AsQueryable().ElementType;
                }
                if (t == nullreturn;
                //Make Blank Data List
                dynamic instant = Activator.CreateInstance(t);//create instance
                var Datalist = new ObservableCollection<dynamic>();
                for (int i = 0; i < NoRow; i++)
                    Datalist.Add(instant);
 
                //add item source
                ((BlankDataGrid)d).ItemsSource = null;
                ((BlankDataGrid)d).ItemsSource = Datalist;
 
                //Clear data grid column
                ((BlankDataGrid)d).Columns.Clear();
                foreach (var columnName in PdataGrid.Columns)
                {
                    ((BlankDataGrid)d).Columns.Add(CreateTextColumn("id"));
                }
                //adjust blank grid according to parent grid.
                AdjustWidth(PdataGrid, ((BlankDataGrid)d));
 
                //Clear blank grid teporary
                BdataGrid = null;
                //set as a further use
                BdataGrid = ((BlankDataGrid)d);
            }
            catch (Exception)
            {
                throw;
            }
        }
 
        //create blank data grid column
        private static DataGridTextColumn CreateTextColumn(string fieldName)
        {
            DataGridTextColumn column = new DataGridTextColumn();
            column.Binding = new Binding(fieldName);
            column.Binding.Converter = converter;
            column.Binding.Mode = BindingMode.OneWay;
            return column;
        }
 
        //adjust blank grid according to parent grid.
        private static void AdjustWidth(DataGrid paren, DataGrid blank)
        {
            for (int i = 0; i < blank.Columns.Count; i++)
            {
                for (int j = 0; j < PdataGrid.Columns.Count; j++)
                {
                    if (i == j)
                    {
                        blank.Columns[i].Width = new DataGridLength(PdataGrid.Columns[j].ActualWidth);
                        blank.Columns[i].Visibility = PdataGrid.Columns[j].Visibility;
                        break;
                    }
                }//end of second for
            }//end of 1st for
        }
 
        //parent grid sized changed
        private static void dataGrid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            try
            {
                if (sender == nullreturn;
                if (BdataGrid == nullreturn;
                isParenDGColumnHide = false;
                var pGrid = sender as DataGrid;
                AdjustWidth(pGrid, BdataGrid);
            }
            catch (Exception)
            { throw; }
        }
 
        //Parent Grid LayoutUpdated Event fire when parent grid column hide
        private static void PdataGrid_LayoutUpdated(object sender, EventArgs e)
        {
            if (isParenDGColumnHide)
            {
                if (BdataGrid == nullreturn;
                if (PdataGrid == nullreturn;
                AdjustWidth(PdataGrid, BdataGrid);
                isParenDGColumnHide = false;
            }
        }
 
        #endregion
 
        #region Properties
 
        public int NumberOfRow
        {
            get { return (int)GetValue(NumberOfRowProperty); }
            set { SetValue(NumberOfRowProperty, value); }
        }
        public static readonly DependencyProperty NumberOfRowProperty =
            DependencyProperty.Register("NumberOfRow"typeof(int), typeof(BlankDataGrid),
            new PropertyMetadata(0, new PropertyChangedCallback(NumberOfRowChanged)));
 
        private static void NumberOfRowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (d != null)
                {
                    NoRow = 0;
                    NoRow = ((BlankDataGrid)d).NumberOfRow;
                }
            }
            catch (Exception)
            { throw; }
        }
 
        public int isRemoveFirstRow
        {
            get { return (int)GetValue(isRemoveFirstRowProperty); }
            set { SetValue(isRemoveFirstRowProperty, value); }
        }
        public static readonly DependencyProperty isRemoveFirstRowProperty =
            DependencyProperty.Register("isRemoveFirstRow"typeof(int), typeof(BlankDataGrid),
            new PropertyMetadata(0, new PropertyChangedCallback(isRemoveFirstRowChanged)));
 
        private static void isRemoveFirstRowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (d != null)
                {
                    RemoveFirstRow = ((BlankDataGrid)d).isRemoveFirstRow;
                }
            }
            catch (Exception)
            { throw; }
        }
 
        public bool isParenDataGridColumnHide
        {
            get { return (bool)GetValue(isParenDataGridColumnHideProperty); }
            set { SetValue(isParenDataGridColumnHideProperty, value); }
        }
        public static readonly DependencyProperty isParenDataGridColumnHideProperty =
            DependencyProperty.Register("isParenDataGridColumnHide"typeof(bool), typeof(BlankDataGrid),
            new PropertyMetadata(falsenew PropertyChangedCallback(isMainGridHideChanged)));
 
        private static void isMainGridHideChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (d != null)
                {
                    isParenDGColumnHide = ((BlankDataGrid)d).isParenDataGridColumnHide;
                }
            }
            catch (Exception)
            { throw; }
        }
 
        #endregion
    }

use of custom control in xaml
<lib:BlankDataGrid VerticalAlignment="Top" Grid.Row="1" Grid.Column="0" ParentDataGrid="{Binding ElementName=dgMasterGrid}"/>
                                                <sdk:DataGrid x:Name="dgMasterGrid" Grid.Row="1" Grid.Column="0" VerticalAlignment="Top"/>

No comments:

Post a Comment