Solved

Specifying Gridview Row Cells

Posted on 2008-09-30
3
465 Views
Last Modified: 2008-10-01
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

0
Comment
Question by:rito1
[X]
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
3 Comments
 
LVL 7

Expert Comment

by:ajolly
ID: 22605148
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

0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 22607497
1. Convert the Quantity and Availability BoundFields into a TemplateFields
2. In your code, do something like this:
 

Sub ProductGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
 
    If e.Row.RowType = DataControlRowType.DataRow Then
    
		Dim lbl As Label = CType(e.Row.FindControl("lblQuantity"), Label 'Change lblQuantity to your Label's name
		Dim lbl2 As Label = CType(e.Row.FindControl("lblAvailability"), Label) 'Change the name if needed
		
		If (lbl IsNot Nothing And lbl2 IsNot Nothing) Then		
	        Dim myQty as Integer = CInt(lbl.Text)	 
	        If myQty > 0 then
	                lbl2.Text = "In Stock"
	        Else
	                lbl2.Text = "Not In Stock"
	        End If 
		End If
       
    End If
    
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:rito1
ID: 22614110
Thanks both,
prairiedog, this is exactly what I was looking for.

Rit
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS 2015 is giving [No relevant source lines] 2 49
Why use this lambda? 12 98
Header Font Size in Grid View 6 51
How do i create a simple array in a public class? 6 38
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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.

710 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