如何通过id过滤到gridview但不显示它

时间:2012-09-07 16:49:32

标签: asp.net vb.net gridview

在下面的gridview中我不想显示“ID”,但我需要按它过滤。我怎么做?您会看到我尝试使用“IF”进行过滤,但这不起作用e.Row.Cells("ID").Text = "21"

Imports System.Data

Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim table2 As New DataTable
    ' Create four typed columns in the DataTable.
    table2.Columns.Add("ID", GetType(Integer))
    table2.Columns.Add("Drug", GetType(String))
    table2.Columns.Add("Patient", GetType(String))
    table2.Columns.Add("Date", GetType(DateTime))
    ' Add five rows with those columns filled in the DataTable.
    table2.Rows.Add(25, "Indocin", "David", DateTime.Now)
    table2.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
    table2.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
    table2.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
    table2.Rows.Add(1100, "Dilantin", "Melanie", DateTime.Now)
    table2.Rows.Add(125, "Indocin", "David", DateTime.Now)
    table2.Rows.Add(150, "Enebrel", "Sam", DateTime.Now)
    table2.Rows.Add(110, "Hydralazine", "Christoff", DateTime.Now)

    GridView1.DataSource = table2


    Dim s_Patient As BoundField = New BoundField
    s_Patient.DataField = "Patient"
    s_Patient.HeaderText = "Patient"

    Dim s_Drug As BoundField = New BoundField
    s_Drug.DataField = "Drug"
    s_Drug.HeaderText = "Drug"

    GridView1.Columns.Clear()
    GridView1.Columns.Add(s_Patient)
    GridView1.Columns.Add(s_Drug)


    GridView1.AutoGenerateColumns = False

    GridView1.DataBind()

End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow AndAlso (e.Row.Cells("ID").Text = "21" OrElse e.Row.Cells("ID").Text = "150") Then

        e.Row.BackColor = Drawing.Color.Blue

    End If

End Sub

结束班

2 个答案:

答案 0 :(得分:1)

此代码为我工作......

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' only bind gridViewData once!
    If Not Page.IsPostBack Then
        ' INIT your table

        Dim s_Patient As BoundField = New BoundField
        s_Patient.DataField = "Patient"
        s_Patient.HeaderText = "Patient"
        Dim s_Drug As BoundField = New BoundField
        s_Drug.DataField = "Drug"
        s_Drug.HeaderText = "Drug"

        ' only for option 2
        GridView1.DataKeyNames = New String() {"ID"}

        GridView1.Columns.Add(s_Patient)
        GridView1.Columns.Add(s_Drug)
        GridView1.AutoGenerateColumns = False

        GridView1.DataSource = table2
        GridView1.DataBind()
    End If
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        ' option one: check the underlying dataRowView
        Dim row As DataRowView = CType(e.Row.DataItem, DataRowView)
        If row("ID") = "21" OrElse row("ID") = "150" Then
            e.Row.BackColor = Drawing.Color.Blue
        End If

        ' option two: check according to the current DataItemdIndex 
        '             (ie what's the dataKey, in this case ID, of the fifth element)
        Dim id As String = GridView1.DataKeys(e.Row.DataItemIndex).Value
        If id = "21" OrElse id = "150" Then
            e.Row.BackColor = Drawing.Color.Brown
        End If
    End If
End Sub

Furthemrore我建议看看dataBinding in WebForms(和dataBoundControls)。例如,您可以在标记中设置boundFields

答案 1 :(得分:1)

可以显式设置DataGridView的任何列的可见性。 设置网格的datasource属性后,您可以访问列:

Dim gridViewColumn as datagridviewcolumn = GridView1.Columns("ID")
gridViewColumn.Visible = False

对于过滤器,您可能需要重新定义绑定设置:

对于datagridview,您现在将该表用作数据源。 (事实上​​,你正在使用(在'屏幕'后面)表格上的DEFAULT数据视图。)

使用更灵活,更清晰:

a)显式绑定源和

b)显式数据视图,

在视图中,您可以根据需要设置过滤器(并更改运行时), 像这样(假设您已经填充了表格):

dim mySort As Sting = ""
dim mySort As Sting = ""
dim mySort As Sting = ""
dim myFilter as String = "ID>23"
dim myDataView as new DataView(table2, myFilter, mySort, DataViewRowState.Currentrows)
dim myBindingSource as new BindingSource
myBindingSource.DataSource = myDataView
DataGridView1.DataSource = myBindingSource

然后再次:

DataGridview1.Columns("ID").Visible = False

运行时,您可以修改DataView的过滤器(和/或排序)属性。这将通过绑定源反映到网格显示的内容。

myDataView.Filter = "ID>24 AND ID<27"

顺便说一下:您可以直接将数据视图设置为网格的数据源。