Link to home
Start Free TrialLog in
Avatar of directxBOB
directxBOBFlag for Ireland

asked on

Searching a GridView?

I have a Gridview which is bound to a datasource.

I want to check that certain columns within that Gridview are empty. I don't want to have to rely on indexes to find the right column as the DBA has a habit of changing certain tables so I would prefer to have something that would work irrespective of what he does in the background.

If it were a DatabTable I could do :

        For Each row As DataRow In priceBandTable.Rows
            If row.Item("ProductId") = cbProduct.SelectedValue Then
                If (row.Item(ComboBox1.Text) = ComboBox1.Text Then
                    lblmessage.Text = "Error : This Price Band is already set."
                    Exit Sub
                End If
            End If
        Next


As with a DataTable I can pull out the exact columns based on names (Column Names will not change, just more will be added, and they may be reordered)

So I've been trying to get something like:

        For Each row As GridViewRow In Me.GridView2.Rows
            If row.Cells(0).Text = cbProduct.SelectedValue Then
                'search the columns.
                For c As Integer = 0 To row.Cells.Count
                    If  row.Cells(c)????? = ComboBox1.Text Then
                        lblmessage.Text = "Error : This Price Band is already set."
                        Exit Sub
                    End If
                Next c
            End If
        Next

But as you can see I'm stuck with this line, as I have no idea howto get a particular cells Name.

row.Cells(c)????? = ComboBox1.Text

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Joruus10
Joruus10

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of directxBOB

ASKER

Doesn't appear to be a Value field. Also I believe if there was a value field (row.Cells(c).Text) it would return me the actual value of the Cell, not the Column name for the cell.

?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But I am not after the cell text, what I am after is the Column Name.

Basically I know that the 1st cell of my Table will always be ProductID. So I am matching the row using this value.

I then need to find out what price applies to this product so I am using the the column name to make sure I am comparing the same columns values. Once I know that I have the correct column, I will either do an insert or an update to the database depending on whether or not sufficient information is there for either case.

But the problem I am having with the gridview is trying to establish what the column name is.
I ended up using the database.