Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello programmers, got a minor question here. As you see I am getting data on a grid view. My problem is how can I get data on a bound field? Please sir help me out
Here are my code in getting based on textbox label but i cannto get on a bound field. Sorry for my english
Dim gvCourse As GridView = DirectCast(sender, GridView)
            'Get the values stored in the text boxes
            Dim strOrderID As String = DirectCast(gvCourse.Rows(e.RowIndex).FindControl("lblOrderID"), Label).Text
            Dim strFreight As String = DirectCast(gvCourse.Rows(e.RowIndex).FindControl("txtFreight"), TextBox).Text
            Dim strShipperName As String = DirectCast(gvCourse.Rows(e.RowIndex).FindControl("txtShipperName"), TextBox).Text

Thanks
Posted

 
Share this answer
 
Hi,

See this sample:

In client code sample:
C#
 <Columns>
    <asp:templatefield headerstyle-horizontalalign="Center" headertext="View">
      <itemtemplate>
         <asp:linkbutton id="viewdata" runat="server" text="View">
         OnClick="viewdata_Click">
         </asp:linkbutton>
      </itemtemplate>
      <asp:boundfield datafield="OrderId" headertext="Order Id" />
      <asp:boundfield datafield="Freight" headertext="Freight" />
      <asp:boundfield datafield="ShipperName" headertext="Shipper Name" />
 </Columns>
</asp:templatefield>


In code behind sample:
C#
Protected Sub viewdata_Click(sender As Object, e As EventArgs)
    Dim viewdata As LinkButton = TryCast(sender, LinkButton)
    Dim row As GridViewRow = DirectCast(viewdata.NamingContainer, GridViewRow)
    ' row.Cells(0) is the link view button...
    Dim strOrderID As String = row.Cells(1).Text.Trim()
    Dim strFreight As String = row.Cells(2).Text.Trim()
    Dim strShipperName As String = row.Cells(3).Text.Trim()    
End Sub


Hope this could help...

Please vote if could help so that others may consider as an answer...

Regards,
 
Share this answer
 
v2
Comments
Zeek2 19-Jan-21 9:01am    
Is there a way to do this using the Datafield (i.e. field name) or column name, instead of using a constant number (e.g. column number 0,1,2,etc.), which could change with future development i.e. using column/cell number is not good for maintenance (poor show Microsoft!)
GridView Edit Update Using BoundField
http://patilranjeet.blogspot.in/p/aspnet-gridview-edit.html
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900