Click here to Skip to main content
15,888,022 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to remove a complete row from a GridView if it satisfied a condition.

Here I have got
VB
a Description Column from Product table in database 
with which a GridView is populated. I set the sqlcommand in my vb app to only
VB
select the Description which does not contain the String s
. Here
C#
String s has two keywords "Tomatoes" and "Apple"
.

So the Sql Query should retrieve the
VB
Description that does not have "Tomatoes" and "Apple"
and therefore the Gridview should be updated by removing the rows which satisfy the condition.

I am facing a difficulty in removing the
VB
Description row in GridView
which has "Tomatoes" and "Apple". I tried populating another GridView with the results, but it does not appear in the web page despite the fact that everything is correct because I have seen the values where I specified some Breakpoints. Any suggestions or thoughts?

Here is my code:


VB
Dim cmd1 = New SqlCommand(" SELECT DISTINCT [Description] FROM [Product] WHERE ([Description] LIKE '%' + @Description + '%')", conn)
                        cmd1.Parameters.AddWithValue("@Description", s)

                        MsgBox("NO")

                        For i As Integer = 0 To GridView1.Rows.Count - 1
                            DA.SelectCommand = cmd1
                            DA.Fill(dt)
                           'row is of a GridViewRow datatype As GridView1.Rows
                            row.Cells.RemoveAt(i) '' do not know if it is correct or not
                            '  GridView3.DataSource = '' dt tried with no luck
                            ' GridView3.DataBind() '' tried with no luck
                            cmd1.Dispose()
                            DA.Dispose()
                            dt.Clear()
                            dt.Dispose()
                        Next
                    End If
Posted

1 solution

You can treat it by Query, no need to get into code.
Something like:
SQL
SELECT DISTINCT [Description] FROM [Product] WHERE ([Description] LIKE '%' + @ Description + '%') And ([Description] Not Like '%s%')
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900