DataGridView控件-学习笔记总结
1、GridColor属性用来获取或设置网格线的颜色
dataGridView1.GridColor=Color.Blue;
2、设置宽度 、高度
dataGridView1.Columns[].Width=;
dataGridView1.Rows[].Height = ;
3、设置字体
dataGridView1.DefaultCellStyle.Font = new Font("隶书",);
dataGridView1.Columns[].DefaultCellStyle.Font = new Font("宋体", );
dataGridView1.Rows[].DefaultCellStyle.Font = new Font("行楷", );
4、设置货币在DataGridView控件中的显示
dataGridView1.Columns[].DefaultCellStyle.Format = "c";
5、设置DataGridView单元格的文本对齐方式
dataGridView1.Columns[].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
6、DataGridView控件可编辑的时候验证数据输入
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == )
{
int result = ;
string before = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
if(!int.TryParse(e.FormattedValue.ToString(),out result))
{
dataGridView1.Rows[e.RowIndex].ErrorText = "年龄必须为数值类型";
e.Cancel = true;
}
}
}
7、获取DataGridView控件中鼠标单击表格任意单元格的文本
//单击单元格
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
} //单击单元格内容
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
8、在单元格中换行
dataGridView1.DefaultCellStyle.WrapMode=DataGridViewTriState.True;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace DataGridView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btn_GridColor_Click(object sender, EventArgs e)
{
dgv_ShowGridColor.DataSource = new List<Student>()
{
new Student("张三",,),
new Student("李四",,),
new Student("王五",,)
};
//设置表头文本
dgv_ShowGridColor.Columns[].HeaderText = "姓名";
dgv_ShowGridColor.Columns[].HeaderText = "年龄";
//设置列宽度
dgv_ShowGridColor.Columns[].Width = ;
dgv_ShowGridColor.Columns[].Width = ;
//设置网格线颜色
dgv_ShowGridColor.GridColor = Color.Blue;
//设置全局字体
dgv_ShowGridColor.Font = new Font("隶书", );
//设置数据字体
dgv_ShowGridColor.DefaultCellStyle.Font = new Font("楷体", );
//设置数字数据显示
dgv_ShowGridColor.Columns[].DefaultCellStyle.Format = "c";
//设置数据填充样式 dgv的宽度一定要够长
dgv_ShowGridColor.Columns[].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
//设置对齐样式
dgv_ShowGridColor.Columns[].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//全局对齐样式
//dgv_ShowGridColor.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//换行显示过长文本
dgv_ShowGridColor.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
//设置行高
//dgv_ShowGridColor.Rows[2].Height = 100;
////添加列
//dgv_ShowGridColor.Columns.Add("day", "日期");
//dgv_ShowGridColor.Columns.Remove("day");
//禁止添加行和列
//dgv_ShowGridColor.AllowUserToAddRows = false;
//dgv_ShowGridColor.AllowUserToDeleteRows = false;
//自动排序
//for (int i = 0; i < dgv_ShowGridColor.Columns.Count; i++)
//{
// dgv_ShowGridColor.Columns[i].SortMode = DataGridViewColumnSortMode.Automatic;
//}
//隔行换色
//for (int i = 0; i < dgv_ShowGridColor.Rows.Count; i++)
//{
// if (i % 2 == 0)
// {
// dgv_ShowGridColor.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
// }
//} } private void Form1_Load(object sender, EventArgs e)
{
//SqlConnection myConn = new SqlConnection("server=(local);database=testDB;integrated security=true");
//SqlDataAdapter mysda = new SqlDataAdapter("select * from student", myConn);
//DataSet ds = new DataSet();
//mysda.Fill(ds);
//dgv_ShowGridColor.DataSource = ds.Tables[0];
////dgvData.RowHeadersVisible = false;
////btnAdd.Enabled = false;
//dgv_ShowGridColor.Columns[0].ReadOnly = true;
} private void button1_Click(object sender, EventArgs e)
{
dgv_ShowGridColor.DataSource = new List<Images>()
{
new Images(){_Im=Image.FromFile(System.Environment.CurrentDirectory+"\\Image\\1.bmp")},
new Images(){_Im=Image.FromFile(System.Environment.CurrentDirectory+"\\Image\\2.bmp")},
new Images(){_Im=Image.FromFile(System.Environment.CurrentDirectory+"\\Image\\3.bmp")},
new Images(){_Im=Image.FromFile(System.Environment.CurrentDirectory+"\\Image\\4.bmp")},
new Images(){_Im=Image.FromFile(System.Environment.CurrentDirectory+"\\Image\\5.bmp")},
new Images(){_Im=Image.FromFile(System.Environment.CurrentDirectory+"\\Image\\6.bmp")},
new Images(){_Im=Image.FromFile(System.Environment.CurrentDirectory+"\\Image\\7.bmp")}
};
dgv_ShowGridColor.Columns[].HeaderText = "图片";
dgv_ShowGridColor.Columns[].Width = ;
for (int i = ; i < dgv_ShowGridColor.Rows.Count; i++)
{
dgv_ShowGridColor.Rows[i].Height = ;
}
}
}
class Student
{
private string name;
private int age;
private int money;
public string _Name
{
get;
set;
}
public int _Age
{
get;
set;
}
public int _Money
{
get;
set;
}
public Student(string name, int age,int money)
{
this._Name = name;
this._Age = age;
this._Money = money;
}
}
class Images
{
private Image im;
public Image _Im
{
get;
set;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace DataGridView中添加合计和平均值
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Fruit> fruit;
private void button1_Click(object sender, EventArgs e)
{
fruit = new List<Fruit>()
{
new Fruit(){Name="ada",Price=},
new Fruit(){Name="adfa",Price=},
new Fruit(){Name="fddd",Price=}
};
dataGridView1.Columns.Add("Fruit", "水果");
dataGridView1.Columns.Add("Price", "价格");
//dataGridView1.DataSource = fruit;
foreach (Fruit f in fruit)
{
dataGridView1.Rows.Add(new string[]
{
f.Name,
f.Price.ToString()
});
}
float sum = ;
fruit.ForEach(
(pp) =>
{
sum += pp.Price;
}
);
dataGridView1.Rows.Add(new string[]{
"合计:"+sum.ToString(),"均价:"+(sum/fruit.Count).ToString()
});
}
}
class Fruit
{
private string name;
private int price;
public string Name
{
get;
set;
}
public int Price
{
get;
set;
}
}
}
eg
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data; namespace _19事务01
{
class SqlHelper
{
public static bool ExecDataBySqls(List<string> strSqls, string strConn)
{
SqlConnection sqlConn = new SqlConnection(strConn);
bool boolIsSucceed = false;
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlConn;
if (sqlConn.State == ConnectionState.Closed)
{
sqlConn.Open();
}
SqlTransaction sqlTran = sqlConn.BeginTransaction();//启动一个事务
try
{
sqlCmd.Transaction = sqlTran;//为事务创建一个命令
foreach (string item in strSqls)
{
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = item;
sqlCmd.ExecuteNonQuery();
}
sqlTran.Commit();//提交事务
boolIsSucceed = true;
}
catch
{
sqlTran.Rollback();//回滚事务,恢复数据
boolIsSucceed = false;
}
finally
{
sqlConn.Close();
strSqls.Clear();
}
return boolIsSucceed;
}
public static DataSet DataBanding(string connStr,string selectCmdText)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlDataAdapter sda = new SqlDataAdapter(selectCmdText, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "Student");
return ds;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; namespace _19事务01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string connStr = "Server=(local);DataBase=testDB;Integrated Security=true";
string selectCmdText = "select * from Student";
private void Form1_Load(object sender, EventArgs e)
{ dataGridView1.DataSource = SqlHelper.DataBanding(connStr, selectCmdText).Tables[];
} private void button1_Click(object sender, EventArgs e)
{
List<string> strSqls = new List<string>();
string strDelete =string.Format("delete from student where stuNumber='{0}'",txtDStuNumber.Text);
string strInsert = string.Format("insert into student values('{0}','{1}',{2})", txtStuNumber.Text, txtStuName.Text, txtStuAge.Text);
string strSelect = "select * from student where stuNumber='2010181055'";
strSqls.Add(strDelete);
strSqls.Add(strInsert);
strSqls.Add(strSelect);
if (SqlHelper.ExecDataBySqls(strSqls, connStr))
{
MessageBox.Show("事务执行成功");
}
//dataGridView1.DataSource = SqlHelper.DataBanding(connStr, selectCmdText).Tables[0];
Form1_Load(sender,e);
MessageBox.Show("加载成功");
}
}
}
using(SqlConnection myConn=new SqlConnection(connectionText))
{
if(myConn.State==ConnectionState.Closed)
{
myConn.Open();
}
//将数据显示到DataGridView中
string selectCommandText = "select * from student";
SqlDataAdapter mySDA = new SqlDataAdapter(selectCommandText,myConn);
DataSet myDS = new DataSet();
mySDA.Fill(myDS,"student");
dgvShow.DataSource=myDS.Tables[0];
//DataTable myDT = new DataTable();
//for (int i = 0; i < dgvShow.Rows.Count;i++ )
//{
//}
}
DataGridView控件-学习笔记总结的更多相关文章
- 转)delphi chrome cef3 控件学习笔记 (二)
(转)delphi chrome cef3 控件学习笔记 (二) https://blog.csdn.net/risesoft2012/article/details/51260832 原创 2016 ...
- Corelocation及地图控件学习笔记
Corelocation基本使用 在地图章节的学习中,首先要学的便是用户位置定位,因此我们首先要掌握Corelocation的使用.(在IOS8以前可以系统会直接请求授权,现在需要我们自己调用方式通知 ...
- Winform控件学习笔记【第二天】——常用控件
背景:期末考试刚过就感冒了,嗓子火辣辣的,好难受.但是一想起要学习总结就打起精神来了,Winform控件网上也没有多少使用教程,大部分都是自己在网上零零散散的学的,大部分用的熟了,不总结会很容易忘得. ...
- WinForm控件学习笔记【第一天】——Control类
感悟:明天就又是学校双选会的日子了.两年我都参与了学校的双选会的服务工作,现在该是双选会服务的我时候了.怎么样找到一份好的工作,或者说怎么样学习才能符合企业对人才的要求,我现在也是很迷茫.平时都是在看 ...
- dev控件学习笔记之----CxGrid
本人总结的DEV学习:希望对大家有所帮助. 一.是否显示分组工具: 二.表格左边记录信息显示的宽度: 三.设置表格行高: 四.表头文件的水平和垂直设置:多个设置用按住SHIFT后进行多选,然后就可以设 ...
- web前端开发控件学习笔记之jqgrid+ztree+echarts
版权声明:本文为博主原创文章,转载请注明出处. 作为web前端初学者,今天要记录的是三个控件的使用心得,分别是表格控件jqgrid,树形控件ztree,图表控件echarts.下边分别进行描述. ...
- C# WinForm调用UnityWebPlayer Control控件 <学习笔记1>
工具 1.三维场景 Unity 5.0.2f1 2.开发环境Microsoft Visual Studio 2010 3.需要使用的控件 UnityWebPlayer Control 出现的问题及解决 ...
- Winform控件学习笔记【第六天】——TreeView
TreeView控件用来显示信息的分级视图,如同Windows里的资源管理器的目录.TreeView控件中的各项信息都有一个与之相关的Node对象.TreeView显示Node对象的分层目录结构,每个 ...
- Winform控件学习笔记【第五天】——ListView
[第五天] 常用的基本属性: FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. GridLines:设置行和列之间是否显示网格线.( ...
随机推荐
- 用java 删除mongodb的数据
import java.net.UnknownHostException; import com.mongodb.BasicDBObject;import com.mongodb.DB;import ...
- C#中,为什么结构体也可以设置构造函数?
结构体派生自ValueType,ValueType派生自Object,可访问Object的方法.结构体是一种缩小版的类.结构体不能继承.结构体总是有一个无参数的默认构造函数,不允许替换.结构体可指定字 ...
- 历经曲折的freescale
从来没有想到自己在大学最信任的队友会突然放弃,,为了这个,,我可以说放弃了很多,,这几天可以我也不知道怎么办了,,我想不通的是为什么可以这样,为什么可以这么不负责任,,为什么这么自私,,这些我永远都不 ...
- ubuntu下安装Sublime Text并支持中文输入
Sublime Text还是文本编辑器中比较不错的,就是他的文件对比有些差劲吧,还有中文输入需要打补丁,不知道开发者是怎么想的... 当然,这个软件是收费的,但是不买也能一直的使用,在我天朝就这点好处 ...
- sublime text修改TAB缩进为空格
在sublime text中将TAB缩进直接转化为4个空格,可以按照如下方式操作: 菜单栏: Preferences -> Settings – More -> Syntax Specif ...
- 学习28个HTML5特征、窍门和技术
当下,H5火热得不行,写下这篇文章,认真的认识下HTML5. HTML5最早应该是09年左右被提出,然而当时受浏览器兼容性的影响,一直没得到普遍应用,最近也是因为移动端的发展,带动HTML5. 回归正 ...
- 实用工具推荐(Live Writer)(2015年05月26日)
1.写博客的实用工具 推荐软件:Live Writer 使用步骤: 1.安装 Live Essential 2011,下载地址:http://explore.live.com/windows-live ...
- uml与数据库设计
一.类之间的关系如下图所示: 二.UML与数据库设计主要讨论的内容: 三.依赖关系强调的是类操作间的使用关系,类图到表结构的映射中并不涉及这种关系,所以只需讨论泛化关系.关联关系到表的映身规范. 1. ...
- C语言(简单游戏)-走出迷宫
#include <stdio.h> //宏定义 maze[ROWS][COLS];行和列; #define ROWS 7 #define COLS 6 //绘制迷宫(全局变量) char ...
- Unity连接本地数据库sqlite
首先要创建一个sqlite的数据库,记住文件地址,拷贝到Assets目录下,创建的数据库文件后缀为.sqlite.具体创建方法百度sqlite 然后百度Mono.Data.Sqlite,这是一个dll ...