Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 16th, 2003, 11:47 AM
Registered User
 
Join Date: Sep 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Binding an aarraylist to a datagrid

This is a new post.
Please,I need to know what’s wrong with my code and the possible solution.

I have an arraylist that was population with a database record,but anytime I bind it to a datagrid, It will not display the records,rather it will display the arraylist properties like “Rank”,”isreadonly”,”SyncRoot” etc.

Please,could u offer me the solution to the above problem.

The code is shown below and I will attach the screen if needed.

You can change the dbconnection and sql statement to see what I mean

THE FORM CODE

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Laboratory
{
    public class frmLaboratory : System.Windows.Forms.Form
    {
        private System.Windows.Forms.DataGrid dataGridLab;
        private System.ComponentModel.Container components = null;
        private BusinessObjectCollection oArrayList;

        public frmLaboratory()
        {
            InitializeComponent();
            CreateArrayList();
            BindListToGrid(dataGridLab);
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridLab = new System.Windows.Forms.DataGrid();
            ((System.ComponentModel.ISupportInitialize)(this.d ataGridLab)).BeginInit();
            this.SuspendLayout();
            //
            // dataGridLab
            //
            this.dataGridLab.DataMember = "";
            this.dataGridLab.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGridLab.Name = "dataGridLab";
            this.dataGridLab.Size = new System.Drawing.Size(568, 232);
            this.dataGridLab.TabIndex = 0;
            //
            // frmLaboratory
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(566, 231);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                         this.dataGridLab});
            this.Name = "frmLaboratory";
            this.Text = "Advance Laboratory";
            ((System.ComponentModel.ISupportInitialize)(this.d ataGridLab)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new frmLaboratory());
        }

        private void CreateArrayList()
        {
            try
            {
                //Establish the connection to the data base and open it
                SqlDataReader oReader = null;
                SqlConnection sqlConn = new SqlConnection("Database=mydb;Server=local; uid = sa; pwd= ");
                //*-Pass the required details
                sqlConn.Open();
                //create the sql command object and set its command type to
                //execute the sql query to get the results
                SqlCommand sc = new SqlCommand();
                sc.Connection = sqlConn;
            // sc.CommandType = CommandType.Text;
                sc.CommandText = "SELECT Top 30 clClientName,clClientID,clClientType,clHeadhunt FROM tblClient";
                oReader = sc.ExecuteReader();

                oArrayList = new BusinessObjectCollection();
                if (oReader != null)
                {
                    while (oReader.Read())
                    {
                        object[] values = new object[oReader.FieldCount];
                        oReader.GetValues(values);
                        oArrayList.Add(values);
                    }
                }
            }
            catch(Exception e){
                MessageBox.Show(e.Message.ToString());
            }
        }


        private void BindListToGrid(DataGrid ctlGrid)
        {
            ctlGrid.Anchor = AnchorStyles.Bottom |AnchorStyles.Left |
            AnchorStyles.Right| AnchorStyles.Top;
            ctlGrid.AlternatingBackColor = System.Drawing.Color.SkyBlue;
            ctlGrid.BackColor = ColorTranslator.FromHtml("#FFFFFF");
            ctlGrid.BorderStyle = System.Windows.Forms.BorderStyle.None;

            ctlGrid.SetDataBinding(oArrayList,"");

        }

    }
}
THE BUSINESSOBJECTCODE


using System;
using System.Collections;

namespace Laboratory
{

    public class BusinessObjectCollection : CollectionBase,ICollection
    {
        public BusinessObjectCollection()
        {

        }

        //*************************************************
        // Item Property - Public - ReadOnly
        //-------------------------------------------------
        // Returns the BusinessObject with the specified
        // Index
        //*************************************************
        public object this[Int32 Index]
        {
            get{
                return this.List[Index];
            }
        }

        //*************************************************
        // Add() Method
        //-------------------------------------------------
        // Add the specified BusinessObject object to the
        // CollectionBase.List
        //*************************************************
        public void Add(object BusinessObject)
        {
            this.List.Add(BusinessObject);
        }

        //*************************************************
        // Insert() Method
        //-------------------------------------------------
        // Insert the specified BusinessObject object to
        // the CollectionBase.List at the specified Index
        //*************************************************
        public void Insert(Int32 Index,object BusinessObject)
        {
            this.List.Insert(Index,BusinessObject);
        }

        //*************************************************
        // Remove() Method
        //-------------------------------------------------
        // Remove the specified BusinessObject object to the
        // CollectionBase.List
        //*************************************************
        public void Remove(object BusinessObject)
        {
            this.List.Remove(BusinessObject);
        }

        //*************************************************
        // IndexOf() Method
        //-------------------------------------------------
        // Return the index value from the
        // CollectionBase.List for the specified item.
        //*************************************************
        public Int32 IndexOf(object BusinessObject)
        {
            return this.List.IndexOf(BusinessObject);
        }

        //*************************************************
        // Clear() Method
        //-------------------------------------------------
        // Clear the specified BusinessObject object to the
        // CollectionBase.List
        //*************************************************
        public void Clear()
        {
            this.List.Clear();
        }

        //*************************************************
        // RemoveAt() Method
        //-------------------------------------------------
        // Remove the specified BusinessObject object to the
        // CollectionBase.List at a specified Index
        //*************************************************
        public void Remove(Int32 Index)
        {
            this.List.RemoveAt(Index);
        }

        //*************************************************
        // Contains() Method
        //-------------------------------------------------
        // Return if the CollectionBase.List contains
        // a specific BusinessObject.
        //*************************************************
        public bool Contains(object BusinessObject)
        {
            return this.List.Contains(BusinessObject);
        }

        //*************************************************
        // Count() Method
        //-------------------------------------------------
        // Returns the number of BusinessObject object
        // In the Collection.
        //*************************************************
        public Int32 Count
        {
            get{
                return this.List.Count;
            }
        }
    }
}
 
Old September 20th, 2003, 11:12 AM
Registered User
 
Join Date: Sep 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Delano

The grid is looking for a string to display. Where does it look, you ask? It gets the string returned by the object's ToString() method. In objects like DataTables, DataRows, DataViews, the ToString() method returns meaningful data but in the case of user defined objects (like your BusinessObject) it returns the-not-so-useful stuffs like namespace membership, blah,blah. The ToString() method is a member of the System.Object which is the base of all objects and it is [u]virtual</u> meaning your your BusinessObject class already has it but the problem is, it does not return "sensible" string. The solution is to [u]override</u> the method in your class.

To make myself clear, suppose I have a class Person which I am going to use in a collection to be bound in a comboBox and I want that the combobox should display the "LastName, Firstname MI" format. The following is the code for the class
Code:
public class Person {
    string lastName, firstName, MI;

    /*
    useful members here
    */

    public override string ToString() {
        return lastName + ", " + firstName + " " + MI;
    }
}


 
Old September 22nd, 2003, 05:20 AM
Registered User
 
Join Date: Sep 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks bani. Take my businessobjectcollection to be an arraylist. How can I bound that arraylist to a datagrid.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Binding Using DataGrid hoailing22 ASP.NET 1.0 and 1.1 Basics 17 November 14th, 2006 02:17 AM
Datagrid binding two dropdown inkrajesh ASP.NET 1.0 and 1.1 Basics 0 July 18th, 2006 10:46 PM
Binding a datagrid on errors drono ASP.NET 1.0 and 1.1 Professional 3 June 9th, 2005 08:30 AM
Binding Data to a DataGrid RPG SEARCH ASP.NET 1.0 and 1.1 Basics 5 August 9th, 2004 02:27 PM
DataGrid Binding GrindCrusher Classic ASP Databases 0 February 21st, 2004 05:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.