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