Link to home
Start Free TrialLog in
Avatar of rito1
rito1

asked on

Specifying Gridview Row Cells

Hi All,

Say I have the following RowDataBound event..

Rather than specifying the cell using it's ordinal value is there a more specific way that won't mean that I need to check/ update this event every time a new column is added to the Gridview control?

Many thanks,

Rit
Sub ProductGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
 
    If e.Row.RowType = DataControlRowType.DataRow Then
    
	Dim myQty as Integer = CInt(e.Row.Cells(1).Text)
 
	If myQty > 0 then
		e.Row.Cells(3).Text = "In Stock"
	Else
		e.Row.Cells(3).Text = "Not In Stock"
	End If 
       
    End If
    
End Sub

Open in new window

Avatar of ajolly
ajolly
Flag of India image

I use C#, so I am writing solution in C#.

One part I have tried to keep in VB.Net which was copied from yours.
// C# Code
Private int QuantityColumnIndex;
Private int AvailabilityColumnIndex;
 
Page_Load()
{
    for(int i = 0; i < ProductGridView.Columns.Count; i++)
    {
        if (ProductGridView.Columns[i].HeaderText == "Quantity")
        {
                QuantityColumnIndex = i;
        }
        if (ProductGridView.Columns[i].HeaderText == "Availability")
        {
                AvailabilityColumnIndex = i;
        }
    }
}
 
// VB.Net Code
Sub ProductGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
 
    If e.Row.RowType = DataControlRowType.DataRow Then
    
        Dim myQty as Integer = CInt(e.Row.Cells(QuantityColumnIndex).Text)
 
        If myQty > 0 then
                e.Row.Cells(AvailabilityColumnIndex).Text = "In Stock"
        Else
                e.Row.Cells(AvailabilityColumnIndex).Text = "Not In Stock"
        End If 
       
    End If
    
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

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 rito1
rito1

ASKER

Thanks both,
prairiedog, this is exactly what I was looking for.

Rit