Solved

Specifying Gridview Row Cells

Posted on 2008-09-30
3
460 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I use this extension method? 8 47
ASP.Net Session State alternatives 3 61
ADO.NET ENTITY DATA MODEL 3 32
Pagebreak issue while printing the aspx page 3 16
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now