2017-09-22 2 views
0

Ich bin neu in Winforms und versuchte gerade eine Beispielanwendung mit 2 Forms.
Datagrid zeigt keine Daten in Winforms an

  • Form1 wird mit Textbox, Combobox, Radiobutton und eine Absenden-Button
  • Form2 hat einen Datagrid, die die Daten sollten von Form1 gesendet wird, angezeigt.

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(); 
 
     }

Form2

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

Form2 Designer-Code

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; 
 
    }


Obwohl ich in der Lage bin Daten im Konstruktor von Form2 zu bekommen ich in dieser auf nicht in der Lage es an das Netz zu binden, noch bin Fall zeigt das zum Gitter.

+0

Haben Sie 'AutoGenerateColumns' für' dataGridView1' in Designer? Es wäre besser, wenn Sie den Designer für 'dataGridView1' freigeben. – praty

Antwort

1

DataGridView'sDataSource Im Allgemeinen ist eine Sammlung und ich bin mir nicht sicher, warum versuchen Sie, ein einzelnes Objekt daran zu binden. Wie auch immer, Sie können eine Liste mit einem Eintrag initialisieren und sie dann an die Datenquelle datagridview1 binden.

Zum Beispiel:

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

Vielen Dank. Das hat für mich funktioniert. :) –

+0

Gern geschehen. – SeM

0

Sie die leicht

haben kann

alle Artikel von Ihnen in Datentabelle hinzufügen möchten, und dass auf dem Formular 2 senden und dann binden sie auf gridview

ihtink es wird Ihnen helfen