Array to able

Source: Internet
Author: User


Using system;
Using system. Data;

Namespace arraytodatatable
{
Class arraytodatatable
{
/// <Summary>
/// Convert a one-dimensional array to a able
/// </Summary>
Public static datatable convert (string columnname, string [] array)
{
Datatable dt = new datatable ();
DT. Columns. Add (columnname, typeof (string ));

For (INT I = 0; I <array. length; I ++)
{
Datarow DR = DT. newrow ();
Dr [columnname] = array [I]. tostring ();
DT. Rows. Add (DR );
}
Return DT;
}

/// <Summary>
/// Convert the two-dimensional array of the N column of the m row to the datatable
/// </Summary>
Public static datatable convert (string [] columnnames, string [,] arrays)
{
Datatable dt = new datatable ();

Foreach (string columnname in columnnames)
{
DT. Columns. Add (columnname, typeof (string ));
}

For (INT I1 = 0; I1 <arrays. getlength (0); I1 ++)
{
Datarow DR = DT. newrow ();
For (INT I = 0; I <columnnames. length; I ++)
{
Dr [I] = arrays [I1, I]. tostring ();
}
DT. Rows. Add (DR );
}
Return DT;
}
/// <Summary>
/// Convert the two-dimensional array of the N column of the m row to the datatable
/// </Summary>
Public static datatable convert (string [,] arrays)
{
Datatable dt = new datatable ();

Int A = arrays. getlength (0 );
For (INT I = 0; I <arrays. getlength (1); I ++)
{
DT. Columns. Add ("col" + I. tostring (), typeof (string ));
}

For (INT I1 = 0; I1 <arrays. getlength (0); I1 ++)
{
Datarow DR = DT. newrow ();
For (INT I = 0; I <arrays. getlength (1); I ++)
{
Dr [I] = arrays [I1, I]. tostring ();
}
DT. Rows. Add (DR );
}
Return DT;
}
}
}
Call
[CSHARP] view plaincopyprint?
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace arraytodatatable
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
{
Datagridview1.datasource = arraytodatatable. convert ("Haha", new string [] {"1", "2", "3", "4", "5", "6 "});
}

Private void button2_click (Object sender, eventargs E)
{
String [,] array3d = {

{"1", "Convert array to able 1", "0 "},

{"2", "Convert array to able 2", "1 "},

{"3", "Convert array to able 3", "1 "},

{"4", "Convert array to able 4", "2 "},

{"5", "Convert array to able 5", "2 "},

{"6", "Convert array to able 6", "5 "},

};
Datagridview1.datasource = arraytodatatable. Convert (New String [] {"haha1", "haha2", "hahaha3"}, array3d );
}

Private void button3_click (Object sender, eventargs E)
{
String [,] array3d = {

{"1", "Convert array to able 1", "0 "},

{"2", "Convert array to able 2", "1 "},

{"3", "Convert array to able 3", "1 "},

{"4", "Convert array to able 4", "2 "},

{"5", "Convert array to able 5", "2 "},

{"6", "Convert array to able 6", "5 "},

};
Datagridview1.datasource = arraytodatatable. Convert (array3d );
}
Private void linklabel1_linkclicked (Object sender, linklabellinkclickedeventargs E)
{
System. Diagnostics. process. Start ("iexplore.exe", "http://www.yongfa365.com /");

}
}
}

 

Array to able

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.