列头设置: 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;
}
}
}
行字体样式: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);