将一个数据表的某些列复制到新数据表

时间:2012-08-09 18:56:54

标签: c# .net datatable

我在visual basic中找到了类似的问题,但在c#中没有一点确切。

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

这是我到目前为止的大纲('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();
    }

Form2:

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

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


    }

如果还有其他我需要提供的内容,请告诉我们!

1 个答案:

答案 0 :(得分: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

    }
}