Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get selected columns from checkedlistbox and show in datagridview only selected columns

additional information copied from comment below
sir i do something like this but this is not best way and code is so large
C#
if (dataGridView1.Rows.Count == 0)
{
   MessageBox.Show("Please first a search a record");
   return;
}

if (ch_cast.Checked == true)
{
   dataGridView1.Columns[10].Visible = true;
}
else
{
   dataGridView1.Columns[10].Visible = false;
}

if (ch_doa.Checked== true)
{
   dataGridView1.Columns[8].Visible = true;
}
else
{
   dataGridView1.Columns[8].Visible = false;
}

if (ch_dob.Checked == true)
{
   dataGridView1.Columns[3].Visible = true;
}
else
{
   dataGridView1.Columns[3].Visible = false;
}

if (ch_fn.Checked == true)
{
   dataGridView1.Columns[6].Visible = true;
}
else
{
   dataGridView1.Columns[6].Visible = false;
}

if (ch_mn.Checked == true)
{
   dataGridView1.Columns[7].Visible = true;
}
else
{
   dataGridView1.Columns[7].Visible = false;
}

if (ch_regno.Checked == true)
{
   dataGridView1.Columns[9].Visible = true;
}
else
{
   dataGridView1.Columns[9].Visible = false;
}

if (ch_smn.Checked == true)
{
   dataGridView1.Columns[5].Visible = true;
}
else
{
   dataGridView1.Columns[5].Visible = false;
}

if (checkBox1.Checked == true)
{
   dataGridView1.Columns[11].Visible = true;
}
else
{
   dataGridView1.Columns[11].Visible = false;
}
Posted
Updated 8-Jun-14 22:30pm
v2
Comments
What have you tried?
Member 10285877 9-Jun-14 1:31am    
sir i have many columns in datagridview and show some columns in different time so i use checkboxlist and user select the columns from check-boxlist and display colmns in datagridview
Fadi Halboni 9-Jun-14 1:50am    
Dear,
kindly give us example to help you.

Thanks.
Member 10285877 9-Jun-14 2:46am    
ok Sir I have a table with 20 columns and all the columns show in my datagridview.
bur user want there checkedboxlist and only datagridview shows those columns that user cheeked .
Member 10285877 9-Jun-14 2:47am    
checked list box is different there show only datagridview columns

1 solution

you have checkbox list with columns names and datagridview with all columns names with visible false, and when check all columns you need click button "generate" to visible all datagridview columns selected from check box list.
 
Share this answer
 
Comments
Member 10285877 9-Jun-14 3:54am    
ya sir
Problem is that how to columns of table bind the checkboxlist
Member 10285877 9-Jun-14 3:56am    
sir i do something like this but this is not best way and code is so large

if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("Please first a search a record");
return;
}
if (ch_cast.Checked == true)
{
dataGridView1.Columns[10].Visible = true;
}
else
{
dataGridView1.Columns[10].Visible = false;
}
if (ch_doa.Checked== true)
{

dataGridView1.Columns[8].Visible = true;
}
else
{
dataGridView1.Columns[8].Visible = false;
}
if (ch_dob.Checked == true)
{

dataGridView1.Columns[3].Visible = true;
}
else
{
dataGridView1.Columns[3].Visible = false;
}
if (ch_fn.Checked == true)
{

dataGridView1.Columns[6].Visible = true;
}
else
{
dataGridView1.Columns[6].Visible = false;
}
if (ch_mn.Checked == true)
{

dataGridView1.Columns[7].Visible = true;
}
else
{
dataGridView1.Columns[7].Visible = false;
}
if (ch_regno.Checked == true)
{

dataGridView1.Columns[9].Visible = true;
}
else
{
dataGridView1.Columns[9].Visible = false;
}
if (ch_smn.Checked == true)
{

dataGridView1.Columns[5].Visible = true;
}
else
{
dataGridView1.Columns[5].Visible = false;
}
if (checkBox1.Checked == true)
{

dataGridView1.Columns[11].Visible = true;
}
else
{
dataGridView1.Columns[11].Visible = false;
}
Fadi Halboni 9-Jun-14 4:35am    
Dear,
this is not best for performance please bind check box from database and check it by using (for loop).

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900