C# 将数据写入DataGridView 中并显示
2013-02-25 12:26
302 查看
一、DataGridView数据使用几大步骤(转载自http://zhidao.baidu.com/question/421640280.html)
(有一些小错误,已改正)
1、设置控件的外观;
DataGridView dgv=new DataGridView();
dgv.BackguoundColor=Color.FormArgb(255,255,255);
........
2、设置列标题,行标题;
dgv.EnableHeadeersVisualStyles=false;
dgv.ColumnHeadersDefaultCellStyle.Font=new Font("微软雅黑",10F);
........
3、设置列的格式,添加列;
DataGridViewCellStyle style=new DataGridViewCellStyle();
style.Alignment=DataGridViewContentAlignment.MiddleCenter;
........
4、取得数据,根据需要在控件中添加对应的行;
if(lsInfo!=null&&lsInfo.Count>0) //lsInfo就是List<数据>
{
dgv.Rows.Add(lsInfo.Count);
}
5、填充数据到控件中;
for(int i=0;i<lsInfo.Count;i++)
{
dgv.
Rows[i].Cells[0].Value=lsInfo[i].XXX; //填充第一列的数据
dgv.
Rows[i].Cells[1].Value=lsInfo[i].XXX; //填充第二列的数据
…………
}
二、注意的地方(原创)
在填充行数据前必须记得用手工设计器或者用程序先定义列,否则会出现“不能向没有列的 DataGridView 控件添加行。必须首先添加列。”的错误
用程序添加列的方法是:
DataGridView dgv = new DataGridView(); dgv.Columns.Add(A, B);
A:是程序中引用列时使用的名字;B:打算显示在UI上的列名。
(有一些小错误,已改正)
1、设置控件的外观;
DataGridView dgv=new DataGridView();
dgv.BackguoundColor=Color.FormArgb(255,255,255);
........
2、设置列标题,行标题;
dgv.EnableHeadeersVisualStyles=false;
dgv.ColumnHeadersDefaultCellStyle.Font=new Font("微软雅黑",10F);
........
3、设置列的格式,添加列;
DataGridViewCellStyle style=new DataGridViewCellStyle();
style.Alignment=DataGridViewContentAlignment.MiddleCenter;
........
4、取得数据,根据需要在控件中添加对应的行;
if(lsInfo!=null&&lsInfo.Count>0) //lsInfo就是List<数据>
{
dgv.Rows.Add(lsInfo.Count);
}
5、填充数据到控件中;
for(int i=0;i<lsInfo.Count;i++)
{
dgv.
Rows[i].Cells[0].Value=lsInfo[i].XXX; //填充第一列的数据
dgv.
Rows[i].Cells[1].Value=lsInfo[i].XXX; //填充第二列的数据
…………
}
二、注意的地方(原创)
在填充行数据前必须记得用手工设计器或者用程序先定义列,否则会出现“不能向没有列的 DataGridView 控件添加行。必须首先添加列。”的错误
用程序添加列的方法是:
DataGridView dgv = new DataGridView(); dgv.Columns.Add(A, B);
A:是程序中引用列时使用的名字;B:打算显示在UI上的列名。
相关文章推荐
- C#如何使用dataGridView控件显示类对象数据
- C#中datagridview显示数据库数据
- C#中使用DataGridView按数字大小排序显示access中特定数据
- C# Winform datagridview中的数据不能显示,单击后才能显示出来
- 使用C#语言,从Excel2007中读取数据,并显示到Form中的DataGridView。
- C#中读取sql server的数据,并在datagridview中显示
- C#学习笔记之在DataGridView上显示数据
- C#向RichTextBox写入数据并始终显示在最下方
- [C#] 检索数据库并用DataGridView 控件显示数据.
- C#datagridview读取数据库数据并显示
- C#中把当前显示的datagridview中的数据以excel的形式导出
- c# 在datagridview中添加comboboxcolumn 绑定数据库读取显示数据
- C#连接access数据库并将数据读取到dataGridView中显示
- C# 将数据写入文件并换行显示
- C# 面试 (datagrid || datagridView显示数据)
- c#dataGridView分页显示数据
- C#中读取sql server的数据,并在datagridview中显示
- C#:在DataGridView控件中显示数据
- [C#] 检索数据库并用DataGridView 控件显示数据.
- c#将dataGridView中显示的数据导出到excel中