Object Binding in DataGrid with C #

zhaozj2021-02-16  56

Authorda Of SubmissionUser Levelmahesh Chand06 / 15 / 2004Beginner

Introduction

In this Article, I will discuss how to bind Objects such as an arraylist to a datagrid control using c # and add .net.

Binding an arraylist

If you create an ArrayList and bind it to the DataGrid control, DataGrid does not display the contents For example, the following code creates an ArrayList objects, add items to it, and binds it to the DataGrid.:

ArrayList List = new arraylist (); list.add (1); list.add (2); List.Add (3); DataGrid1.datasource = list;

. ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

The Solution

The solution of the problem is to create a class that exposes an integer as its property. Then you create an ArrayList and bind it to the DataGrid. For example, the following code creates an ArrayList of MyClass object, which takes one value as constructor parameter . Here...............

ArrayList List = new arraylist (); list.add (new myclass (1); list.add (new myclass (2); list.add (new myclass (3); DataGrid1.datasource = list;

The Complete Code

Here is a class with integer and string proferties.

public class BindingObject {private int intMember; private string stringMember; private string nullMember; public BindingObject (int i, string str1, string str2) {intMember = i; stringMember = str1; nullMember = str2;} public int IntMember {get {return intMember }} public string Stringmember {get {returnctionmember;}} public string nullmember {get {return nullmember;}}}

Now you can create an ArrayList of this class objects and bind it to the DataGrid as following: ArrayList list = new ArrayList (); list.Add (new BindingObject (1, "One", null)); list.Add (new BindingObject (2, "Two", NULL); List.Add (New BindingObject (3, "Three", NULL); DataGrid1.datasource = List;

The Contents Would Look Like The Following:

WHERE STRINGMEMBER, INTMEMBER, AND NULLMEMEMBER ARE Three Properties of the Object.

See the attach code for more details.

Summary

.

转载请注明原文地址:https://www.9cbs.com/read-21345.html

New Post(0)