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