Display multiple data in tabcontrol c #

I have a small program that takes a datatable (takes data from a sql database) then splits it into an array of data by field and then displays it in tabcontrol, each field in its own tab

split, takes a single datatable and splits into a 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

 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 desired number of tabs, but there is no data in them

EDIT 1 so far nothing good, show tabs without gridview changed it to a function that receives parts of the data 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 by 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 with post

 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; } 
+2
source share
3 answers

ImportRow in a DataTable without a schema produces no 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 only copies a column, not the entire set of columns. Perhaps your code should create a datatable with only a column to copy.

+1
source

Since your dgw size is 0.0 or a given size:

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

or set the type of dock to fill in:

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

In both places where you create the new DataGridView s, you set up a data source that will allow it to access the rows, but you did not tell it to process the columns themselves. 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]; 
0
source

Source: https://habr.com/ru/post/946669/


All Articles