Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CheckBoxList in Wpf With C#

0.00/5 (No votes)
29 May 2012 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here