Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Specifying Gridview Row Cells

Posted on 2008-09-30
3
Medium Priority
?
470 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
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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

916 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