tryng RowDataBound event to bind a TemplateField to a GridView in VB2015/ASP

I have a working GridView bound to SQL query, and I'm adding onto it, a new TemplateField  via:
     
           <columns>
                    <asp:BoundField DataField="spid" HeaderText="spid" SortExpression="spid" />
                    <asp:BoundField DataField="Login" HeaderText="Login" ReadOnly="True" SortExpression="Login" />
                    <asp:BoundField DataField="DB" HeaderText="DB" ReadOnly="True" SortExpression="DB" />
                    <asp:BoundField DataField="SERVER" HeaderText="SERVER" SortExpression="SERVER"/>
                    <asp:BoundField DataField="LOGIN TIME" HeaderText="LOGIN TIME" ReadOnly="True" SortExpression="LOGIN TIME"/>
                    <asp:BoundField DataField="USER COUNT" HeaderText="USER COUNT" ReadOnly="True" SortExpression="USER COUNT" visible="FALSE"/>
                    <%--<asp:BoundField DataField="siteName" HeaderText="SITE" SortExpression="siteName"/>--%>
                    <asp:TemplateField HeaderText="SiteName" SortExpression="Sitename"> 
                        <ItemTemplate> 
                            <asp:Label ID="lblSiteName" runat="server"></asp:Label> 
                        </ItemTemplate> 
                    </asp:TemplateField>

                  </columns>

Open in new window


I'm trying to use:
    Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound

Open in new window

to then, populate the new column cells with text, but I notice that when stepping through the code, if I highlight GridView1
in as seen below:
  stepCode.PNG
as you can see, the GridView has NO rows.
So, somehow, I'm losing the bindings before entering the even handler...?
Richard GouetteIT ManagerAsked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
any reason why you want to do a looping on:

nameToCheck = GridView1.Rows(i).Cells(1).Text

??

let's say if you want to verify the second column (index as 1)'s value and place a custom value in cell 7 (index as 6), you still can use the RowDataBound event, perhaps in this way:

Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim nameToCheck As String = e.Row.Cells(1).Text
            If (Convert.ToInt32(nameToCheck) >= 5) Then
                e.Row.Cells(6).Text = "ME"
            Else
                e.Row.Cells(6).Text = "NOT ME"
            End If
        End If
    End Sub

Open in new window


try customize accordingly
0
 
Ryan ChongConnect With a Mentor Commented:
as you can see, the GridView has NO rows.
So, somehow, I'm losing the bindings before entering the even handler...?

Check this article for the info:

GridView.RowDataBound Event
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound(v=vs.110).aspx

The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.

it means RowDataBound event will be triggered when the data row is generating, in the event that when the RowDataBound event is first raised, your GridView is empty (no row)
0
 
Richard GouetteIT ManagerAuthor Commented:
I seem to be struggling to know where I would set cells(7).text  values, and have them bound to the GridView1..

Any further direction?

Thanks,
R
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Ryan ChongCommented:
I seem to be struggling to know where I would set cells(7).text  values, and have them bound to the GridView1..
is this related to your question? what is cells(7).text? how you want it bound to the GridView1?
0
 
Richard GouetteIT ManagerAuthor Commented:
is this related to your question?
YES

what is cells(7).text?
I THINK I MEANT TO SAY CELLS(6)
THIS SI MEANT TO BE THE NEW COLUMN OF CELLS

how you want it bound to the GridView1?
I AM TRYING TO HAVE THE NEW COLUMN/CELLS BOUND ANYTIME BEFORE THE PAGE IS DISPLAYED

Perhaps the better question might be:
"When should I bind a new column of data, so that it is displayed with the SQL bound data?"
0
 
Ryan ChongCommented:
ok, so knowing you got a template field with a label control that need to be bounded, so what data to bind to this template field?

from your codes in your question, it seems that you're reading the field's value instead of populating the value into it?
0
 
Richard GouetteIT ManagerAuthor Commented:
I'm trying to write in values in this way:

           For i = 0 To GridView1.Rows.Count - 1
                         nameToCheck = GridView1.Rows(i).Cells(1).Text
                         nameToCheck = Trim(nameToCheck)
                         lb1.Items.Add(String.Concat(nameToCheck + i.ToString))
                         Select Case nameToCheck
                                   Case "AVALUE"
                                               GridView1.Rows(i).Cells(6).Text = "ME"
                                   Case "ANOTHERVALUE"
                                               GridView1.Rows(i).Cells(6).Text = "ME"
             Next

Open in new window

0
 
Ryan ChongCommented:
any feedback on this?
0
 
Ryan ChongCommented:
suggested solutions should enable asker to address the solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.