
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中什么显示都没有,这是怎么回事,我要怎么改? 展开
利用泛型集合向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中什么显示都没有,这是怎么回事,我要怎么改? 展开
7个回答
展开全部
你只是照搬别人的代码,并没有理解什么是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对象应该有名称,年龄,性别三个属性。
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里面是否添加了内容。
因为我没有调试,不过单看代码应该没有什么问题。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
方式如下:
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++;//添加的一个序号
}
}
}
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++;//添加的一个序号
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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)
}
{
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)
}
追问
能否就上面我给出的代码进行解答?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
dgvDetail.DataBindings();
你少写了一行代码,还要绑定值
你少写了一行代码,还要绑定值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
本节视频我们来聊一聊如何添加数据表的行与列。Treelab 官网:www.treelab.com.cn
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询