I am trying to hide a TD named ImportnatInfo in a table when the gridview is empty. This gridview has one column from a table in a database to show. When this gridview is empty I want to hide the TD.
The following is the asp code:
<td runat ="server" ID="ImportnatInfo"
style="width: inherit; border: 5px double #585858; padding-left: 5px; padding-right: 5px;
height: inherit; background: #FFFFFF; background-position: center; border-radius: 25px;"
enableviewstate="True" visible="False">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="None"
DataSourceID="ImportantSqlDataSource">
<Columns>
<asp:BoundField DataField="Importatnat_Info" SortExpression="Importatnat_Info">
<ControlStyle BorderStyle="None" Height="10px" />
<FooterStyle BorderStyle="None" Height="10px" />
<HeaderStyle BorderStyle="None" Height="10px" />
<ItemStyle BorderStyle="None" Height="10px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="ImportantSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:AgainConnectionString %>"
SelectCommand="SELECT [Importatnat_Info] FROM [StationInfoTable] WHERE ([StationNo] = @StationNo)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlStationNames" Name="StationNo" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<br />
</td>
And the VB.net code is the following:
If GridView1.Rows.Count = 0 Then
ImportnatInfo.Visible = False
Else
ImportnatInfo.Visible = True
End If
I have dropdownlist and i choose something everytime so if there is nothing in the grid to view, i want the whole TD to be invisible. the TD that im trying to hide when it is empty, the thing the TD is always visible because when there is nothing in the gridView Vb add so it won't be empty. so i thought i should use the datasource and still not sure what to do. Any idea how to do that?
Check the type of row (via row.RowType
); it's probably not a data row, but an empty row or header row or something else. So you need to make sure rows with an ItemType of Item or AlternateItem are the type you are counting. You can see more detailed information about this property here .
EDIT: You can use LINQ to filter the results of the appropriate type:
Dim cnt = GridView1.Rows.Where( _
Function(i) i.RowType = DataControlRowType.Item OrElse _
i.RowType = DataControlRowType.AlternateItem).Count()
If cnt = 0 Then
ImportnatInfo.Visible = False
Else
ImportnatInfo.Visible = True
End If
Try hooking into the Selected event of your data source
<asp:SqlDataSource ID="ImportantSqlDataSource" OnSelected="SqlDataSource1_Selected" runat="server" ConnectionString="<%$ ConnectionStrings:AgainConnectionString %>"
SelectCommand="SELECT [Importatnat_Info] FROM [StationInfoTable] WHERE ([StationNo] = @StationNo)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlStationNames" Name="StationNo" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
Note OnSelected="SqlDataSource1_Selected"
Then in your code behind
Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected
ImportnatInfo.Visible = e.AffectedRows > 0
End Sub
Your gridview may have rows even if no data is returned: headers, footers and/or a now data found message.
UPDATE
Also try the following
Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected
ImportnatInfo.Visible = e.AffectedRows > 0
Gridview1.Visible = e.AffectedRows > 0
End Sub
This should also set the gridview to not show when there are no rows, regardless of the visibility of the td
. I would also try moving your datasource outside of the td
it shouldn't make a difference, but it may.
If after trying this if the gridview is hidden, but the td remains visible there may be something happening later in the page life cycle that is affecting the visibility of the td.
GridView1.DataBind()
ImportantInfo.Visible = True
If GridView1.Rows.Count = 1 Then
If GridView1.Rows(0).Cells.Count = 1 Then
If GridView1.Rows(0).Cells(0).Text.Trim() = " " Then
ImportantInfo.Visible = False
End If
End If
Else
ImportantInfo.Visible = False
End If
ASP.NET always leave a space when there is nothing in the GridView. So, I check if it is empty or not if not i check if it is the space or the GridView has some data.
GridView1.DataBind()
ImportnatInfo.Visible = True
If GridView1.Rows.Count >= 1 Then
If GridView1.Rows(0).Cells.Count >= 1 Then
If GridView1.Rows(0).Cells(0).Text.Trim() = " " Then
ImportnatInfo.Visible = False
Else
ImportnatInfo.Visible = True
End If
End If
Else
ImportnatInfo.Visible = False
End If
Yours is working good but you just need to add Else to show the TD after you check it is not
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.