在c#中创建一个datagridview表单 [英] creating a datagridview form in c#
问题描述
现在,我想在我的表单中创建一个Datagridview,其行要填充一个业务对象的属性。我按照这个msdn页面的例子:如何:将对象绑定到Windows窗体DataGridView控件并创建了我自己的程序,但是不像msdn示例中得到类似的结果,我得到一个带有三个空行的datagridview。我究竟做错了什么?这是我的程序:
using System;
使用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)
{
//填充数据源。
bindingSource1.Add(new Test(bli,bla,blop,ha,ho,he));
bindingSource1.Add(new Test(bli,bla,blop,ha,ho,he));
//初始化DataGridView。
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AutoSize = true;
dataGridView1.DataSource = bindingSource1;
//初始化并添加一个文本框列。
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName =Name1;
column.Name =Name1;
dataGridView1.Columns.Add(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);
//初始化表单。
this.Controls.Add(dataGridView1);
this.AutoSize = true;
this.Text =DataGridView对象绑定演示;
}
#regiontest 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
静态类程序
{
[STAThread]
public static void Main()
{
Application.Run(new Form3());
}
}
}
每个列的DataPropertyName属性必须与正在评估的数据对象的属性的名称相匹配。所以如果你只是这样做一个微小的改变:
column.DataPropertyName =Test1;
一切都应该可以工作。
I`m new to c# and windows form applications. Right now, I want to create a Datagridview within my form, whose rows I want to fill with the properties of a business object. I followed the example from this msdn page: How to: Bind Objects to Windows Forms DataGridView Controls and created my own program, but instead of getting a similar result as in the msdn example I get a datagridview with three empty rows. What am I doing wrong? Here is my program:
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());
}
}
}
"DataPropertyName" property of every column must match the name of a property of the data object being evaluated. So if you just make one tiny change like this:
column.DataPropertyName = "Test1";
everything should work.
这篇关于在c#中创建一个datagridview表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!