Click here to Skip to main content
11,415,711 members (85,774 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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:
 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 18-Sep-11 21:12pm
Edited 18-Sep-11 21:15pm
m@dhu29.6K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You first bind the grid and then write the code to make grid column visible false
it will work.
  Permalink  
v2
Comments
vipul ghadge at 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
Salini.P at 19-Sep-11 2:45am
   
Are you getting any error.
vipul ghadge at 19-Sep-11 3:05am
   
nope ,m not getting any error.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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;
}
  Permalink  
Comments
vipul ghadge at 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 at 19-Sep-11 4:12am
   
are you writing this code in grvadhocrpt_RowDataBound or some where else
vipul ghadge at 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 at 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 at 19-Sep-11 6:55am
   
Hey ,appreciate your help
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
Comments
vipul ghadge at 19-Sep-11 2:58am
   
Yes ,I Have set that property
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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

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.
  Permalink  
v3

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,270
1 OriginalGriff 7,512
2 Maciej Los 3,710
3 Abhinav S 3,325
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web02 | 2.8.150427.4 | Last Updated 19 Sep 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100