We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Building a GridView in ASP.NET vs VB.NET

Chaffe asked
Medium Priority
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

Watch Question

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

-- Jason

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2006

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

Good luck!
-- Jason


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.
Top Expert 2006

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


I will give that a try.  Thanks again Jason.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.