Click here to Skip to main content
13,191,642 members (59,149 online)
Click here to Skip to main content
Add your own
alternative version

Stats

15.6K views
Posted 29 May 2012

CheckBoxList in Wpf With C#

Rate this:
Please Sign up or sign in to vote.
Display Selected Column Dynamically Using 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;
                }
            }
        }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Comments and Discussions

 
GeneralNice one Pin
Member 1338498231-Aug-17 1:20
memberMember 1338498231-Aug-17 1:20 
GeneralMy vote of 2 Pin
ateeq@tekskills20-Sep-12 3:03
memberateeq@tekskills20-Sep-12 3:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171017.2 | Last Updated 29 May 2012
Article Copyright 2012 by Amit Sanandiya From Morbi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid