简体   繁体   English

Datagrid不显示Winforms中的数据

[英]Datagrid not showing data in Winforms

I am new to Winforms and was just trying a Sample Application having 2 Forms. 我是Winforms的新手,只是尝试具有2个表单的示例应用程序。

  • Form1 is having textbox, combobox , radiobutton and a submit button Form1具有文本框,组合框,单选按钮和一个提交按钮
  • Form2 has a datagrid which is supposed to show the data sent by Form1. Form2有一个数据网格,该网格应该显示Form1发送的数据。

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

Form2 窗体2

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

Form2 Designer Code 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; } 


Though i am able to get Data in the constructor of Form2 yet i am not able to bind it to the grid on in this case show that to the grid. 虽然我可以在Form2的构造函数中获取数据,但在这种情况下我无法将其绑定到网格上以将其显示到网格上。

DataGridView's DataSource generally is a collection and I'm not sure why are you trying to bind a single object to it. DataGridView's DataSource通常是一个集合,我不确定为什么要尝试将单个对象绑定到它。 But anyway, you can initialize a one-item List, then bind it to datagridview1 's datasource. 但是无论如何,您可以初始化一个单项列表,然后将其绑定到datagridview1的数据源。

For example: 例如:

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

you can did that easily 你可以轻松做到

all items of you want add in datatable and that send on Form 2 and then bind it on gridview 您要添加的所有项目都添加到数据表中,并发送到表单2上,然后将其绑定到gridview上

ihtink it will help you ihtink将为您提供帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM