使用VB隐藏DataGrid asp.net中的特定行

时间:2014-10-06 09:00:13

标签: asp.net vb.net datagrid

对于任何人来说这似乎都很容易,但我无法使用vb搜索如何在asp.net中隐藏DataGrid(Not GridView)中的特定行。当我在搜索时,我只看到了如何使用DataGrid1.Columns(0)隐藏列.Visible = False。我尝试使用ItemDataBound事件隐藏它,但它隐藏了整个列及其headercolumntext。

我的目标是使用Date = textboxdate.text的文本框搜索数据。这在sql中很容易做,但我不能修改查询,因为它在存储过程中。

这是我目前的代码:

 If txtAdmDate.Text <> "" Then

        If Not String.Equals(txtAdmDate.Text, e.Item.Cells(0).Text) Then
            e.Item.Cells(0).Visible = False
        End If

    End If

我想做这样的事情。

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
    If txtAdmDate.Text <> "" Then
        If DataGrid1.Row(0).text <> txtAdmDate.Text Then
            DataGrid1.Row(0).Visible = False
        End If
    End If
End Sub

ASPX页面:

Search By Date:
 <asp:TextBox ID="txtAdmDate" runat="server"></asp:TextBox>
 <asp:Button ID="btnSearch" runat="server" Text="Refresh / Search" />
<br />
<asp:DataGrid ID="DataGrid1" runat="server" CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False">
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <Columns>
        <asp:BoundColumn DataField="Admission Date" HeaderText="Admission Date"></asp:BoundColumn>
        <asp:BoundColumn DataField="Hospital #" HeaderText="Hosp. #"></asp:BoundColumn>
        <asp:BoundColumn DataField="Admission #" HeaderText="Reg. #"></asp:BoundColumn>
        <asp:BoundColumn DataField="Patient Name" HeaderText="Patient Name" Visible="false"></asp:BoundColumn>
        <asp:ButtonColumn DataTextField="Patient Name"  HeaderText="Patient Name" CommandName="Select"></asp:ButtonColumn>
        <asp:BoundColumn DataField="Discharged Date" HeaderText="Discharged Date"></asp:BoundColumn>
        <asp:BoundColumn DataField="Billing Date" HeaderText="Billing Date"></asp:BoundColumn>
    </Columns>
</asp:DataGrid>

1 个答案:

答案 0 :(得分:1)

添加此行

<asp:DataGrid ID="DataGrid1" runat="server" 
OnItemDataBound="DataGrid1_ItemDataBound"
 CellPadding="4" EnableModelValidation="True" 
ForeColor="#333333" GridLines="None" 
AutoGenerateColumns="False">

IN Code Behind

Protected Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
     Dim Row As DataGridItem
    Row = DataGrid1.Item

    ' Dim txbox As TextBox
    ' txbox = CType(Row.FindControl("txtbox"), TextBox)
    ' this for template fields Only

    'if you used BoundFields you can access like this

 If Not String.Equals(txtAdmDate.Text, Row.Cells(0).Text) Then
            Row.Visible = False
        End If


    End Sub 'Item_Bound 

' this will give you selected row 
' from this you can find controls on that row like text boxes,labels

Source