Friday, June 1, 2012

CheckBoxList in Wpf With C#

Introduction

Display Selected Column Dynamically Using checkBoxList in Wpf With C# Demo

Using the code
You Want to Display Selected Column Dynamically run time Using CheckBoxList then Just Use this Code in your application.

First Create one Data base
Example of ->

-- DataBase Name :- db
-- Create Five Column
-- Id Name Address City Phone
-- Enter some data in database

First We Create Form Design
<Grid>

<Grid Margin="20,12,0,180" HorizontalAlignment="Left" Width="256" VerticalAlignment="Top">

<GroupBox Header="Select Column" Margin="0,0,6,-113">

<telerik:RadWrapPanel Name="RadWrapPanel1" VerticalAlignment="Top" Height="auto" HorizontalAlignment="Left" Width="auto"></telerik:RadWrapPanel>

</GroupBox>

</Grid>

<telerik:RadGridView HorizontalAlignment="Left" Margin="0,131,0,0" Name="radGridView1" ShowGroupPanel="False" VerticalAlignment="Top" Width="288" />

</Grid>

This Code Write down in your code file

public partial class onlytestingform : Page
{
        //Declare Connection String and other object
        SqlConnection con = new SqlConnection(@"Enter your Connection String");
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        public List<System.Windows.Controls.CheckBox> checkBoxList = new List<System.Windows.Controls.CheckBox>();
        public System.Windows.Controls.CheckBox chk;
        public onlytestingform()
        {
            InitializeComponent();
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //Call your Function For Fill Your Grid Load Time
            fillgrid();
            //Call your Function For Fill Your RadWrapPanel1 Load Time
            BindParts();
        }
        //Fill Your Grid First
        public void fillgrid()
        {
            con.Open();
            string sql="select * from db";
            cmd = new SqlCommand(sql,con);
            da.SelectCommand = cmd;
            da.Fill(ds);
          
            if (ds.Tables[0].Rows.Count > 0)
            {
                radGridView1.ItemsSource = ds.Tables[0].DefaultView;
            }
            con.Close();
        }

        //Bind Column in RadWrapPanel1 With CheckBox Using GridView Column
        public void BindParts()
        {
            checkBoxList.Clear();
            for (int i = 0; i < radGridView1.Columns.Count; i++)
            {
                chk = new System.Windows.Controls.CheckBox();
                checkBoxList.Add(chk);
                RadWrapPanel1.Children.Add(chk);
                chk.Width = 150;
                chk.Height = 22;
                chk.Content = Convert.ToString(radGridView1.Columns[i].Header);
                chk.IsChecked = true;
                chk.Checked += new RoutedEventHandler(chk_Checked);
                chk.Unchecked += new RoutedEventHandler(chk_Unchecked);
            }
        }
        //Hide Column Code
        void chk_Unchecked(object sender, RoutedEventArgs e)
        {
            List<string> chkUnchekList = new List<string>();
            chkUnchekList.Clear();
            foreach (System.Windows.Controls.CheckBox item in checkBoxList)
            {
                if (item.IsChecked == false)
                {
                    chkUnchekList.Add(item.Content.ToString());
                }
            }
            for (int i = 0; i < radGridView1.Columns.Count; i++)
            {
                if (chkUnchekList.Contains(radGridView1.Columns[i].Header.ToString()))
                {
                    radGridView1.Columns[i].IsVisible = false;
                }
            }
        }
      
        //Un-Hide Column Code
        void chk_Checked(object sender, RoutedEventArgs e)
        {
            List<string> chkCheckList = new List<string>();
            chkCheckList.Clear();
            foreach (System.Windows.Controls.CheckBox item in checkBoxList)
            {
                if (item.IsChecked == true)
                {
                    chkCheckList.Add(item.Content.ToString());
                }
            }
            for (int i = 0; i < radGridView1.Columns.Count; i++)
            {
                if (chkCheckList.Contains(radGridView1.Columns[i].Header.ToString()))
                {
                    radGridView1.Columns[i].IsVisible = true;
                }
            }
        }
}

This Code Succefully Run.............

0 comments:

Post a Comment

Followers