【问题标题】:Datagrid not showing data in WinformsDatagrid 未在 Winforms 中显示数据
【发布时间】:2018-03-03 17:22:00
【问题描述】:

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

Form2

 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 的构造函数中获取数据,但我无法将其绑定到网格上,在这种情况下显示到网格。

【问题讨论】:

  • 您是否在设计器中为dataGridView1 设置了AutoGenerateColumns?如果你分享dataGridView1的设计师会更好。

标签: c# winforms datagridview datagrid


【解决方案1】:

DataGridView's DataSource 通常是一个集合,我不确定你为什么要尝试将单个对象绑定到它。不过不管怎样,你可以初始化一个单项List,然后绑定到datagridview1的数据源。

例如:

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

【讨论】:

    【解决方案2】:

    你可以轻松做到这一点

    您想要添加到数据表中并在表格 2 上发送的所有项目 然后绑定到gridview上

    我会帮你的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      相关资源
      最近更新 更多