2

我有一个小程序,它需要一个数据表(从 sql 数据库中获取数据),然后按字段将其拆分为数据表数组,然后应该将其显示在 tabcontrol 中,每个字段都在它自己的选项卡中

拆分,采用单个数据表并拆分为数据表数组,我认为工作正常

public DataTable[] splitTable(DataTable mainDT,string columnName)
    {
        int tmp=0;
        DataTable[] splitDT = new DataTable[11];
        for (int i=0;i<11;i++)
            splitDT[i]=new DataTable();



        foreach (DataRow row in mainDT.Rows)
        {              
           tmp = row[columnName].GetHashCode();
           splitDT[tmp].ImportRow(row);
        }
        return splitDT;
    }

这是问题部分

public Display(string Name, string rname, DataTable[] left,int tabNum)
    {
        InitializeComponent();
        TabPage tp;
        DataGridView dgw;

        lLeftTable.Text = Name;

        for (int i = 0; i < tabNum;i++ )
        {
            tp = new TabPage(""+i);
            dgw = new DataGridView();
            dgw.DataSource = left[i];
            tp.Controls.Add(dgw);
            tbcLeftPages.Controls.Add(tp);
            tbcLeftPages.Refresh();
        }

    }

它会打开一个选项卡控件,其中包含适量的选项卡,但其中没有数据

编辑 1仍然不好,显示没有 gridview 的选项卡将其更改为获取数据表数组部分的函数

 public void addDGWtoTab(DataTable dt,string side,int num)
        {
        MessageBox.Show("table:" + side + " bucket:" + num + "rows:" + dt.Rows.Count);
        DataGridView dgw = new DataGridView();
        TabPage tp = new TabPage();

        //data grid view
        dgw.Name = "dgv" + num;
        dgw.AutoSize = true;
        dgw.Dock = DockStyle.Fill;

        //tab page
        tp.Name = "tp" + num;
        tp.Text = "Bucket " + num;
        tp.Tag = dt.Rows.Count;
        tp.TabIndex = num;

        if (side == "left")             
            tbcLeftPages.Controls.Add(tp);   
        else tbcRightPages.Controls.Add(tp);
        dgw.DataSource = dt; 
        tp.Controls.Add(dgw);

    }

EDIT 2 添加了spitDT

public DataTable[] splitTable(DataTable mainDT,string columnName,int mod)
    {
        DataTable[] splitDT = new DataTable[11];
        for (int i=0;i<11;i++)
            splitDT[i]=new DataTable();

        int splitINT;
        int tmp=0;

        foreach (DataRow row in mainDT.Rows)
        {              
           splitINT = row[columnName].GetHashCode();
           tmp = splitINT % mod;
           splitDT[tmp].ImportRow(row);
        }

        return splitDT;
    }

编辑 3与消息分开

public DataTable[] splitTable(DataTable mainDT,string columnName,int mod)
    {
        DataTable[] splitDT = new DataTable[11];
        for (int i=0;i<11;i++)
            splitDT[i]=new DataTable();

        int splitINT;
        int tmp=0;

        foreach (DataRow row in mainDT.Rows)
        {              
           splitINT = row[columnName].GetHashCode();
           tmp = splitINT % mod;
           splitDT[tmp].ImportRow(row);
           MessageBox.Show("value:" + row[columnName].ToString() + "splitINT:" + splitINT + "mod:" + mod +
                            " to table:" + tmp);
            MessageBox.Show("" + splitDT[tmp].Rows.Count);
        }

        return splitDT;
    }
4

3 回答 3

1

没有架构的 DataTable 上的 ImportRow 不会产生任何结果。

public DataTable[] splitTable(DataTable mainDT,string columnName,int mod)
{
    DataTable[] splitDT = new DataTable[11];
    for (int i=0;i<11;i++)
    {
        // Create a datatable with the same structure (schema) of the source table
        splitDT[i] = mainDT.Clone();
    }

    int splitINT;
    int tmp=0;

    foreach (DataRow row in mainDT.Rows)
    {              
       splitINT = row[columnName].GetHashCode();
       tmp = splitINT % mod;
       splitDT[tmp].ImportRow(row);
    }

    return splitDT;
}

此代码仅复制一列,而不是整组列。也许您的代码应该创建一个只有要从中复制的列的数据表。

于 2013-06-06T09:53:47.903 回答
0

因为您的dgw尺寸为 0,0 任一设置尺寸:

dgw.Size = new Size(100, 100);

或将停靠类型设置为填充:

dgw.Dock = System.Windows.Forms.DockStyle.Fill;
于 2013-06-04T11:54:28.077 回答
0

在您创建新 s 的两个地方DataGridView,您都设置了数据源 - 这将允许它访问行,但您没有告诉它自己处理列。设置;AutoGenerateColumns之前尝试设置 IEtrue DataSource

DataGridView dgw = new DataGridView();
TabPage tp = new TabPage();

//data grid view
dgw.AutoGenerateColumns = true; // <====== added this line
dgw.Name = "dgv" + num;
dgw.AutoSize = true;
dgw.Dock = DockStyle.Fill;

// ... some here not shown

dgw.DataSource = dt; 

和:

dgw = new DataGridView();
dgw.AutoGenerateColumns = true; // <====== added this line
dgw.DataSource = left[i];
于 2013-06-06T09:47:45.940 回答