在tabcontrol C#中显示多个数据表

时间:2013-06-04 11:50:25

标签: c# datagridview datatable tabcontrol

我有一个小程序,它接受一个数据表(从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();
        }

    }

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

编辑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);

    }

编辑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;
    }

EDIT 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;
    }

3 个答案:

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

此代码仅复制列,而不复制整个列集。也许你的代码应该创建一个只有要复制的列的数据表。

答案 1 :(得分:0)

因为您的dgw大小为0,0,无论是设置大小:

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

或设置码头类型以填充:

dgw.Dock = System.Windows.Forms.DockStyle.Fill;

答案 2 :(得分:0)

在您创建新DataGridView的两个地方,您都设置了数据源 - 这将允许它访问行,但您还没有告诉它自己处理列。在 设置AutoGenerateColumns之前,请尝试将true设置为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];