dataGridView如何添加数据

我想手动添加一组数据,不用链接数据库,看到网上有个说法:利用泛型集合向DataGridView中添加数据关键代码:(List<>泛型集合)privatevoidForm1... 我想手动添加一组数据,不用链接数据库,看到网上有个说法:
利用泛型集合向DataGridView中添加数据
关键代码:(List<>泛型集合)
private void Form1_Load(object sender, EventArgs e)
{
//使用List<>泛型集合填充DataGridView
List<Student> students = new List<Student>();
Student hat = new Student("Hathaway", "12", "Male");
Student peter = new Student("Peter","14","Male");
Student dell = new Student("Dell","16","Male");
Student anne = new Student("Anne","19","Female");
students.Add(hat);
students.Add(peter);
students.Add(dell);
students.Add(anne);
this.dataGridView1.DataSource = students;
}
照做后,结果dataGridView中什么显示都没有,这是怎么回事,我要怎么改?
展开
 我来答
star_ais
推荐于2016-08-23 · 超过13用户采纳过TA的回答
知道答主
回答量:41
采纳率:0%
帮助的人:19.9万
展开全部
你只是照搬别人的代码,并没有理解什么是List泛型,以下是你代码的解释,希望对你有帮助。
private void Form1_Load(object sender, EventArgs e)
{
//使用List<>泛型集合填充DataGridView
List<Student> students = new List<Student>();’=====此行是定义泛型,其中Student是一个自定义的类,你抄代码的时候肯定少抄了关于sutdent的定义
Student hat = new Student("Hathaway", "12", "Male");‘===实例化student,并赋值。
Student peter = new Student("Peter","14","Male");’====同上
Student dell = new Student("Dell","16","Male");‘====同上
Student anne = new Student("Anne","19","Female");’====同上
students.Add(hat);‘===想泛型List中添加实例化的对象
students.Add(peter);’====同上
students.Add(dell);’====同上
students.Add(anne);’====同上
this.dataGridView1.DataSource = students;‘===给dataGridView赋值。
}
根据以上代码不难看出student对象应该有名称,年龄,性别三个属性。
追问
我定义过Student这个类啊,要是不添加,程序都不能编译通过。。。在dataGridView中就是显示不出来添加的数据,不知道为什么才问的
class Student
{
public Student(string a, string b, string c)
{
name = a;
age = b;
gender = c;
}
public string name;
public string age;
public string gender;
}
追答
你会使用调试吗?如果会的话,调试一下,跟踪Student里面是否添加了内容。
因为我没有调试,不过单看代码应该没有什么问题。
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
du瓶邪
推荐于2016-11-01 · TA获得超过2.4万个赞
知道大有可为答主
回答量:1.7万
采纳率:100%
帮助的人:3239万
展开全部
方式如下:
private void setDataGridView(string GoodsId)
{
if (h_Acticle_repertorys != null)
{
int number = 1;
foreach (H_Acticle_Repertory h_acticle1 in getRepertoryByGoodsId(GoodsId))
{
this.dgvTotal.Rows.Add(number, getGoodsByID(h_acticle1.ActicleId.ToString()).ActicleName, getGoodsByID(h_acticle1.ActicleId.ToString()).WorkShop, h_acticle1.ImportNum, h_acticle1.ImportTime, h_acticle1.HeadName);
number++;//添加的一个序号
}

}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
fufknp
2011-03-06 · TA获得超过397个赞
知道小有建树答主
回答量:463
采纳率:0%
帮助的人:0
展开全部
taGridView.Rows.count;i++)
{
string a=textBoxEmpID.Text ;
string b=this.dataGridView1.Rows[i][0].ToString();
string c=this.dataGridView1.Rows[i][1].ToString();
string d=this.dataGridView1.Rows[i][2].ToString();
insert into Salary(EmployeeID,Income,Outcome,Note3) values(a,b,c,d)
}
追问
能否就上面我给出的代码进行解答?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
大发园长的更新Cz
推荐于2018-03-22
知道答主
回答量:25
采纳率:0%
帮助的人:7万
展开全部
dgvDetail.DataBindings();
你少写了一行代码,还要绑定值
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Treelab
2020-12-26 · 贡献了超过299个回答
知道答主
回答量:299
采纳率:100%
帮助的人:14.8万
展开全部

本节视频我们来聊一聊如何添加数据表的行与列。Treelab 官网:www.treelab.com.cn

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式