Building a GridView in ASP.NET vs VB.NET

Posted on 2006-03-26
Last Modified: 2008-01-09
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

Question by:Chaffe
    LVL 22

    Accepted Solution

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

    -- Jason
    LVL 22

    Expert Comment

    And I'm not 100% sure, but you might need to add the following line after the .DataSourceID:

    Good luck!
    -- Jason

    Author Comment

    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.
    LVL 22

    Expert Comment

    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

    Author Comment

    I will give that a try.  Thanks again Jason.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now