views:

248

answers:

0

I'm trying to retrieve the DataTextField (The auto inputted CompanyName) so I can know which button/row was clicked.

Here's my Gridview

<asp:GridView ID="VendorsGridView" runat="server" 
     AllowPaging="True" AutoGenerateColumns="False" 
     DataSourceID="ObjectDataSource_Vendors" 
     DataKeyNames="ID,ModuleId" CellPadding="4" OnRowCommand="ChangeVendor" 
     CellSpacing="1" BorderWidth="0px" EnableViewState="False"> 
  <Columns> 
    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 
    <asp:ButtonField DataTextField="CompanyName" ButtonType="Link" CommandName="ChangeVendors" />
  </Columns> 
  <EmptyDataTemplate>
    There are no vendors available.
  </EmptyDataTemplate> 
</asp:GridView>

Here's my code-behind method:

Protected Sub ChangeVendor(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
            Dim vendor As VendorsInfo
            Dim vendors As List(Of VendorsInfo)
            Dim index As Integer
            Dim selectedRow As GridViewRow
            Dim vendorCell As TableCell
            Dim companyName As String

            If e.CommandName.Equals("ChangeVendors") Then
                index = Convert.ToInt32(e.CommandArgument)
                selectedRow = VendorsGridView.Rows(index)
                vendorCell = selectedRow.Cells(0)
                companyName = vendorCell.Text

                myLabel.Text = companyName

                vendors = VendorsController.Vendors_Select(ModuleId, companyName)

                If vendors.Count > 0 Then
                   //Do stuff
                End If
            End If
        End Sub

I've tried changing selectedRow.Cells(0) to (1) and (2) -> 2 gives an out of bounds exception... But the label ends up being blank for 0 and 1 (I know it's setting the label properly because the label originally says "Hello World" and gets changed to be blank...)

Does anyone know how I can retrieve the DataTextField from an asp:ButtonField similar to the way I've attempted? Microsoft's tutorial is what I've followed so far, but I'm not sure what i'm doing wrong though...