Accessing cell text and changing background color (

 Hello! I am trying to add on to the function below a feature that would change the background color of a row based on the cell values.

I basically have 3 cells with data in them ('In Stock, Needed, On Order). If something is not in stock, I want the row to be red, if it's on order I need it to be yellow, and so on.

What I am having trouble with is actually accessing the text values of the cells. I thought it was e.Row.Cells[2].Text but this hasn't worked. I am very new to as I usually use C# for this.

It seems like any input I give it crashes. Adding a simple statement such as "Dim var1 As String;" to the top of the function causes the program to crash.

Any help would be much appreciated, thank you all.


Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

''''''Dim var1 As String;

    ' only apply changes if its DataRow
    If e.Row.RowType = DataControlRowType.DataRow Then

        ' when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
        e.Row.Attributes.Add("onmouseover", ";'#CFF'")
        ' when mouse leaves the row, change the bg color to its original value
        e.Row.Attributes.Add("onmouseout", ";")
  'If e.Row.Cells[2].Text = "1" Then
  'e.Row.Attributes.Add("onmouseover", "'#000000'")
    End If
End Sub  

11/20/2008 2:53:33 PM 72751 articles. 3 followers. Follow

8 Replies

Similar Articles

[PageSpeed] 7

 I think you need to use the gridview's RowDataBound event instead of the RowCreated event to do what you need to do.

Jeff Turner (simpleModus)

Don't forget to mark the correct answer for your
question to help out future visitors!
11/20/2008 3:07:29 PM

I will give it a try. Do you think that's what causing the error to be thrown? It seems to me like it would just cause the logic to be off.


Thank you!

11/20/2008 3:17:11 PM

 I don't think that's what's crashing your app... You might want to run your javascript code through a js debugger. Right off the bat I see you're missing some ";" at the end of the javascript code you're inserting into the onmouseover event.

Jeff Turner (simpleModus)

Don't forget to mark the correct answer for your
question to help out future visitors!
11/20/2008 3:23:21 PM

The Javascript seems to be okay, I took a look at it and fixed that issue.


It just seems bizarre to me that adding "Dim var1 As String;" declaration to the top of the function would cause it to bomb.

11/20/2008 4:47:07 PM

Alright, I really just need to know how to access the Text in a cell of  a gridview in VB.NET. I've got this statement yet it isn't working: 

If e.Row.Cells(2).Text = "1"

It should be working because the value IS 1. I am just accessing it wrong and cannot figure out how to access it correctly. Can anyone help me out? Thanks!

11/20/2008 6:48:21 PM

 Are you sure your index of the cell is correct? The index starts at 0, so e.row.cells(2).text should give you the content of the third column.

Jeff Turner (simpleModus)

Don't forget to mark the correct answer for your
question to help out future visitors!
11/20/2008 7:25:08 PM

Unfortunately I had thought of that already, and it's not the issued. My columns are in this order: (Edit column), Cartridge Name, In Stock, etc etc. In stock is the one I'm trying to use a condition on.

Thank you for your suggestion!

11/20/2008 7:33:35 PM

Hi cdavis,

First off , I agree with tfsmag . You need to put your code in rowdatabound event .

see my code .


    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.Cells(1).Text = "1" Then
        End If
    End Sub
       <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            DeleteCommand="DELETE FROM [country] WHERE [countryid] = @countryid" InsertCommand="INSERT INTO [country] ([countryid], [countryname]) VALUES (@countryid, @countryname)"
            SelectCommand="SELECT * FROM [country]" UpdateCommand="UPDATE [country] SET [countryname] = @countryname WHERE [countryid] = @countryid">
                <asp:Parameter Name="countryid" Type="Int64" />
                <asp:Parameter Name="countryname" Type="String" />
                <asp:Parameter Name="countryid" Type="Int64" />
                <asp:Parameter Name="countryid" Type="Int64" />
                <asp:Parameter Name="countryname" Type="String" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="countryid"
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="countryid" HeaderText="countryid" ReadOnly="True" SortExpression="countryid" />
                <asp:BoundField DataField="countryname" HeaderText="countryname" SortExpression="countryname" />

Samu Zhang
Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
11/24/2008 10:04:37 AM

Similar Artilces:

Sending SMS using or using or
Hi  My requirement is I hav one csv file with these fields id,mobilenum,messgae,status.intiallu staus is 0. once i read the all fileds and take that mobile number.using tat mobile number  i need to send sms .after sending sms i shuld change status as 1 How to send sms thru coding (please dont provide any links.if it is provide also please give working links becox i checked codeproject .i didnt get any nice link. and also provide the how to update the status field im csv file   Thank ssandhya   To send SMS, you need some third party SMS providers. if you consul...

How to change code to C#.Net ?...
Hi, Earlier i had a class  that i got it from this Forum, it in and i am trying it to convert it in but i am unable to covert properly to work. the functionality of this class is to convert number value to words. i am putting both of code and please rectify it.Public Class NumberToWords Dim Character As String Public Function ConvertNumber(ByVal A As Double) As String On Error Resume Next Dim MM Dim IM ConvertNumber = "" MM = Microsoft.VisualBasic.Len(A) IM = Microsoft.VisualBasic.Left(A, Mic...

converting to from offered me some sample code when I signed up with them the only problem is the sample code is in but my page that they type all of their credit card into is <code><%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.IO" %> <script language="C#" runat="server"> void Page_Load(Object Src, EventArgs E) { myPage.Text = readHtmlPage(""); } private String readHtmlPage(string url) { ...

Access Net work mapped drive VB.NET
Hi I have a docuemnt mangemnt system coded in VB.NET. The system sometimes needs to get documents from Network mapped drive and shared drives. In development machine everything works fine. But when I moved the code to Test serever, it could not access the mapped drives. I tried all security settings. But still could not. I tried unc path, but it throws In valid user exception. Can anybody help m eto solve this issue. Thank you Jikk Hi Jikk, What login is your app running under? Whichever it is, it has to have permission to access the network, which ASPNET doesn'...

How to change Html Controls in
Hi there, I just want to change the Html Controls disabled property to true in code. how to do pls help.. regards shekhar control.enabled=false ***********************Dinakar NethiLife is short. Enjoy it.*********************** Hi when i load the page it is disabled by default <INPUT class="TxtLogin" id="txtTitle" tabIndex="1" type="text" maxLength="50" size="50" disabled> Inside x.aspx.vb code view i'm unable to get the text control's design time property asume this is my ...

Changing the default font size is for
Subject kinda says it all. Surely there must be a way to changing the default font size for the items in the forms. Thanks for the help in advance. Daniel Yes in Tools Options, I believe its Text Editor > Basic. BrianBrian"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).... vs vs
just out of curiousity which looks, acts most like java? i'm still learning trying to used to but it seems that it was easier to do some stuff with java. C# is Java++ in my opinion. If you have done C or Jave you will prefer C#.Mike Schellenberger - MCAD If you are looking for language similar to java go for J#. However most code samples you find on the net will be in VB.NET and C#....

.Net 2
Hi Friends, I want to show Different Tabs for different information on my web form ( .aspx page ). I could not found Tab Control with .net 2005. I know there is some but couldnt find it. Can you please tell me from where can I add Tab Control to my web form ? Thanks in advance, Nirav With some tedious settings of properties on the menu control, it will behave like a tab control.  Then you need to use a multiview control and fill it with views.  Then you need to wire up the menu to the multiview or something.  I tried this before and it works, but we wrote our own contro...

Change user controls properties /
I have a user control that contains several divs.  I want to know how to make the div visible or invisible.  Please help... below is a code snippet.<div id="travelDetails" class="menuDetails"></div> <div id="settingsHeader" class="menuHeader"><asp:Label ID="label12" Text="Settings" runat="server" CssClass="menuHeaderText"></asp:Label> </div>   How would I make the travelDetails div visible or invisible? <%@ Register TagPrefix="menu" TagName=&q...

Accessing a control inside CreateUserWizard
Hi all, im still quite a newbie with programming and so bear with me while i try to explain my problem. I have added a DropDownList to my CreateUserWizard so that whoever sets up a new account can select a role for that user. The DropDownList is populated from the system table which holds the roles.  Most users i do not wish to be able to select a role, but only "SuperAdmin" users should be able to do that so that Admin users cannot add more Admin Users, (or superadmin users). So i have defaulted the dropdownlist to the "User" Role and set the enabled prope...

How to access divs with a user control /
I have a user control that have several divs.  How do I access the div within the user control to make them visible or invisible.  Below is a code snippet:  <div id="countDetails" class="menuDetails"> <asp:Label id="label5" Text="View on Map" runat="server" CssClass="menuDetailText"></asp:Label><br /><asp:Label id="label6" Text="Print Report" runat="server" CssClass="menuDetailText"></asp:Label><br /> <asp:Label id="label7&q...

How to access a cell value from datagrid in
I have written this code for accessing value of a selected row from is not working and showing exception that  Object reference not set to an instance of an object.use new keyword... i am nt able to understand there any other way to access a cell value of datagrid??  Dim row As GridViewRow row = grdNomination.SelectedRow Dim a As String = "" a = row.Cells(2).Text.ToString() Response.Redirect("details.aspx")       Move your codes at SelectedIndexChanged event of GridView so that you can access it thi...

How to access data grid cells in
 Anyone who knows the code for accessing data grid cell, pls share it.Thanks in advance.  DataGrid1.Items(0).Cells(0).Text return first cell's text.Click on 'Mark as Answer' if this post is helpful.ImranKhan pathan DataGridView.Rows(index).Cells("CellName").Value If it helps then 'Mark as Answer'...

Vb.Net:Change the text of link button
Hi, I have a linkbutton in my datagrid Initially, I set the text of the link button as "show" once user click it, i want it to change into "view more detail" I tried to change as below but it lose its hyperlink function and just as a text only dim dgitem as datagridItem  For Each dgitem In DataGrid1.Items dgitem.Cells(7).Text = "view more detail" Next can anyone give me direction Thanks in advance   You can cast the sender of the control at Button Click event..see belowProtected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As Eve...

Web resources about - Accessing cell text and changing background color ( -

Discovery of cosmic microwave background radiation - Wikipedia, the free encyclopedia
The accidental discovery of cosmic microwave background radiation is a major development in modern physical cosmology . Although predicted by ...

Android App OStream Speeds Up Facebook Experience By Synchronizing Content In Background
Facebook’s newly revamped Android application was rebuilt with a focus on speed, and startup oStream wants to help users of devices on that operating ...

Background for Dick Armey’s Endorsement of “Nothing for the Nation”
To truly grasp the profound nature of Dick Armey’s endorsement of Nothing for the Nation: Who Got What Out of Iraq, one must understand the events ...

Facebook Photos Gets Polish: White Background, Bigger Images That Load Faster
... Facebook product manager Justin Shaffer describes a new version of Facebook’s photo viewer coming out now. In place of the black lightbox background ...

Easy Background Tasks in ASP.NET
As I work on the badge implementation for Stack Overflow, I needed a way to call the code that detects and awards the badges out of band. Traditionally ...

Camera Amazing with Pictures Background Remover for iPhone 3GS, iPhone 4, iPhone 4S, iPhone 5, iPod touch ...
Get Camera Amazing with Pictures Background Remover on the App Store. See screenshots and ratings, and read customer reviews.

Malcolm Turnbull uses shared business background to reset Indonesian relations
Malcolm Turnbull has used his first major overseas visit to reset Australia's fractious relationship with Indonesia, leveraging his common personal ...

More to selecting political background music than indiscriminate meandering
Gathering a song to use as a protest weapon can backfire.

Obama to Veto Bill Requiring Background Checks On Syrian Refugees; France Demands Tighter Controls, EU ...
... requirements " that would harm efforts to assist some of the world's most vulnerable people. The bill would add a new requirement for FBI background ...

Ben Carson hits media amid background questions - Business Insider
"What happened to investigative reporting?" CBS Retired neurosurgeon Ben Carson, under increased scrutiny over his personal background this ...

Resources last updated: 11/29/2015 1:30:51 AM