Tuesday, 15 July 2014

asp.net - gridview hide columns -



asp.net - gridview hide columns -

this question has reply here:

gridview hide column code 12 answers

i have gridview , want hide column after databind gridview error below.

"index out of range. must non-negative , less size of collection. parameter name: index"

my c# code below,

protected void grid_all_posts_databound(object sender, eventargs e) { if (grid_all_posts.columns[1].visible) { grid_all_posts.columns[1].visible = false; } } // read posts , fill gridview ////////////////////////////////////////////////// dbcommand dbcommand2; dbcommand2 = db.getstoredproccommand("sp_select_news"); db.addinparameter(dbcommand2, "userid", dbtype.guid, new guid(session["userid"].tostring().trim())); dataset ds = db.executedataset(dbcommand2); grid_all_posts.datasource = ds; grid_all_posts.databind(); //////////////////////////////////////////////////

my aspx code,

<asp:gridview runat="server" id="grid_all_posts" onrowdatabound="grid_all_posts_databound"></asp:gridview>

what think problem is? how can hide first column

try below work....

dbcommand dbcommand2; dbcommand2 = db.getstoredproccommand("sp_select_news"); db.addinparameter(dbcommand2, "userid", dbtype.guid, new guid(session["userid"].tostring().trim())); dataset ds = db.executedataset(dbcommand2); grid_all_posts.datasource = ds; grid_all_posts.databind(); **//after databind write below code** if (grid_all_posts.columns.count > 0) grid_all_posts.columns[0].visible = false; else { grid_all_posts.headerrow.cells[0].visible = false; foreach (gridviewrow gvr in grid_all_posts.rows) { gvr.cells[0].visible = false; } }

asp.net gridview

No comments:

Post a Comment