Well, the first thing is that you are really not binding the ICollection to the 
datagrid, you are binding the objects in the Collection.  So the first thing you need 
to know is that when you use DataBinding on a datagrid or any other bindable object is 
that it will only bind to Properties instead of Fields.  Here is the difference...

Public Class EmployeeBiz
  public EmployeeName As String
  public EmployeeZip As String
End Class

The above is using "Fields" because the data members are public.  Below is an example 
of using Properties.

Public Class EmployeeBiz
   Private  _employeeName As String
   Private  _employeeZip As String

  Public Property EmployeeName as String
    Get 
        Return _employeeName
    End Get

    Set
        _employeeName = value
    End Set
  End Property

  Public Property EmployeeZip as String
    Get 
        Return _employeeZip
    End Get

    Set
        _employeeZip = value
    End Set
  End Property
End Class

The above class with Public Properties will bind correctly, but the one with Fields 
will not.

It is actually amazing that I wrote some VB code.  

HTH.

Ben Miller
  ----- Original Message ----- 
  From: Leela krishna 
  To: [EMAIL PROTECTED] 
  Sent: Tuesday, August 17, 2004 8:38 AM
  Subject: [AspNetAnyQuestionIsOk] Binding a collection to a datagrid


  Hi ,
  can anybody help me ?
  I am trying Bind a collection to a datagrid.

  The test code is as below

  " AutoGenerateColumns="false" ShowFooter="true" CellPadding="3" BorderWidth="1" 
Height="144px" Width="320px" runat="server">****************************************
  " AutoGenerateColumns="false" ShowFooter="true" CellPadding="3" BorderWidth="1" 
Height="144px" Width="320px" runat="server">  <asp:DataGrid id="grdEmployees" 
style="Z-INDEX: 101; LEFT: 136px; POSITION: absolute; TOP: 72px"
      runat="server" Width="320px" Height="144px"
       BorderWidth="1" CellPadding="3" ShowFooter="true"     
AutoGenerateColumns="false"
       datasource='<%#empCol%>'>
         <HeaderStyle BackColor="#00aaaa"></HeaderStyle>
         <FooterStyle BackColor="#00aaaa"></FooterStyle>
         <Columns>
                <asp:BoundColumn HeaderText="Employee ID" 
                       DataField="EmployeeID" >
                       <ItemStyle BackColor="yellow"></ItemStyle>
                </asp:BoundColumn>
                <asp:BoundColumn HeaderText="Last Name" DataField="LastName" />
                <asp:BoundColumn HeaderText="First Name" 
DataField="FirstName"></asp:BoundColumn>

         </Columns>
         </asp:DataGrid>
  *************************************


  ***********************************

  Imports System.Collections.Specialized

  Public Class WebForm1

  Inherits System.Web.UI.Page

  Dim col As System.Collections.ICollection

  Public empCol As System.Collections.ICollection

  #Region " Web Form Designer Generated Code "

  'This call is required by the Web Form Designer.

  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

  End Sub

  Protected WithEvents grdEmployees As System.Web.UI.WebControls.DataGrid

  'NOTE: The following placeholder declaration is required by the Web Form Designer.

  'Do not delete or move it.

  Private designerPlaceholderDeclaration As System.Object

  Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) 
Handles MyBase.Init

  'CODEGEN: This method call is required by the Web Form Designer

  'Do not modify it using the code editor.

  InitializeComponent()

  End Sub

  #End Region

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
Handles MyBase.Load

  'Put user code to initialize the page here

  Dim Emp As New EmployeeBiz

  Emp = New EmployeeBiz

  empCol = Emp.GetEmployees

  ' grdEmployees.DataSource = empCol

  grdEmployees.DataBind()

  End Sub

  Public Class EmployeeBiz

  Public EmployeeID As String

  Public LastName As String

  Public FirstName As String

  Public Function GetEmployees() As Collection

  Dim tmpcol As New Collection

  Dim Emp As New EmployeeBiz

  Emp = New EmployeeBiz

  Emp.EmployeeID = "101"

  Emp.FirstName = "Leela"

  Emp.LastName = "Krishna"

  tmpcol.Add(Emp)

  Emp = New EmployeeBiz

  Emp.EmployeeID = "102"

  Emp.FirstName = "Prasanth"

  Emp.LastName = "pp"

  tmpcol.Add(Emp)

  Emp = New EmployeeBiz

  Emp.EmployeeID = "103"

  Emp.FirstName = "Nisanth"

  Emp.LastName = "Kulkarni"

  tmpcol.Add(Emp)

  Emp = New EmployeeBiz

  Emp.EmployeeID = "104"

  Emp.FirstName = "Salim"

  Emp.LastName = "peerzada"

  tmpcol.Add(Emp)

  Dim xcol As System.Collections.ICollection

  xcol = tmpcol

  Return xcol

  End Function

  End Class

  End Class

  ***************************************



  Regards,

  Leela Krishna


              
  ---------------------------------
  Do you Yahoo!?
  New and Improved Yahoo! Mail - Send 10MB messages!

  [Non-text portions of this message have been removed]



        Yahoo! Groups Sponsor 
              ADVERTISEMENT
             
       
       


------------------------------------------------------------------------------
  Yahoo! Groups Links

    a.. To visit your group on the web, go to:
    http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/
      
    b.. To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]
      
    c.. Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service. 



[Non-text portions of this message have been removed]



------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 

Reply via email to