locked
matching gridview data in a column with control data RRS feed

  • Question

  • User-1474096950 posted

    suppose a textbox1.text ="India" gridview id binded with data . suppose column 10 of gridview contains country data comma seperated china, usa, india i need to display only that row if the textbox value doesnot match gridview column(10) data than that row should not be displayed

    Saturday, May 14, 2016 10:19 AM

Answers

  • User36583972 posted

    Hi svibuk,

    You can try the following way to implement your needs.

    HTML:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server">india</asp:TextBox>
                <br />
                <asp:GridView ID="GridView1"
                    runat="server" AutoGenerateColumns="false" >
                    <Columns>
                        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
                        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
                        <asp:ButtonField Text="Select" CommandName="btnSelectItems" />
                    </Columns>
    
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    
    ASPX.VB:
    
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Me.IsPostBack Then
                Dim dt As New DataTable()
                dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
                dt.Rows.Add(1, "John Hammond", "china,usa,india")
                dt.Rows.Add(2, "Mudassar Khan", "india")
                dt.Rows.Add(3, "Suzanne Mathews", "France,india")
                dt.Rows.Add(4, "Robert Schidner", "china")
                GridView1.DataSource = dt
                GridView1.DataBind()
    
            End If
        End Sub
    
    
        Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    
            If e.Row.RowType = DataControlRowType.DataRow Then
    
                Dim celltxt As String = e.Row.Cells(1).Text.Trim()
                Dim copmaretxt As String = TextBox1.Text.Trim()
    
                Dim x As New List(Of String)(celltxt.Split(","c))
                If (x.Contains(copmaretxt) = False) Then
                    e.Row.Visible = False
                End If
    
            End If
    
        End Sub
    


    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 16, 2016 7:37 AM

All replies

  • User36583972 posted

    Hi svibuk,

    You can try the following way to implement your needs.

    HTML:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server">india</asp:TextBox>
                <br />
                <asp:GridView ID="GridView1"
                    runat="server" AutoGenerateColumns="false" >
                    <Columns>
                        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
                        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
                        <asp:ButtonField Text="Select" CommandName="btnSelectItems" />
                    </Columns>
    
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    
    ASPX.VB:
    
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Me.IsPostBack Then
                Dim dt As New DataTable()
                dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
                dt.Rows.Add(1, "John Hammond", "china,usa,india")
                dt.Rows.Add(2, "Mudassar Khan", "india")
                dt.Rows.Add(3, "Suzanne Mathews", "France,india")
                dt.Rows.Add(4, "Robert Schidner", "china")
                GridView1.DataSource = dt
                GridView1.DataBind()
    
            End If
        End Sub
    
    
        Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    
            If e.Row.RowType = DataControlRowType.DataRow Then
    
                Dim celltxt As String = e.Row.Cells(1).Text.Trim()
                Dim copmaretxt As String = TextBox1.Text.Trim()
    
                Dim x As New List(Of String)(celltxt.Split(","c))
                If (x.Contains(copmaretxt) = False) Then
                    e.Row.Visible = False
                End If
    
            End If
    
        End Sub
    


    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 16, 2016 7:37 AM
  • User-1780421697 posted

    Get data source like a data table tblCountries which bind to gridview control.

    Copy the table in Cache,Session Or ViewState.

    When user enter India in textbox capture text changed event or there should be a button click event to capture.

    Get the data table from ViewState Or Session Or Cache, Filter the data table with query and bind it to gridview.

    You can make any query what you want on data table.

    CACHE,VIEWSTATE,SESSION make the table avaliable when ever need only underlying table is changed on filtering.

    Monday, May 16, 2016 7:43 AM