Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
485
Assigning arraylist as datasource to wingrid
posted

I have an class Employee

 

 

 

 

 

 

public class cEmployee

{public string sFirstName;

 

 

public string sLastName;

 

 

public int iAge;

 

 

public bool bMale;}

 

and an Arraylist ArrayList objList = new ArrayList();

 

I create the 3 objects of the class 

 

oEmployee =

new cEmployee();

oEmployee.sFirstName =

"Mike";

oEmployee.sLastName =

"Jack";

oEmployee.iAge = 32;

oEmployee.bMale =

true;

objList.Add(oEmployee);

oEmployee =

new cEmployee();

oEmployee.sFirstName =

"Rosy";

oEmployee.sLastName =

"M";

oEmployee.iAge = 29;

oEmployee.bMale =

false;

objList.Add(oEmployee);

oEmployee =

new cEmployee();

oEmployee.sFirstName =

"Ron";

oEmployee.sLastName =

"Lon";

oEmployee.iAge = 27;

oEmployee.bMale =

false;

and i am filling these objects in an arraylist

 

 

 

which i am passing as Datasource to the Grid.

objListGrid.DataSource = oEmployee;

 

I am not able to see any results why?

Parents
  • 469350
    Offline posted

    Hi,

    There are a coupe of things about this code that will not work.

    First, you are setting the DataSource of the grid to a single Employee instance, and not the ArrayList.

    Second, your Employee class has 4 public members, but no public properties, so again, the BindingManager in DotNet will not be able to determine the data structure.

    Also, an ArrayList is not a good choice for DataBinding. It will work in a very basic way, but ArrayList and List<T> implement the IList interface which is not really intended for DataBinding. The grid will not be notified when values in your data source change, nor will the grid be able to allow the user to add new rows using these objects.

    If you want to create object in memory for the purposes of DataBinding, then those objects have to expose public properties for each column you want the grid to show. And I recommend using a BindingList<T>, rather than an ArrayList.


            public class cEmployee
            {
                private string firstName;

                public string FirstName
                {
                    get{ return this.firstName; }
                    set { this.firstName = value; }
                }            
            }

            private void Form1_Load(object sender, EventArgs e)
            {           
                //ArrayList arrayList = new ArrayList();
                BindingList<cEmployee> arrayList = new BindingList<cEmployee>();
               
                cEmployee employee1 = new cEmployee();
                employee1.FirstName = "Mike";
                arrayList.Add(employee1);

                this.ultraGrid1.DataSource = arrayList;
            }

     

Reply Children
No Data