winform datagridview表头加图标
2017-02-15 本文已影响0人
球球1104
要把dataGridView1_RowPostPaint这个方法激活,在form.desiger.cs中加入事件
** this.SizeChanged += new System.EventHandler(this.Form_SizeChanged);**
private void dataGridView1_RowPostPaint(object sender,DataGridViewRowPostPaintEventArgs e)
{
if (this.dataGridView1.Rows[e.RowIndex].Cells["Check狀態"].Value == DBNull.Value)
//DBNull是一個類, DBNull.Value表示一個對象在數據庫中的值爲空,或者說未初始化
//DBNull.Value對象是指向有效的對象, DBNull可以表示數據庫中的字符串,數字或者日期,對於DataRow,它的row[column]返回的值永遠不爲null,要麼是具體的爲column的類型的值,要麼就是DBNull
return;
string intcheck = Convert.ToString(this.dataGridView1.Rows[e.RowIndex].Cells["Check狀態"].Value);
Image RowIcon;//標頭圖示
//string strToolTip="";
if(intcheck=="Pass")
{
RowIcon = global::LayoutSZQCAndGAD.Properties.Resources.accept;
// strToolTip = "OK!";
}
else if (intcheck == "Fail")
{
RowIcon = global::LayoutSZQCAndGAD.Properties.Resources.cross;
// strToolTip = "需要注意check!";
}
else if (intcheck == "Lim")
{
RowIcon = global::LayoutSZQCAndGAD.Properties.Resources.lightbulb;
//strToolTip = "需要注意check!";
}
else
{
RowIcon = global::LayoutSZQCAndGAD.Properties.Resources._027;
}
//7是因爲默認圖標爲16x16,位置應該爲8x8,但是前面有個小的默認的圖標佔用1,所以16/2-1=7
//8是因爲默認圖標爲16x16,位置應爲16/2=8
e.Graphics.DrawImage(RowIcon, e.RowBounds.Left + Convert.ToInt16(this.dataGridView1.RowHeadersWidth / 2) - 4, Convert.ToInt16((e.RowBounds.Top + e.RowBounds.Bottom) / 2 - 8), 16, 16);
//this.dataGridView1.Rows[e.RowIndex].HeaderCell.ToolTipText = strToolTip;//設置提示資訊
}
效果:
