2012-08-09 73 views
0

我在visual basic中发现了类似的问题,但没有发现确切的结果和c#中的问题。将一个数据表的某些列复制到一个新的数据表

我想从10列数据表中使用INDEX(即1,4和5是索引,而不是它们的名称/头)复制某些列(比如1,4和5),并创建一个对象(我认为数组/列表最好?),我会传递给form2。在form2中,我希望从这些数组/列表中创建一个新的数据表,因此它最终将有3列与原始数据表的第1,4和5列相同。我还希望在我传递它之前,根据我将在别处设置的真/假值删除每个数组的第一个元素。

这里的轮廓到目前为止,我已经得到了( 'ALLDATA' 是我的数据表, 'CX' 是x列我想):

Form1中:

private void button2_Click(object sender, EventArgs e) //next 
    { 
     this.Hide(); 

     int c1 = 1; int c2 = 4; int c3 = 5 
     int[] 1st_col; int[] 2nd_col; int[] 3rd_col; 
     [assign c1th column to 1st_col, etc] 

     if (variable = marker_number) 
     { 
      [delete first element of each array] 
     } 

     Form2 step2 = new Form2(1st_col, 2nd_col, 3rd_col); 
     step2.ShowDialog(); 
    } 

窗体2:

public Form2(int 1st_col, int 2nd_col, int 3rd_col) 
    { 

     DataTable mytable1 = new DataTable();    
     [add 1st, 2nd, and 3rd cols to mytable1] 
     InitializeComponent(); 


    } 

如果还有什么我应该提供的,请让我知道!

回答

0

更新,试试这个:

Form1中

private void button2_Click(object sender, EventArgs e) 
    { 
     //10 column datatable 
     var dt10 = new DataTable("DT10"); 
     dt10.Columns.Add("PKID"); 
     dt10.Columns.Add("FName"); 
     dt10.Columns.Add("MName"); 
     dt10.Columns.Add("LName"); 
     dt10.Columns.Add("Address"); 
     dt10.Columns.Add("City"); 
     dt10.Columns.Add("State"); 
     dt10.Columns.Add("Zip"); 
     dt10.Columns.Add("Phone"); 
     dt10.Columns.Add("Fax"); 

     //give some sample data 
     dt10.Rows.Add(new object[] { 1, "Matt", "James", "Smith", "123 Main", "Philadelphia", "PA", "12141", "215-555-1111", "215-555-1212" }); 
     dt10.Rows.Add(new object[] { 2, "Mark", "James", "Smith", "123 Main", "Pittsburgh", "PA", "12141", "215-555-1111", "215-555-1212" }); 
     dt10.Rows.Add(new object[] { 3, "Luke", "James", "Smith", "123 Main", "Scranton", "PA", "12141", "215-555-1111", "215-555-1212" }); 
     dt10.Rows.Add(new object[] { 4, "John", "James", "Smith", "123 Main", "Reading", "PA", "12141", "215-555-1111", "215-555-1212" }); 
     dt10.Rows.Add(new object[] { 5, "Paul", "James", "Smith", "123 Main", "Harrisburg", "PA", "12141", "215-555-1111", "215-555-1212" }); 

     //create new datatable with subset of columns 
     var dt3 = new DataTable("DT3"); 
     dt3.Columns.Add("FName"); 
     dt3.Columns.Add("LName"); 
     dt3.Columns.Add("Phone"); 

     //indexes can be hardcoded or set at runtime 
     int c1 = 1; int c2 = 3; int c3 = 8; 

     //add all rows, but only the specified columns 
     foreach (DataRow r in dt10.Rows) 
     { 
      dt3.Rows.Add(new object[] { r[c1], r[c2], r[c3] });     
     } 

     bool includeFirstRow = chkIncludeFirstRow.Checked; 
     if (!includeFirstRow) 
     { 
      dt3.Rows.RemoveAt(0); 
     } 

     Form2 step2 = new Form2(dt3); 
     step2.ShowDialog(); 
    } 

窗体2

public partial class Form2 : Form 
{ 
    public Form2() 
    { 
     InitializeComponent(); 
    } 

    public Form2(DataTable dt) 
    { 
     InitializeComponent(); 
     //do your thing here, bind to datagridview or whatever 

    } 
} 
+0

我试图得到这个工作,但首先是如何正确初始化DRC我得到错误“类型'System.Data.DataRowCollection'没有定义构造函数”。我从来没有遇到过这个问题 - 为什么它不使用默认的构造函数?另外,“oneRow”应该是什么?我认为这意味着像“alldata [i]”,但产生一个错误,说我可以用[]索引到'System.Data.DataTable'类型的表达式 – Hyung 2012-08-13 16:58:48

相关问题