当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - GridView css Clashing with BoundField css

问题描述:

In my GridView I am using w3schools css. Inside the grid view, I want to hide one column (BoundField) but not by setting the "Visible = false" as I want to access that columns data.

For the BoundField (The column I want to hide) I am using css to hide it by giving "" and the HeaderStyle-cssClass="hidden".

When I apply the w3schools css in the grid view it overrides the css in the BoundField.

If I remove the w3schools css then the BoundField css works fine but I have lost the GridView design.

How can I make both the css work together? If not possible then how can i hide that column without using "Visible = false".

Giving the code below.

In the head section:

<script type="text/css">

.hiddenGV

{

display:none;

}

</script>

In the body section

`<asp:GridView ID="GridView1" runat="server" class="w3-table w3-striped w3-bordered w3-card-4 gridtop" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False" DataKeyNames="Project_ID,Instance_ID" DataSourceID="SqlDataSource1">

<Columns>

<asp:BoundField DataField="Project_ID" HeaderText="Project_ID" ReadOnly="True" SortExpression="Project_ID" HeaderStyle-CssClass="hiddenGV" ><ItemStyle CssClass="hiddenGV"/></asp:BoundField>

`

网友答案:

A couple of thing wrong here. Your css class in the head is incorrect in that it should be in a style tag. In the BoundField you do not need to add an ItemStyle tag because ItemStyle is a property of BoundField. To hide the column and still be able to access the value, use this css in the head

   <style type="text/css">
      .hiddenGV
      {
        visibility :hidden;
      }
    </style>

Then in the BoundField set the css for the header and data rows like this:

  <asp:BoundField DataField="Project_ID" HeaderText="Project_ID" ReadOnly="True" SortExpression="Project_ID" ItemStyle-CssClass="hiddenGV"  HeaderStyle-CssClass="hiddenGV"/></asp:BoundField>
网友答案:

I solved the issue by writing the code behind instead of css.

I used the OnRowCreated property function of the GridView and selected the row by using

e.Row.Cells[0].Visible = false;

With this the column is also hidden and I can still access it in the code behind.

分享给朋友:
您可能感兴趣的文章:
随机阅读: