Click here to Skip to main content
15,883,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make gridview column visible true and false according user select column name from checkbox list and deselect.My code is as follow

this code is on button click event:
C#
for (int i = 0; i <= chklstSelectCol.Items.Count - 1; i++)
       {
           if (chklstSelectCol.Items[i].Selected == false)
               grvadhocrpt.Columns[i].Visible = false;
           else
               grvadhocrpt.Columns[i].Visible = true;
       }
grvadhocrpt.DataBind();



if i tried to make visible false column of gridview when first assigning datasource
this do give result but after that it won't

please help,
vipul
Posted
Updated 18-Sep-11 20:15pm
v2

You first bind the grid and then write the code to make grid column visible false
it will work.
 
Share this answer
 
v2
Comments
vipul ghadge 19-Sep-11 2:32am    
thanks for rply,

actually , i have tried that also,
i have even tried to make each row's cell visible false on rowdatabound event it still not working
P.Salini 19-Sep-11 2:45am    
Are you getting any error.
vipul ghadge 19-Sep-11 3:05am    
nope ,m not getting any error.
C#
grvadhocrpt.DataSource = ds.Tables[0];
grvadhocrpt.DataBind();
for (int i = 0; i <= chklstSelectCol.Items.Count - 1; i++)
{
if (chklstSelectCol.Items[i].Selected == false)
grvadhocrpt.Columns[2].Visible = false;// specify coloumn no. instead of i
else
grvadhocrpt.Columns[2].Visible = true;
}
 
Share this answer
 
Comments
vipul ghadge 19-Sep-11 3:13am    
this works only one time when i first assigning datasorce to gridview,
actly i have to use 'i' there instead of col. no. as user will select column name from chekbox list which one to display and which one not,please tell me guys if you have other way to do this.
Anuja Pawar Indore 19-Sep-11 4:12am    
are you writing this code in grvadhocrpt_RowDataBound or some where else
vipul ghadge 19-Sep-11 4:27am    
i am writing this code in one function ,
and calling this function on pageload event(am also doing ispostback check) and on button event.just to test i have as well written this on rowdatabound and then removed.
Anuja Pawar Indore 19-Sep-11 4:54am    
Vipul you better post your code, then only can help you. As it should work, syntatically its correct
vipul ghadge 19-Sep-11 6:55am    
Hey ,appreciate your help
Hi,
you need to set
AutoGenerateColumns="false"
to your gridview initialization code of aspx page. did you set this property.

also refer:
Using the AutoGenerateColumns attribute to control column rendering[^]
http://stackoverflow.com/questions/114521/hide-asp-net-gridview-row[^]
 
Share this answer
 
Comments
vipul ghadge 19-Sep-11 2:58am    
Yes ,I Have set that property
earlier my code was on <asp:Button
and now i place code this code on one <asp:ImageButton it giving expected result ,I think problem may be that of button which i have place in panel,this panel I am using like that of chat box on FB.And that imagebutton i am using is on aspx page,my function is finely working on this image button.

this time i have use rowcreated event instead of rowdatabound event and writen this

C#
GridView grvAd = (GridView)sender;
        if (e.Row.RowType == DataControlRowType.Header)
        {
            for (int i = 0; i <= chklstSelectCol.Items.Count - 1; i++)
            {
                if (chklstSelectCol.Items[i].Selected == false)
                    grvAd.Columns[i].Visible =false;
                else
                {
                    grvAd.Columns[i].HeaderStyle.CssClass = "";
                    grvAd.Columns[i].Visible = true;
                }
            }

        }


thank you guys,truly appreciate your help.
 
Share this answer
 
v3

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