学习DataGridViewX
2015-11-01 13:50
656 查看
列头设置: Columns 集合, name, ColumnType,DataPropertyName. Shape可以选(ButtonXColum)
行字体样式:DefaultCellStyle
行高,复制设置: RowTemplate
DataGridViewButtonXColumn -- 带有按钮的列,可以在form初始化时候设置按钮的样式:
1.获取按钮
DataGridViewButtonXColumn bcx = dataGridViewX1.Columns["Country"] as DataGridViewButtonXColumn;
2. 设置自定义按钮文本
bcx.UseColumnTextForButtonValue = false;
3. 设置自定义按钮图片(paintCell事件前, 重绘也是逐行绘制,所以bcx.Text得到是该列某一个cell的值)
bcx.BeforeCellPaint +=X1Country_BeforeCellPaint;
void X1Country_BeforeCellPaint(object sender, BeforeCellPaintEventArgs e)
{
DataGridViewButtonXColumn bcx = sender as DataGridViewButtonXColumn;
// Set the button flag image to correspond to country
if (bcx != null)
bcx.Image = imageList1.Images[bcx.Text];
}
DataGridView点击事件: CellContentClick
dataGridViewX1.CellContentClick += DataGridViewX1_CellContentClick;
void DataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// 把列转换为DataGridViewButtonXCell, 如果列类型本身不是DataGridViewXColumn,则cell值为null
DataGridViewButtonXCell cell = dataGridViewX1.CurrentCell as DataGridViewButtonXCell;
if (cell != null)
{
// 根据Cell得到列对象ButtonXColumn
DataGridViewButtonXColumn bc =
dataGridViewX1.Columns[e.ColumnIndex] as DataGridViewButtonXColumn;
if (bc != null)
{
string s = Convert.ToString(cell.Value);
//得到列名字
switch (bc.Name)
{
case "Country":
MessageBox.Show("What a great country " + s + " is!", "",
MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "Region":
cell.Value = string.IsNullOrEmpty(s) ? "Global" : "";
break;
}
}
}
}
// Update the entire column so that each column cell
// will be reformatted according to the new list values.
重绘列表的方法
dataGridViewX1.InvalidateColumn(cell.ColumnIndex);
行字体样式:DefaultCellStyle
行高,复制设置: RowTemplate
DataGridViewButtonXColumn -- 带有按钮的列,可以在form初始化时候设置按钮的样式:
1.获取按钮
DataGridViewButtonXColumn bcx = dataGridViewX1.Columns["Country"] as DataGridViewButtonXColumn;
2. 设置自定义按钮文本
bcx.UseColumnTextForButtonValue = false;
3. 设置自定义按钮图片(paintCell事件前, 重绘也是逐行绘制,所以bcx.Text得到是该列某一个cell的值)
bcx.BeforeCellPaint +=X1Country_BeforeCellPaint;
void X1Country_BeforeCellPaint(object sender, BeforeCellPaintEventArgs e)
{
DataGridViewButtonXColumn bcx = sender as DataGridViewButtonXColumn;
// Set the button flag image to correspond to country
if (bcx != null)
bcx.Image = imageList1.Images[bcx.Text];
}
DataGridView点击事件: CellContentClick
dataGridViewX1.CellContentClick += DataGridViewX1_CellContentClick;
void DataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// 把列转换为DataGridViewButtonXCell, 如果列类型本身不是DataGridViewXColumn,则cell值为null
DataGridViewButtonXCell cell = dataGridViewX1.CurrentCell as DataGridViewButtonXCell;
if (cell != null)
{
// 根据Cell得到列对象ButtonXColumn
DataGridViewButtonXColumn bc =
dataGridViewX1.Columns[e.ColumnIndex] as DataGridViewButtonXColumn;
if (bc != null)
{
string s = Convert.ToString(cell.Value);
//得到列名字
switch (bc.Name)
{
case "Country":
MessageBox.Show("What a great country " + s + " is!", "",
MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "Region":
cell.Value = string.IsNullOrEmpty(s) ? "Global" : "";
break;
}
}
}
}
// Update the entire column so that each column cell
// will be reformatted according to the new list values.
重绘列表的方法
dataGridViewX1.InvalidateColumn(cell.ColumnIndex);
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程
- C#实现基于链表的内存记事本实例
- C#托管堆对象实例包含内容分析