2010-03-08 129 views
2

如何在C#中动态创建DataGridView?你能举个例子吗?如何在C#中动态创建DataGridView?

+0

你在使用WinForms,WPF,ASP.NET吗?当我看到标签'datagridview时,我想到了.NET 2.0引入的WinForms控件。其他人(就像你在下面的答案中看到的那样)会根据ASP.NET思考,以及在System.Web.UI.WebControls中找到的'GridView。请澄清,谢谢。 – BillW 2010-03-08 09:56:36

+0

@BillW我正在使用WinForms – ratty 2010-03-08 10:18:21

回答

6

您可以像任何其他控件创建它。

发生在你的页面的占位符控制(这将作为起点)

让你的页面看起来像

<body> 
    <form id="form" runat="server" /> 
    <asp:PlaceHolder id="ph" runat="server" /> 
</body> 

然后,在后面的代码,只需创建和控件添加到地方Holder

// Let's create our Object That contains the data to show in our Grid first 
string[] myData = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I" }; 

// Create the Object 
GridView gv = new GridView(); 

// Assign some properties 
gv.ID = "myGridID"; 
gv.AutoGenerateColumns = true; 

// Assing Data (this can be a DataTable or you can create each column through Columns Colecction) 
gv.DataSource = myData; 
gv.DataBind(); 

// Now that we have our Grid all set up, let's add it to our Place Holder Control 
ph.Controls.Add(gv); 

也许你想添加更多的控件?

// How about adding a Label as well? 
Label lbl = new Label; 
lbl.ID = "MyLabelID"; 
lbl.Text = String.Format("Grid contains {0} row(s).", myData.Length); 

ph.Controls.Add(lbl); 

// Done! 

希望它可以帮助你开始

+0

+1 @balexandre:很好/快速的起点。 @ratty:如果您想要更多地了解动态添加控件,请查看:http://izlooite.blogspot.com/search/label/Dynamic%20Controls – 2010-03-08 10:01:22

3

根据您使用的WinForms您的回复。首先是一个非常简单的例子,然后根据“典型”使用场景对需要考虑的问题进行一些讨论。

这里的地方,在回答关于在运行时点击一个按钮,创建一个新的DataGridView一个具体的例子,定位在表格上,大小等:

// declare a form scoped variable to hold a reference 
// to the run-time created DataGridview 
private DataGridView RunTimeCreatedDataGridView; 

// sample of creating the DataGridView at run-time 
// and adding to the Controls collection of a Form 
// and positioning and sizing 
// fyi: the Form used here is sized 800 by 600 
private void button1_Click(object sender, EventArgs e) 
{ 
    RunTimeCreatedDataGridView= new DataGridView(); 
    RunTimeCreatedDataGridView.Size = new Size(300, 542); 
    RunTimeCreatedDataGridView.Location = new Point(10,12); 
    this.Controls.Add(RunTimeCreatedDataGridView); 
} 

你可以的当然简化了设置的大小和使用bounds属性位置,或方法“的setBounds如:

RunTimeCreatedDataGridView.SetBounds(10,12,300,542); 

您不妨设置确定大小和位置设置Dock或锚属性‘自动’其他属性。

你可能会想要通过添加调用来将BackGroundColor,BorderStyle等设置为上述代码来以其他方式“定制配置”DataGridView的视觉外观。这个时候,我希望你能想到这样的事情:“真的重要的事情,如配置列,数据绑定等?”通过DataGridView右上角的“智能标签”和属性浏览器窗口,在DesignTime中暴露的所有美妙功能如何?

这里是我们得到一般的地方,而不是具体的。

如果您肯定某些时候运行时用户想要创建DataGridView:为什么不提前创建它:以可视化的方式设置它,创建列等等,然后隐藏它表格载入:然后按需显示。

如果您绝对必须在运行时从头开始创建DataGridView,但要避免大量输入:首先在设计时创建DataGridView,然后进入Designer。cs文件并将自动生成的代码复制出来,这些代码对于视觉样式,添加和配置列非常有用:然后将该代码粘贴到创建DataGridView的方法或事件中(是的,您需要稍微调整它)。因为,在这种情况下,我们对你可能会或可能不会将DataGridView绑定到什么都一无所知,所以我们将只保留这个“妈妈”。

在(奇?关闭的机会?)要创建在运行时多DataGridViews不太可能的情况下,建议你在一个变量维护它们的内部列表像List<DataGridView>和有一个变量命名为currentDataGridView,你可以依靠保存对当前活动(具有焦点,可见等)DataGridView的引用。

在任何情况下,我都建议在设计时模式下使用DataGridView“乱搞”,然后检查Designer.cs文件中生成的代码(但不要更改它!)以获取有关如何使用的快速信息DataGridView的各种功能。对于将DataGridView绑定到复杂数据源和格式的主要示例:请检查CodeProject的相关文章,提示等。

“收获”您需要从Designer.cs文件中自动生成的代码中获取所需内容,然后,当你准备好时,在表单上删除DataGridView实例,并在运行时“做你自己的事情”。