0

我在visual basic中发现了类似的问题,但在c#中没有什么确切的问题。

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

这是我到目前为止的大纲('alldata' 是我的数据表,'cx' 是我想要获得的第 x 列):

表格1:

    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();


    }

如果还有什么我应该提供的,请告诉我!

4

1 回答 1

0

更新了,试试这个:

表格1

    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

    }
}
于 2012-08-09T20:46:02.213 回答