2014-10-06 87 views
0

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

我的目标是使用文本框在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

添加此行

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

代码背后

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

+0

未将对象引用设置为对象的实例。突出显示的行是如果不是String.Equals(txtAdmDate.Text,Row.Cells(0).Text)然后 – Dale 2014-10-06 14:39:41

+0

行“Row = GridView1.SelectedItem”会产生错误?因为我没有选择任何物品。 – Dale 2014-10-06 14:58:11

+0

@Dale您的txtAdmDate在哪里?如果它是外部DataGrid那么你可以检查我更新的代码 – 2014-10-06 16:35:58