用一个循环生成多个标签或文本框窗体上。在C#里如何实现? 连接数据库后返回一个记录集或ArrayList,可以从中抽出数据库中的字段名,然后在在应用程序中自动生成与字段名一致的标签。在VB中用Load方法可以实现的。但在C#中请问如何实现呢?也就是说用一个循环生成多个标签或文本框在窗体上。在C#里如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 生成标签对象跟生成其它的对象没有什么区别label lb=new label();lb.text=...;controls.add(lb); int n = 10;label [] lbs=new label[n];for (int i =0 ;i < n ;i ++){ lbs[i] = …; lbs.Location = …; controls.add(lbs[i]);} 楼上两位能举一个详细地址来看看吗?我在窗体的LOAD方法写上以上代码会报这样的错:未处理的“System.NullReferenceException”类型的异常出现在 ch10.exe 中。其他信息: 未将对象引用设置到对象的实例。 vs2005,分开说。1、连接数据库后返回一个记录集或ArrayList。以Northwind数据库为例using System.Data.SqlClient;...private System.Collections.ArrayList al = new System.Collections.ArrayList();... using (SqlConnection conn = new SqlConnection("server=localhost;database=northwind;uid=sa;pwd=")){ SqlCommand cmd = new SqlCommand(); cmd.CommandText = "select * from Employees"; cmd.Connection = conn; conn.Open(); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); foreach (System.Data.Common.DbDataRecord rec in reader) { al.Add(rec); } //要查询字段信息,一行记录就够了。 System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)al[0]; Label label; for (int i = 0; i < record.FieldCount; i++) { label = new Label(); label.Name = "label" + record.GetName(i); label.Text = record.GetName(i); label.Location = new Point(30, i * 25); this.Controls.Add(label); TextBox textBox = new TextBox(); textBox.Name = "textBox" + record.GetName(i); textBox.Location = new Point(150, i * 25); this.Controls.Add(textBox); }} 谢谢楼上的,原来是不可以在循环里将label写成带下标的。一次循环生成一个。谢谢! gridcontrol导出报表 请问如何从string转成varbinary 急……html代码保存问题! 如果查询字符串中包含3个SQL语句,则记录集中就有3个表,如何在ds填充时分别给这三个表命不同的名字,替换缺省名“Table”、“Table1”、“Table2”。见例子: 关于WAP浏览器 windows应用程序写时钟代码 请教:用listbox多查询条件的搜索问题 如何用SQL语言备份和还原数据库 这代表什么意思? c#小白,请大牛帮忙看个问题 C# 与Java 那个易学些? 用C#抓取网页的问题
label lb=new label();
lb.text=...;
controls.add(lb);
label [] lbs=new label[n];
for (int i =0 ;i < n ;i ++)
{
lbs[i] = …;
lbs.Location = …;
controls.add(lbs[i]);
}
我在窗体的LOAD方法写上以上代码会报这样的错:未处理的“System.NullReferenceException”类型的异常出现在 ch10.exe 中。其他信息: 未将对象引用设置到对象的实例。
1、连接数据库后返回一个记录集或ArrayList。以Northwind数据库为例
using System.Data.SqlClient;
...
private System.Collections.ArrayList al = new System.Collections.ArrayList();
...
using (SqlConnection conn = new SqlConnection("server=localhost;database=northwind;uid=sa;pwd="))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from Employees";
cmd.Connection = conn; conn.Open(); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); foreach (System.Data.Common.DbDataRecord rec in reader)
{
al.Add(rec);
} //要查询字段信息,一行记录就够了。
System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)al[0]; Label label;
for (int i = 0; i < record.FieldCount; i++)
{
label = new Label();
label.Name = "label" + record.GetName(i);
label.Text = record.GetName(i);
label.Location = new Point(30, i * 25);
this.Controls.Add(label); TextBox textBox = new TextBox();
textBox.Name = "textBox" + record.GetName(i);
textBox.Location = new Point(150, i * 25);
this.Controls.Add(textBox);
}
}