DataGridView 控件

miflower 2009-09-22 05:08:46
建立一个 DataGridView控件,定义了2列,一个叫姓名 ,一个叫性别。
不是用绑定数据源,这里其实也用不到。
我有个泛型,是用类来存储字段,里面假定存储着这样的3条数据:
LiLi Man, Jim Man,Sala Woman.
点击一个按钮以后 我想把这样的3条数据 依次映射到DataGridView控件中 ,如何实现,谢谢大家。
分就这么多,希望谅解。

...全文
50 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
superfrank0711 2009-09-22
  • 打赏
  • 举报
回复
lz可以,创建个实体类,然后通过sql查询返回IList<Info>泛型集合,之后直接把这个泛型集合作为DataGridView的数据源 就OK了!大致思路和楼上的差不多!
BATTLERxANGE 2009-09-22
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<class1> listc1 = new List<class1>();
class1 c1 = new class1("a", "n");
listc1.Add(c1);
class1 c2 = new class1("b", "m");
listc1.Add(c2);
dataGridView1.DataSource = listc1;
}
}
public class class1
{
string name;
string sex;
public class1(string Name, string Sex)
{
_name = Name;
_sex = Sex;
}
public string _name
{
get { return name; }
set { name = value; }
}
public string _sex
{
get { return sex; }
set { sex = value; }
}
}
}
superfrank0711 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xuenzhen123 的回复:]
大概的写了个 跟你的字段名字对不上 你自己参考改改吧

C# codeprivatevoid btnTest_Click(object sender, EventArgs e)
{
List<Info> list=new List<Info>();
Info info1=new Info();
info1.Name1="xxx";
info1.Sex1="male";
Info info2=new Info();
info2.Name1="yyy";
info2.Sex1="male";
Info info3=new Info();
info3.Name1="zzz";
info3.Sex1="female";
list.Add(info1);
list.Add(info2);
list.Add(info3);

dgvTest.DataSource= list;
}class Info
{privatestring Name;publicstring Name1
{get {return Name; }set { Name= value; }
}privatestring Sex;publicstring Sex1
{get {return Sex; }set { Sex= value; }
}
}
[/Quote]


OK!这种方法可以!
xuenzhen123 2009-09-22
  • 打赏
  • 举报
回复
大概的写了个 跟你的字段名字对不上 你自己参考改改吧


private void btnTest_Click(object sender, EventArgs e)
{
List<Info> list = new List<Info>();
Info info1 = new Info();
info1.Name1 = "xxx";
info1.Sex1 = "male";
Info info2 = new Info();
info2.Name1 = "yyy";
info2.Sex1 = "male";
Info info3 = new Info();
info3.Name1 = "zzz";
info3.Sex1 = "female";
list.Add(info1);
list.Add(info2);
list.Add(info3);

dgvTest.DataSource = list;
}

class Info
{
private string Name;

public string Name1
{
get { return Name; }
set { Name = value; }
}
private string Sex;

public string Sex1
{
get { return Sex; }
set { Sex = value; }
}
}
miflower 2009-09-22
  • 打赏
  • 举报
回复
回答 问题啦
大家
等的好辛苦
miflower 2009-09-22
  • 打赏
  • 举报
回复
没有人回答吗 谢谢大家
急用
vb.net操作DataGridView控件的用法的集合,包括: 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一列新追加行非表示 4. DataGridView判断当前选中行是否为新追加的行 5. DataGridView删除行可否设定 6. DataGridView行列不表示和删除 DataGridView控件用法合集(二) 7. DataGridView行列宽度高度设置为不能编辑 8. DataGridView行高列幅自动调整 9. DataGridView指定行列冻结 10. DataGridView列顺序变更可否设定 11. DataGridView行复数选择 12. DataGridView选择的行、列、单元格取得 DataGridView控件用法合集(三) 13. DataGridView指定单元格是否表示 14. DataGridView表头部单元格取得 15. DataGridView表头部单元格文字列设定 16. DataGridView选择的部分拷贝至剪贴板 17.DataGridView粘贴 18. DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息) DataGridView控件用法合集(四) 19. DataGridView中的ContextMenuStrip属性 20. DataGridView指定滚动框位置 21. DataGridView手动追加列 22. DataGridView全体分界线样式设置 23. DataGridView根据单元格属性更改显示内容 24. DataGridView新追加行的行高样式设置る 25. DataGridView新追加行单元格默认值设置 DataGridView中输入错误数据的处理(五) 26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获 DataGridView控件用法合集(六) 29. DataGridView行排序(点击列表头自动排序的设置) 30. DataGridView自动行排序(新追加值也会自动排序) 31. DataGridView自动行排序禁止情况下的排序 32. DataGridView指定列指定排序 DataGridView控件用法合集(七) 33. DataGridView单元格样式设置 34. DataGridView文字表示位置的设定 35. DataGridView单元格内文字列换行 36. DataGridView单元格DBNull值表示的设定 37. DataGridView单元格样式格式化 38. DataGridView指定单元格颜色设定 39. DataGridView单元格文字字体设置 40. DataGridView根据单元格值设定单元格样式 DataGridView控件用法合集(八) 41. DataGridView设置单元格背景颜色 42. DataGridView行样式描画 43. DataGridView显示行号 44. DataGridView焦点所在单元格焦点框不显示的设定 DataGridView控件用法合集(九) 45. DataGridView中显示选择框CheckBox 46. DataGridView中显示下拉框ComboBox 47. DataGridView单击打开下拉框 48. DataGridView中显示按钮 49. DataGridView中显示链接 50. DataGridView中显示图像 DataGridView控件用法合集(十) 51. DataGridView编辑中单元格控件取得 52. DataGridView输入自动完成 53. DataGridView单元格编辑时键盘KEY事件取得 54. DataGridView下拉框(ComboBox)单元格编辑时事件取得 55. DataGridView下拉框(ComboBox)单元格允许文字输入设定 DataGridView控件用法合集(十一) 56. DataGridView根据值不同在另一列中显示相应图片 57. DataGridView中显示进度条(ProgressBar) 58. DataGridView中添加MaskedTextBox DataGridView控件用法合集(十二) 59. DataGridView中Enter键按下焦点移至旁边的单元格 60. DataGridView行集合化(Group)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧