簡體   English   中英

Datagrid不顯示Winforms中的數據

[英]Datagrid not showing data in Winforms

我是Winforms的新手,只是嘗試具有2個表單的示例應用程序。

  • Form1具有文本框,組合框,單選按鈕和一個提交按鈕
  • Form2有一個數據網格,該網格應該顯示Form1發送的數據。

Form1中

  DataFetch fetch ; public Form1() { InitializeComponent(); fetch = new DataFetch(); comboBox1.DataSource = fetch.getDesignation(); comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "Id"; radioButton1.Checked = true; } private void button1_Click(object sender, EventArgs e) { Employee employee = new Employee(); employee.Designation = comboBox1.SelectedText==""?"Admin":comboBox1.SelectedText; employee.IsCertified = radioButton1.Checked == true ? true : false; employee.name = textBox1.Text; Form2 form = new Form2(employee); form.Show(); } 

窗體2

  public partial class Form2 : Form { public Form2(Employee employee) { InitializeComponent(); dataGridView1.DataSource = employee; } } 

Form2設計器代碼

  partial class Form2 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.dataGridView1 = new System.Windows.Forms.DataGridView(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Location = new System.Drawing.Point(72, 93); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.Size = new System.Drawing.Size(240, 150); this.dataGridView1.TabIndex = 0; this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick_1); // // Form2 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(460, 346); this.Controls.Add(this.dataGridView1); this.Name = "Form2"; this.Text = "Form2"; ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.DataGridView dataGridView1; } 


雖然我可以在Form2的構造函數中獲取數據,但在這種情況下我無法將其綁定到網格上以將其顯示到網格上。

DataGridView's DataSource通常是一個集合,我不確定為什么要嘗試將單個對象綁定到它。 但是無論如何,您可以初始化一個單項列表,然后將其綁定到datagridview1的數據源。

例如:

datagridview1.DataSource = new List<Employee> { employee };

你可以輕松做到

您要添加的所有項目都添加到數據表中,並發送到表單2上,然后將其綁定到gridview上

ihtink將為您提供幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM