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

后端 未结 13 1133
粉色の甜心
粉色の甜心 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:09

    Here is how to get the value of a hidden column in a GridView that is set to Visible=False: add the data field in this case SpecialInstructions to the DataKeyNames property of the bound GridView , and access it this way.

    txtSpcInst.Text = GridView2.DataKeys(GridView2.SelectedIndex).Values("SpecialInstructions")
    

    That's it, it works every time very simple.

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

    I have a new solution hide the column on client side using css or javascript or jquery.

    .col0
    {
      display: none;
    }
    

    And in the aspx file where you add the column you should specify the CSS properties:

    <asp:BoundField HeaderText="Address Key" DataField="Address_Id" ItemStyle-CssClass="col0" HeaderStyle-CssClass="col0" > </asp:BoundField>
    
    0 讨论(0)
  • 2020-11-27 18:21

    You can make the column hidden on the server side and for some reason this is different to doing it the aspx code. It can still be referenced as if it was visible. Just add this code to your OnDataBound event.

    protected void gvSearchResults_DataBound(object sender, EventArgs e)
    {
        GridView gridView = (GridView)sender;
    
        if (gridView.HeaderRow != null && gridView.HeaderRow.Cells.Count > 0)
        {
            gridView.HeaderRow.Cells[UserIdColumnIndex].Visible = false;
        }
    
        foreach (GridViewRow row in gvSearchResults.Rows)
        {
            row.Cells[UserIdColumnIndex].Visible = false;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 18:22

    You can use DataKeys for retrieving the value of such fields, because (as you said) when you set a normal BoundField as visible false you cannot get their value.

    In the .aspx file set the GridView property

    DataKeyNames = "Outlook_ID"
    

    Now, in an event handler you can access the value of this key like so:

    grid.DataKeys[rowIndex]["Outlook_ID"]
    

    This will give you the id at the specified rowindex of the grid.

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

    You can do it programmatically:

    grid0.Columns[0].Visible = true;
    grid0.DataSource = dt;
    grid0.DataBind();
    grid0.Columns[0].Visible = false;
    

    In this way you set the column to visible before databinding, so the column is generated. The you set the column to not visible, so it is not displayed.

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

    If I am not mistaken, GridView does not hold the values of BoundColumns that have the attribute visible="false". Two things you may do here, one (as explained in the answer from V4Vendetta) to use Datakeys. Or you can change your BoundColumn to a TemplateField. And in the ItemTemplate, add a control like Label, make its visibility false and give your value to that Label.

    0 讨论(0)
提交回复
热议问题