用c创建一个datagridview表单

本文关键字:一个 datagridview 表单 创建 | 更新日期: 2023-09-27 17:58:16

我是c#和windows窗体应用程序的新手。现在,我想在我的表单中创建一个Datagridview,我想用业务对象的属性填充它的行。我遵循了这个msdn页面中的示例:如何:将对象绑定到Windows窗体DataGridView控件并创建了自己的程序,但我没有得到与msdn示例中类似的结果,而是得到了一个有三个空行的DataGridView。我做错了什么?这是我的程序:

using System;
using System.Windows.Forms;

public class Form3 : Form
{
    private DataGridView dataGridView1 = new DataGridView();
    private BindingSource bindingSource1 = new BindingSource();
    public Form3()
    {            
        this.Load += new System.EventHandler(EnumsAndComboBox_Load);
    }
    private void EnumsAndComboBox_Load(object sender, System.EventArgs e)
    {
        // Populate the data source.           
        bindingSource1.Add(new Test("bli", "bla", "blop", "ha", "ho", "he"));
        bindingSource1.Add(new Test("bli", "bla", "blop", "ha", "ho", "he"));
        // Initialize the DataGridView.
        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.AutoSize = true;
        dataGridView1.DataSource = bindingSource1;
        // Initialize and add a text box column.
        DataGridViewColumn column = new DataGridViewTextBoxColumn();
        column.DataPropertyName = "Name1";
        column.Name = "Name1";
        dataGridView1.Columns.Add(column);
        // Initialize and add a check box column.
        column = new DataGridViewTextBoxColumn();
        column.DataPropertyName = "Name2";
        column.Name = "Name2";
        dataGridView1.Columns.Add(column);
        column = new DataGridViewTextBoxColumn();
        column.DataPropertyName = "Name3";
        column.Name = "Name3";
        dataGridView1.Columns.Add(column);
        column = new DataGridViewTextBoxColumn();
        column.DataPropertyName = "Name4";
        column.Name = "Name4";
        dataGridView1.Columns.Add(column);
        column = new DataGridViewTextBoxColumn();
        column.DataPropertyName = "Name5";
        column.Name = "Name5";
        dataGridView1.Columns.Add(column);
        column = new DataGridViewTextBoxColumn();
        column.DataPropertyName = "Name6";
        column.Name = "Name6";
        dataGridView1.Columns.Add(column);
        // Initialize the form.
        this.Controls.Add(dataGridView1);
        this.AutoSize = true;
        this.Text = "DataGridView object binding demo";
    }
    #region "test object"
    private class Test
    {
        private string test1;
        private string test2;
        private string test3;
        private string test4;
        private string test5;
        private string test6;
        public Test(string s1, string s2, string s3, string s4, string s5, string s6)
        {
            test1 = s1;
            test2 = s2;
            test3 = s3;
            test4 = s4;
            test5 = s5;
            test6 = s6;
        }
        public Test()
        {
            test1 = "bla";
            test2 = "bla";
            test3 = "bla";
            test4 = "bla";
            test5 = "bla";
            test6 = "bla";
        }

        public string Test1
        {
            get
            {
                return test1;
            }
            set
            {
                test1 = value;
            }
        }
        public string Test2
        {
            get
            {
                return test2;
            }
            set
            {
                test2 = value;
            }
        }
        public string Test3
        {
            get
            {
                return test3;
            }
            set
            {
                test3 = value;
            }
        }
        public string Test4
        {
            get
            {
                return test4;
            }
            set
            {
                test4 = value;
            }
        }
        public string Test5
        {
            get
            {
                return test5;
            }
            set
            {
                test5 = value;
            }
        }
        public string Test6
        {
            get
            {
                return test6;
            }
            set
            {
                test6 = value;
            }
        }
    }
    #endregion
    static class Program
    {
        [STAThread]
        public static void Main()
        {
            Application.Run(new Form3());
        }
    }
}

用c创建一个datagridview表单

您正在将列绑定到从1到6的名为Name的属性,但在对象中没有这样的属性。将Name更改为Test。Ie

来自:

column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "Name6";
column.Name = "Name6";
dataGridView1.Columns.Add(column);

到此:

column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "Test6";
column.Name = "Name6";
dataGridView1.Columns.Add(column);

也正如@noobobob建议的那样,移动你的

dataGridView1.DataSource = bindingSource1;

到方法结束

每列的"DataPropertyName"属性必须与要计算的数据对象的属性名称匹配。所以,如果你只做一个这样的微小改变:

column.DataPropertyName = "Test1";

一切都应该正常。

column.DataPropertyName应设置为您在数据源类中指定的属性名称。试试这个:

    column = new DataGridViewTextBoxColumn();
    column.DataPropertyName = "Test1";
    column.Name = "Test1";
    dataGridView1.Columns.Add(column);
    column = new DataGridViewTextBoxColumn();
    column.DataPropertyName = "Test2";
    column.Name = "Test2";
    dataGridView1.Columns.Add(column);
    column = new DataGridViewTextBoxColumn();
    column.DataPropertyName = "Test3";
    column.Name = "Test3";
    dataGridView1.Columns.Add(column);