Building a GridView in ASP.NET vs VB.NET

Greetings, Can someone tell me why when I build this Grid in ASP.NET it works perfect, and when I do the same thing in my code behind, I don't get any errors, but the grid doesn't show up on the page at all.

          <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="2" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="ObjectDS" DataKeyNames="ContactID">            
                <asp:BoundField DataField="ContactID" HeaderText="ContactID" SortExpression="ContactID" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:CommandField HeaderText="Action" ShowEditButton="True" ShowHeader="True" />
        <asp:ObjectDataSource ID="ObjectDS" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetBasicView" TypeName="DA.MasterTableAdapters.ContactsTableAdapter" UpdateMethod="UpdateBasicView">
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="original_ContactID" Type="Int32" />
Protected Sub WPContactSearchGrid()
        Dim ContactID, FirstName As New BoundField
        Dim CmdField As New CommandField
        Dim DataKey(0) As String
        Grid = New GridView
        DataKey(0) = "ContactID"
        With Grid
            'Set Grid Specs
            .ID = "ContactsGridView"
            .AutoGenerateColumns = False
            .DataKeyNames = DataKey
            .AllowSorting = True
            .AllowPaging = True

            ContactID.ReadOnly = True
            ContactID.DataField = "ContactID"
            ContactID.Visible = False
            FirstName.HeaderText = "First Name"
            FirstName.SortExpression = "FirstName"
            FirstName.DataField = "FirstName"

            CmdField.ShowHeader = True
            CmdField.HeaderText = "Action"
            CmdField.ShowEditButton = True

            .DataSource = "ObjectDS"

        End With
        Dim ObjectDS As New ObjectDataSource
        Dim FN, OCID As New Parameter
        With ObjectDS
            .ID = "ObjectDS"
            .OldValuesParameterFormatString = "original_{0}"
            .TypeName = "DA.MasterTableAdapters.ContactsTableAdapter"
            .SelectMethod = "GetBasicView"
            .UpdateMethod = "UpdateBasicView"

            FN.Name = "FirstName"
            FN.Type = TypeCode.String
            OCID.Name = "original_ContactID"
            OCID.Type = TypeCode.Int32
        End With
    End Sub

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason ScolaroCommented:
I think you need to change this line:
.DataSource = "ObjectDS"
.DataSourceID = "ObjectDS"

-- Jason

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jason ScolaroCommented:
And I'm not 100% sure, but you might need to add the following line after the .DataSourceID:

Good luck!
-- Jason
ChaffeAuthor Commented:
Jason, That did it.  I didn't need to have the .DataBind().
I noticed that when you click the edit and the labels become text fields, these text fields contain a bunch of spaces after the text.  Is there a way to trim these spaces when I click on the edit button?  As always, your help is appreciated.
Jason ScolaroCommented:
Glad that worked for you.  The extra spaces are probably coming from your actual data, so you might want to do the trimming as part of the stored procedure (or SELECT query).  Otherwise you'll have to change your BoundField columns to TemplateField columns, add the TextBox to the EditItemTemplate and put the Trim() logic in there, like:

  <asp:TextBox runat="server" id="TextBox1" Text='<%# Bind("FirstName").ToString().Trim() %>' />

Hope that helps.  Good luck!
-- Jason
ChaffeAuthor Commented:
I will give that a try.  Thanks again Jason.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.