[英]Exporting Data From Multiple DataGridViews on a Form to Multiple Excel Sheets in a Single Excel File
[英]Multiple DataGridViews on 1 Form
我有一個顯示DataGridView的簡單表單; 並且我想在同一表格的第一個下方顯示第二個DataGridView對象。
但是,當我運行代碼時,它僅顯示第一個。 通過調試器運行時,該表單列出它具有鏈接到其的兩個datagridviews。
System.Windows.Forms.Form form
= new System.Windows.Forms.Form();
form.Size = new System.Drawing.Size(450, 400);
form.Text = "Form";
DataGridView dg = new DataGridView();
dg.AllowUserToAddRows = false;
dg.AllowUserToDeleteRows = false;
dg.AllowUserToOrderColumns = true;
dg.Dock = System.Windows.Forms.DockStyle.Fill;
dg.Location = new System.Drawing.Point(0, 0);
dg.ReadOnly = true;
dg.TabIndex = 0;
dg.DataSource = dt;
dg.Parent = form;
DataGridView dgHangers = new DataGridView();
dgHangers.AllowUserToAddRows = false;
dgHangers.AllowUserToDeleteRows = false;
dgHangers.AllowUserToOrderColumns = true;
dgHangers.Dock = System.Windows.Forms.DockStyle.Fill;
// attempting get the bottom of the first DataGridView() so the second will display below.
dgHangers.Location = new System.Drawing.Point(0, dg.DisplayRectangle.Bottom);
dgHangers.ReadOnly = true;
dgHangers.TabIndex = 1;
dgHangers.DataSource = hangerTable;
dgHangers.Parent = form;
form.ShowDialog();
形式:
首先:使用System.Windows.Forms.DockStyle.Fill;
您看不到兩個dataGrid都創建了,因此請為您的兩個dataGridView沒有這一行進行測試。
或者您可以使用第一個: dg.Dock = System.Windows.Forms.DockStyle.Top;
第二個: dg.Dock = System.Windows.Forms.DockStyle.Bottom;
你也可以使用form.Controls.Add(dg);
而不是dg.Parent = form;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.