问题
i have a small program that takes a datatable (takes data from sql database) then splits it into datatable array by field and then should display it in a tabcontrol, each field in it's own tab
the split, takes single datatable and splits into datatable array, works fine i think
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;
}
here is the problem part
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();
}
}
it opens a tabcontrol with the right amount of tabs but no data in them
EDIT 1 still no good, show tabs with no gridview changed it into a function that get's parts of the datatable array
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 added 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 split with messages
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;
}
回答1:
The ImportRow on a DataTable without schema doesn't produce any result.
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;
}
This code copies only a column, not the Whole set of columns. Perhaps your code should create a datatable with only the column to copy from.
回答2:
Because your dgw
size is 0,0 either set size:
dgw.Size = new Size(100, 100);
or set dock type to fill:
dgw.Dock = System.Windows.Forms.DockStyle.Fill;
回答3:
In both places where you create the new DataGridView
s, you have set the data-source - which will allow it to access the rows, but you haven't told it to handle the columns itself. Try setting AutoGenerateColumns
to true
before you set the DataSource
; i.e.
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;
and:
dgw = new DataGridView();
dgw.AutoGenerateColumns = true; // <====== added this line
dgw.DataSource = left[i];
来源:https://stackoverflow.com/questions/16917046/displaying-multiple-datatable-in-tabcontrol-c-sharp