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);
"Rosy";
"M";
oEmployee.iAge = 29;
false;
"Ron";
"Lon";
oEmployee.iAge = 27;
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?
Thanks a lotttttttttttttt
vinuthak said: but when i tried to Add the rows with default Add Row button provided by UltraGrid the i Got message "Underlying Datasource does not support adding new rows". Is it because the ArrayList has limited functionality?
but when i tried to Add the rows with default Add Row button provided by UltraGrid the i Got message "Underlying Datasource does not support adding new rows".
Is it because the ArrayList has limited functionality?
Yes, that is why. The grid will not be able to add rows to an ArrayList, because the BindingManager will not know how to create an object of the appropriate type. In fact, the BindingManager in DotNet won't have any way of know what type of object to create.
vinuthak said:My question now is why Adding rows in not possible in Scenario 1 and how it is possible in Scenario 2.
I'm very surprised that this works. My best guess is that the BindingManager in DotNet is looking at the first item on the list and assuming that all items on the list are of the same type and then checking for ValueTypes or maybe specific types like Integer that they know how to create.
Try the same thing with the DataGridView in DotNet and I expect you will get the same results.
Thanks Mike for Reply,
1.I made the public members in employee class as public properties
I tried assigning Arraylist (which has collection of employee objects) as datasource to grid.
objListGrid.DataSource = objList;
when i run the application the grid was displaying all the data, but when i tried to Add the rows with default Add Row button provided by UltraGrid the i Got message "Underlying Datasource does not support adding new rows".
2.Also If i try to assign below ArrayList
ArrayList List = new ArrayList();
List.Add(1);
List.Add(2);
List.Add(3);
ListGrid.DataSource = List;
as datasource to grid and i tried to Add the rows with default Add Row button provided by UltraGrid it works fine and i am able to add rows now .
My question now is why Adding rows in not possible in Scenario 1 and how it is possible in Scenario 2.
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; }