Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Search a gridview and change the item color if found in a query result

Posted on 2007-12-06
9
1,194 Views
Last Modified: 2008-02-01
I have been struggling with this and am frustrated that it isn't easier (or I am not smarter).
What I am trying to accomplish is I have a page of fixed items that I want to vary the color if they show up in a SQL query. I have tried a datalist and now using several gridviews to post the data into.

For example:
SHELF1        SHELF2        SHELF3            <--- These are individual gridviews (shelf1, shelf2, etc)
ABC               DEF              GHI
JKL                 MNO            QRP

When I click a button I will execute a SQL query to a database and if any of the results contain an item in any of the gridviews it will change the item's cell color so as to indicate a match. So I need to loop through each gridview and if there is a match then color, if not then move to the next gridview.

This sounds so simple but not sure if the way I have laid it out will work. I had to break this down into multiple gridviews since I do not want a single column with over 60 rows. And when I tried a dataview someone told me I could not change the item color.

This is webforms, VB 2005

Help! Please?
0
Comment
Question by:edrz01
  • 4
  • 3
  • 2
9 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 20419171
See if this helps..

http://www.netomatix.com/development/gridviewformatcolumn.aspx

There are other articles on data grid and view as well.
0
 

Author Comment

by:edrz01
ID: 20419239
naveenkohli,

Thanks - I will see if I can decode the C# into vb and let you know.
0
 
LVL 9

Expert Comment

by:divinewind80
ID: 20419669
It should be possible using the OnRowDataBound event of the gridview.  I didn't check that link naveenkohli gave, but I can assist if you are going to do it this way.  In VB too...
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 23

Expert Comment

by:naveenkohli
ID: 20419778
The link gives you the idea on how to modify behavior in OnRowCreated event. you should be able to wrie your own solution in VB.Net on similar lines. If you run into any issue feel free to let me know. I can try to wing some VB code :-)
0
 

Author Comment

by:edrz01
ID: 20420229
Thanks both, I found a site that converted the C# to VB. What it provided is listed below:
----------------------------------------------------------------------------------
    Protected Sub OnRowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim drv As Data.DataRowView = TryCast(e.Row.DataItem, Data.DataRowView)
            Dim ob As Object = drv("Weight")
            If Not Convert.IsDBNull(ob) Then
                Dim dVal As Double = 0.0F
                If [Double].TryParse(ob.ToString(), dVal) Then
                    If dVal > 3.0F Then
                        Dim cell As TableCell = e.Row.Cells(1)
                        cell.CssClass = "heavyrow"
                        cell.BackColor = System.Drawing.Color.Orange
                    End If
                End If
            End If
        End If
    End Sub
----------------------------------------------------------------------------------
What I needed is code which will allow me to search each gridview and compare to the returned values provided by the SQL query. Then if a match is made it will change the background color. In the sample provided by the link it looks like it is looking for a hard coded value.
0
 
LVL 9

Expert Comment

by:divinewind80
ID: 20420455
I would suggest, perhaps, a query to your db in the page load and set this as a dataset.  You should be able to reference this dataset outside of that sub if you declare it in the class itself, rather than in the sub.  Then, in your OnRowCreated sub, you can cycle through the dataset for each row/cell that you want and perform your highlighting then.

Does that make sense?
0
 

Author Comment

by:edrz01
ID: 20421780
I am hitting a wall.....
In the example I provided how would I search for, say,  'MNO' which resides under the SHELF2 gridview?
In this example I want to search through all of the rows in SHELF2 and when I find the cell or row that contains 'MNO' do something with it.
If you could provide code it would help......
0
 
LVL 9

Accepted Solution

by:
divinewind80 earned 500 total points
ID: 20421887
Dim ob As Object = drv("SHELF2")

You should be able to reference the contents of the cell by ob.tostring directly... for instance:

If ob.tostring = "MNO" then
Do something here...
End if

Does that help?
0
 

Author Comment

by:edrz01
ID: 20422315
All,
Well I must be well off the beaten path. Either what I am trying to do is not possible or I am not explain it very well.....
When my page loads it populates the various gridviews with data from an Access source (so I can't use the above in a Page load). I have to wait until the data is populated in the gridviews before I do the next step which is to execute another query against another source and if that query returns data to compare against all of the gridviews and if it finds a match to change the background color.
So  the comparison must
1) compare the current gridview items against the results of a query
2) step through each gridview and look at the values in each row, cell
3) change the background color if it has a match

If the gridview is not the way to go - let me know......
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question