Hello-World
Hello-World

Reputation: 9555

how to filter by id to gridview but not show it

In the following gridview I dont want to show the "ID" but I need to filter by it. How do I do that? You will see that I am trying to filter with an "IF" but this is not working 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

End Class

Upvotes: 1

Views: 498

Answers (2)

Bert te Velde
Bert te Velde

Reputation: 853

Visibility of any Column of the DataGridView can be set explicitly. After setting the datasource property of the grid, you have access to the columns:

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

For the filter, you may want to redefine your bindings-setup:

for a datagridview, you now use the table as datasource. (In fact, you're using (behind the 'screens') the DEFAULT dataview on the table.)

More flexible and clear is to use:

a) an explicit bindingsource, and

b) an explicit dataview,

In de view you can set your filter as you like (and change it runtime), like so (assuming you have already your table populated):

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

and then again:

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

Runtime you can modify the filter (and/or the sort) property of the DataView. This will be reflected by the bindingsource to what the grid shows.

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

Btw: you can set the dataview directly as datasource for the grid.

Upvotes: 1

Pilgerstorfer Franz
Pilgerstorfer Franz

Reputation: 8359

This code worked for me...

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 I would suggest having a look at dataBinding in WebForms (and dataBoundControls). For example you could set boundFields in markup.

Upvotes: 1

Related Questions