Datatable demo.

Source: Internet
Author: User
Operate on the table.
Datatabledemo. CS Code
1 Protected   Void Page_load ( Object Sender, eventargs E)
2 {
3 // Use the maketable function below to create a new table.
4 Gridview GD =   New Gridview ();
5 Datatable table;
6 Table = Makenamestable ();
7
8 // Once a table has been created, use
9 // Newrow to create a datarow.
10 Datarow row;
11 Row = Table. newrow ();
12
13 // Then add the new row to the collection.
14 Row [ " Fname " ] =   " John " ;
15 Row [ " Lname " ] =   " Smith " ;
16 Table. Rows. Add (ROW );
17 Datarow row1;
18 Row1 = Table. newrow ();
19 Row1 [ " Fname " ] =   " AA " ;
20 Row1 [ " Lname " ] =   " Bb " ;
21 Table. Rows. Add (row1 );
22
23 Datarow row2 = Table. newrow ();
24 Row2 [ " Fname " ] =   " CC " ;
25 Row2 [ " Lname " ] =   " Dd " ;
26 Table. Rows. Add (row2 );
27
28 Datarow row3 = Table. newrow ();
29 Row3 [ " Lname " ] =   " EE " ;
30 Table. Rows. Add (row3 );
31
32 GD. datasource = Table;
33 GD. databind ();
34
35 Form1.controls. Add (datagrid1 );
36 Form1.controls. Add (GD );
37
38 }
39 Private Datatable makenamestable ()
40 {
41 // Create a new datatable titled 'names .'
42 Datatable namestable =   New Datatable ( " Names " );
43
44 // Add three column objects to the table.
45 Datacolumn idcolumn =   New Datacolumn ();
46 Idcolumn. datatype = System. type. GetType ( " System. int32 " );
47 Idcolumn. columnname =   " ID " ;
48 Idcolumn. autoincrement =   True ;
49 Namestable. Columns. Add (idcolumn );
50
51 Datacolumn fnamecolumn =   New Datacolumn ();
52 Fnamecolumn. datatype = System. type. GetType ( " System. String " );
53 Fnamecolumn. columnname =   " Fname " ;
54 Fnamecolumn. defaultvalue =   " Fname " ;
55 Namestable. Columns. Add (fnamecolumn );
56
57 Datacolumn lnamecolumn =   New Datacolumn ();
58 Lnamecolumn. datatype = System. type. GetType ( " System. String " );
59 Lnamecolumn. columnname =   " Lname " ;
60 Namestable. Columns. Add (lnamecolumn );
61
62 // Create an array for datacolumn objects.
63 Datacolumn [] keys =   New Datacolumn [ 1 ];
64 Keys [ 0 ] = Idcolumn;
65 Namestable. primarykey = Keys;
66
67 // Return the new datatable.
68 Return Namestable;
69 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.