• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 668
  • Last Modified:

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

  • 3
  • 2
1 Solution
Jason ScolaroCommented:
I think you need to change this line:
.DataSource = "ObjectDS"
.DataSourceID = "ObjectDS"

-- Jason
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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now