在WinForm中,双击DataGridView列头设置当前列的显示为指定的图片

jiarongliu 2012-05-05 01:55:50
在WinForm中,
双击DataGridView列头设置当前列的显示为指定的图片,
再双击时列头显示回之前的文字
...全文
156 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiarongliu 2012-05-06
  • 打赏
  • 举报
回复
不是整列显示指定图片,而是列标头显示就可以了.
比如是DataGridVeiwTextBoxColumn.双击它的列标头可以在列标头上显示指定的图片,再双击那图片去可以去掉
cnfixit 2012-05-05
  • 打赏
  • 举报
回复
呃,上面没有考虑当前列问题,不过那应该不是什么大问题
cnfixit 2012-05-05
  • 打赏
  • 举报
回复

public partial class Form1 : Form
{
bool flag = true;
bool show = true;
int index = 0;

public Form1()
{
InitializeComponent();
this.dataGridView1.Rows.Add("1", "1", "1", "1");
this.dataGridView1.Rows.Add("1", "1", "1", "1");
this.dataGridView1.Rows.Add("1", "1", "1", "1");
this.dataGridView1.Rows.Add("1", "1", "1", "1");
this.dataGridView1.Rows.Add("1", "1", "1", "1");
}

private void dataGridView1_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (flag)
{
this.dataGridView1.Columns[e.ColumnIndex].Visible = false;
DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
iconColumn.Image = GetImage(Application.StartupPath + @"\g.png");
iconColumn.Name = "new";
iconColumn.HeaderText = "new";
dataGridView1.Columns.Insert(e.ColumnIndex, iconColumn);
index = e.ColumnIndex;
flag = false;
}
else
{
this.dataGridView1.Columns[index].Visible = !show;
this.dataGridView1.Columns[index + 1].Visible = show;
show = !show;
}

}



private Image GetImage(string path)
{
FileStream fs = new FileStream(path, System.IO.FileMode.Open);
Image result = Image.FromStream(fs);
fs.Close();
return result;
}

}

好久木有写过代码了啊,将就将就看吧……

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧