Hide column in gridView

I had a dataset that returns the following CategoryDI, CategoryName, CateoryPicture im data displaying this data using gridview, however I want to display only the category Name hide categoryid, and CategoryPicture im uinsg the following code, but it does not work, please help me thank you

GridView1.DataSource = getDataSet.Tables (0)

GridView1.DataBind() GridView1.Columns(0).Visible = False GridView1.Columns(1).Visible = true GridView1.Columns(2).Visible = False 
+4
source share
6 answers
  protected void gvDocuments_RowDataBound(object sender, GridViewRowEventArgs e) { e.Row.Cells[1].Visible = false; } 
+3
source
  Me.GridView1.DataSource = ds6 Me.GridView1.DataBind() If GridView1.Columns.Count > 0 Then Me.GridView1.Columns(0).Visible = False Me.GridView1.Columns(9).Visible = False Me.GridView1.Columns(2).Visible = False Me.GridView1.Columns(3).Visible = False Else GridView1.HeaderRow.Cells(0).Visible = False GridView1.HeaderRow.Cells(9).Visible = False GridView1.HeaderRow.Cells(2).Visible = False GridView1.HeaderRow.Cells(3).Visible = False For Each gvr In GridView1.Rows gvr.Cells(0).Visible = False gvr.Cells(9).Visible = False gvr.Cells(2).Visible = False gvr.Cells(3).Visible = False Next End If 
+3
source

on the aspx page, make sure you define the gridview with "AutogenerateColumns = False" and then specify the columns you want to see.

eg:

  <asp:GridView ID="mygrid" runat="server" AutoGenerateColumns="False" CellPadding="4" GridLines="Horizontal" Width="800px" ShowFooter="False" CssClass="grid" AlternatingRowStyle-CssClass="alt"> <Columns> <asp:BoundField HeaderText="Name" DataField="CategoryName"></asp:BoundField> <asp:BoundField HeaderText="Picture" DataField="CategoryPciture"></asp:BoundField> </Columns> <asp:GridView> 

This will only show the columns that you want to see.

in codebebind, just do

  mygrid.Datasource= xxx 

and then

  mygrid.databind 

which will do the trick.

+1
source
 .DisplayNone { Display:none } 

Then put the following before the “BoundField” tag for each of the columns that you want to make invisible:

 <HeaderStyle CssClass="DisplayNone"></HeaderStyle> <ItemStyle CssClass="DisplayNone"></ItemStyle> 

are displayed as if you had specified "Visible = False", but it will be available to your code. Keep in mind that it will appear if you “browse the source” from the browser, so do not use this technique if the hidden data is sensitive.

+1
source

simple code example

  Me.DataGridView1.Columns("CategoryID").Visible = False Me.DataGridView1.Columns("CategoryName").Visible = False Me.DataGridView1.Columns("CateoryPicture").Visible = False 

Sincerely.

0
source

but he gave me the following

 Conversion from string "Branch" to type 'Integer' is not valid. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Response.Cache.SetCacheability(HttpCacheability.NoCache) setMenu() Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache) Response.Cache.SetNoStore() lbldte.Text = DateAndTime.Now.ToShortDateString If Page.PreviousPage IsNot Nothing Then Dim GridView1 As GridView = DirectCast(Page.PreviousPage.FindControl("GridView1"), GridView) End If If Session("ProductsTable") IsNot Nothing Then dt = DirectCast(Session("ProductsTable"), DataTable) GridView1.DataSource = dt GridView1.DataBind() If GridView1.Columns.Count > 0 Then Me.GridView1.Columns("Branch").Visible = False Me.GridView1.Columns("Visit date").Visible = False Me.GridView1.Columns("Emp.Age").Visible = False End If End If If Not IsPostBack Then 'check if the webpage is loaded for the first time. 'Saves the Previous page url in ViewState ViewState("PreviousPage") = Request.UrlReferrer End If End Sub 
0
source

All Articles