Friday, July 15, 2011

Hide & Un-Hide DataGrid Column(s) In WPF

MainWindow.xaml

<window height="350" title="MainWindow" width="525"  x:class="TryWPF.MainWindow"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xam/presentation">
<grid>
<datagrid autogeneratedcolumns="dataGrid1_AutoGeneratedColumns" canuseraddrows="True" canuserdeleterows="True" canuserreordercolumns="True" horizontalalignment="Left" margin="229,12,0,0" name="dataGrid1" verticalalignment="Top" mouserightbuttonup="dataGrid1_MouseRightButtonUp">
</datagrid>
</grid>
</window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sybase.Data.AseClient;
using System.Data;
using System.IO;
namespace TryWPF
{

 public partial class MainWindow : Window
    {
       //context menu for column

        MenuItem menuItem;
        ContextMenu cxMenu;

        AseDataAdapter da = null;

        AseConnection conn = null;

        DataSet DS = null;

        public MainWindow()
        {
            try
            {
                InitializeComponent();

                Bind_Grid();
            }
            catch(Exception ex)
            {
                ex.ToString();
            }
        }

        void Bind_Grid()
        {
            try
            {

                DS = new DataSet();
                conn = new AseConnection(
                    "Data Source=IFLMUD6DLB8JN:5000;Initial 
                     Catalog=master;User ID = sa;   Password=;");

                // 2. Open the connection
                conn.Open();

                // 3. Pass the connection to a command object
                AseCommand cmd = new AseCommand();

                cmd.CommandText = "sp_who";

                cmd.CommandType = CommandType.StoredProcedure;

                //cmd = new AseCommand(cmd.CommandText, conn);

                da = new AseDataAdapter(cmd.CommandText, conn);

                da.Fill(DS);

                dataGrid1.ItemsSource = DS.Tables[0].DefaultView;

                dataGrid1.AutoGeneratedColumns +=
                                                            new EventHandler(dataGrid1_AutoGeneratedColumns);

            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            finally
            {

                // 5. Close the connection
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

void menuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem item = sender as MenuItem;

            if (item.IsChecked)
            {
                item.IsChecked = false;
            }
            else
            {
                item.IsChecked = true;
            }
        }

        void menuItem_Checked(object sender, RoutedEventArgs e)
        {
            MenuItem item = sender as MenuItem;

            dataGrid1.AutoGeneratedColumns -= new EventHandler(dataGrid1_AutoGeneratedColumns);

            List<string> menuList = new List<string>();

            menuList.Clear();

            foreach (MenuItem menuItem in cxMenu.Items)
            {
                if (menuItem.IsChecked == false)
                {
                    menuList.Add(menuItem.Items.ToString());
                }
            }

            dataGrid1.ItemsSource = null;

            dataGrid1.ItemsSource = DS.Tables[0].DefaultView;

            foreach (string menuItem in menuList)
            {
                foreach (DataGridColumn column in dataGrid1.Columns)
                {
                    if (column.Header.ToString() == menuItem)
                    {
                        dataGrid1.Columns.Remove(column);
                        break;
                    }
                }
            }
        }

        void menuItem_Unchecked(object sender, RoutedEventArgs e)
        {
            MenuItem item = sender as MenuItem;

            foreach (DataGridColumn column in dataGrid1.Columns)
            {
                if (column.Header.ToString().Contains(item.Header.ToString()))
                {
                    dataGrid1.Columns.Remove(column);
                    break;
                }
            }
        }
     
        private void dataGrid1_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            DependencyObject DepObj = (DependencyObject)e.OriginalSource;

            while ((DepObj != null) && !(DepObj is DataGridColumnHeader))
            {
                DepObj = VisualTreeHelper.GetParent(DepObj);

                if (DepObj == null)
                {
                    return;
                }

                if (DepObj is DataGridColumnHeader)
                {
                    DataGridColumnHeader dgCHeader = DepObj as DataGridColumnHeader;

                    dgCHeader.ContextMenu = cxMenu;
                }

                if (DepObj is DataGridCell)
                {

                    while ((DepObj != null) && !(DepObj is DataGridRow))
                    {
                        DepObj = VisualTreeHelper.GetParent(DepObj);
                    }
                    DataGridRow dgRow = DepObj as DataGridRow;
                    dgRow.ContextMenu = cxMenuRow;
                }
            }

           
        }

        private void dataGrid1_AutoGeneratedColumns(object sender, EventArgs e)
        {
            cxMenu = new ContextMenu();

            foreach (DataGridColumn item in dataGrid1.Columns)
            {
                menuItem = new MenuItem();

                menuItem.Header = item.Header;

                menuItem.IsChecked = true;

                cxMenu.Items.Add(menuItem);

                menuItem.Click += new RoutedEventHandler(menuItem_Click);

                menuItem.Checked += new RoutedEventHandler(menuItem_Checked);

                menuItem.Unchecked += new RoutedEventHandler(menuItem_Unchecked);
            }
        }

}
}


No comments:

Post a Comment