How to hide a column (GridView) but still access its value?

后端 未结 13 1134
粉色の甜心
粉色の甜心 2020-11-27 17:52

I have a GridView with a DataSource (SQL Database). I want to hide a column, but still be able to access the value when I select the record. Can someone show me

相关标签:
13条回答
  • 2020-11-27 18:27

    Define a style in css:

    .hiddencol { display: none; }
    

    Then add the ItemStyle-CssClass="hiddencol" and the HeaderStyle-CssClass="hiddencol" attribute to the grid field:

    <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-CssClass="hiddencol"  HeaderStyle-CssClass="hiddencol" ClientIDMode="Static" />
    
    0 讨论(0)
  • 2020-11-27 18:27

    I used a method similar to user496892:

    SPBoundField hiddenField = new SPBoundField();
    hiddenField.HeaderText = "Header";
    hiddenField.DataField = "DataFieldName";
    grid.Columns.Add(hiddenField);
    
    grid.DataSource = myDataSource;
    grid.DataBind();
    
    hiddenField.Visible = false;
    
    0 讨论(0)
  • 2020-11-27 18:29

    If you do have a TemplateField inside the columns of your GridView and you have, say, a control named blah bound to it. Then place the outlook_id as a HiddenField there like this:

    <asp:TemplateField HeaderText="OutlookID">
        <ItemTemplate>
            <asp:Label ID="blah" runat="server">Existing Control</asp:Label>
            <asp:HiddenField ID="HiddenOutlookID" runat="server" Value='<%#Eval("Outlook_ID") %>'/>
        </ItemTemplate>
    </asp:TemplateField>
    

    Now, grab the row in the event you want the outlook_id and then access the control.
    For RowDataBound access it like:

    string outlookid = ((HiddenField)e.Row.FindControl("HiddenOutlookID")).Value;
    

    Do get back, if you have trouble accessing the clicked row. And don't forget to mention the event at which you would like to access that.

    0 讨论(0)
  • 2020-11-27 18:34
    <head runat="server">
    <title>Accessing GridView Hidden Column value </title>
    <style type="text/css">
      .hiddencol
      {
        display: none;
      }
    </style>
    
    <asp:BoundField HeaderText="Email ID" DataField="EmailId" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" >
    </asp:BoundField>
    
    ArrayList EmailList = new ArrayList();
    foreach (GridViewRow itemrow in gvEmployeeDetails.Rows)
    {
      EmailList.Add(itemrow.Cells[YourIndex].Text);
    }
    
    0 讨论(0)
  • 2020-11-27 18:34

    You can do it code behind.

    Set visible= false for columns after data binding . After that you can again do visibility "true" in row_selection function from grid view .It will work!!

    0 讨论(0)
  • 2020-11-27 18:34

    When I want access some value from GridView before GridView was appears.

    1. I have a BoundField and bind DataField nomally.
    2. In RowDataBound event, I do some process in that event.
    3. Before GridView was appears I write this:

      protected void GridviewLecturer_PreRender(object sender, EventArgs e) 
      {
          GridviewLecturer.Columns[0].Visible = false;
      }
      
    0 讨论(0)
提交回复
热议问题