紧急求助,dataGridView问题,Winform C#

shui8iuhs 2010-01-07 12:02:30
表:had
字段:hid(int),aid(int),cid(varchar) 共三个字段

select * from had
查询结果:
hid aid cid
1 1 asdfsd
2 1 fdsfse
3 2 fsdfsd
4 5 0
...

我界面上的dataGridView绑定的事先写好的DataTable:select * from had
dataGridView1.DataSource=dt;

dataGridView1显示出来的结果是和上面的一样的。

我现在的问题是,如果cid==0,那对应的那一行就什么都不显示;如果cid!=0,那一行就显示出一张图片,然后图片要连接到另外一个页面。
请教各位大侠在dataGridView里面cid这列应该怎么编辑成可以显示图片的列哦???

...全文
162 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
xr13417555629 2010-01-08
  • 打赏
  • 举报
回复
up
shui8iuhs 2010-01-08
  • 打赏
  • 举报
回复
解决了,在dataGridView的添加列里添加了一列dataGridViewimagecolumn,然后再写的
void DGV_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView DGV = (DataGridView)sender;

if (DGV.Columns[e.ColumnIndex].Name == "image")
{
Rectangle R= DGV.GetCellDisplayRectangle(e.ColumnIndex ,e.RowIndex ,false);
e.Value = new Bitmap(R.Width, R.Height);
using (Graphics G = Graphics.FromImage((Bitmap)e.Value))
G.Clear(Color.Green);
}
}

就可以实现了,谢谢楼上的各位XDJM。
tianliang1 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wxm3630478 的回复:]
hid    aid        cid
1        1        asdfsd
2        1        fdsfse
3        2        fsdfsd
4        5          0

DataGridView 显示数据 是根据Column的类型来的

要么是 Text 要么是Image...  一列中怎么又能显示图片 又能显示文本呢
[/Quote]
这个问题好像应该先考虑清楚。。。
shui8iuhs 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sunhuaiwei 的回复:]
在dataGridView1的RowDataBound事件中加载,判断cid的是什么就可以了。

 protected void dataGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
  if (e.Row.RowType == DataControlRowType.DataRow)
{

  LinkButton myLb = (LinkButton)(e.Row.Cells[8].Controls[1]);

  myLb.Attributes.Add( "onclick ", "javascript:return confirm( '你确认删除 "+e.Row.Cells[0].Text+ "吗? ') ");

  //鼠标经过时改变行的颜色

  e.Row.Attributes.Add( "onmouseover ", "this.style.backgroundColor= '#ffffe7 ' ");

  e.Row.Attributes.Add( "onmouseout ", "this.style.backgroundColor= 'transparent ' ");

  }

  }


[/Quote]


晕了,我这上面这么没有RowDataBound方法呢????我的项目是Winform, C#,不是asp.net,



各位大侠们怎么弄哦?
wxm3630478 2010-01-07
  • 打赏
  • 举报
回复
hid aid cid
1 1 asdfsd
2 1 fdsfse
3 2 fsdfsd
4 5 0

DataGridView 显示数据 是根据Column的类型来的

要么是 Text 要么是Image... 一列中怎么又能显示图片 又能显示文本呢
sunhuaiwei 2010-01-07
  • 打赏
  • 举报
回复
模仿那个应该对了,不小心提交了。呵呵
晚上搂书睡 2010-01-07
  • 打赏
  • 举报
回复
在得到 datatable后就判断 cid 是否等于0 然后在给cid这个列赋值!
sunhuaiwei 2010-01-07
  • 打赏
  • 举报
回复
在dataGridView1的RowDataBound事件中加载,判断cid的是什么就可以了。

 protected void dataGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
  if (e.Row.RowType == DataControlRowType.DataRow)
{

  LinkButton myLb = (LinkButton)(e.Row.Cells[8].Controls[1]);

  myLb.Attributes.Add( "onclick ", "javascript:return confirm( '你确认删除 "+e.Row.Cells[0].Text+ "吗? ') ");

  //鼠标经过时改变行的颜色

  e.Row.Attributes.Add( "onmouseover ", "this.style.backgroundColor= '#ffffe7 ' ");

  e.Row.Attributes.Add( "onmouseout ", "this.style.backgroundColor= 'transparent ' ");

  }

  }

hejialin666 2010-01-07
  • 打赏
  • 举报
回复
如果你想改变某一个单元格的类型可以如下方法:

在编程时遇到问题:需要改变dataGridView中某一个单元格的类型,使其不受列类型的制约(比如说单元格所在的列定义的是DataGridViewTextBoxColumn,可是想把这一列中某几个单元格换成checkbox),这网上找了很长时间都没有结果,甚至有人认为这个单元格上的空间是画上去的,要改变就要重新把空间画出来!


解决办法:经过公司同事的共同研究,发现这个cell类型还是可以很简单的变化的。方法如下:


dataGridView1.Rows[0].Cells[1] = new DataGridViewCheckBoxCell();
hejialin666 2010-01-07
  • 打赏
  • 举报
回复
“添加列” 在dataGridView右上角的箭头里
hejialin666 2010-01-07
  • 打赏
  • 举报
回复
在添加列里添加个DataGridViewImageColumn
lovexilove 2010-01-07
  • 打赏
  • 举报
回复
做个判断嘛 是就显示图片
longhair9711 2010-01-07
  • 打赏
  • 举报
回复
你可以设置两列 把cid的那列隐藏掉 然后加一个image列 根据cid列中的数据来显示image中的图片
wartim 2010-01-07
  • 打赏
  • 举报
回复
是这段

void DGV_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
DataGridView DGV = (DataGridView)sender;

foreach (DataGridViewRow DGVR in DGV.Rows)
{
Object Value = DGVR.Cells["cid"].Value;
if (Value != null && Value.ToString() == "0")
DGVR.Visible = false;
}
}
wartim 2010-01-07
  • 打赏
  • 举报
回复
纠正下

void DGV_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView DGV = (DataGridView)sender;

if (DGV.Columns[e.ColumnIndex].Name == "image")
{
Rectangle R= DGV.GetCellDisplayRectangle(e.ColumnIndex ,e.RowIndex ,false);
e.Value = new Bitmap(R.Width, R.Height);
using (Graphics G = Graphics.FromImage((Bitmap)e.Value))
G.Clear(Color.Green);
}
}
wartim 2010-01-07
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication279
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();

DataTable DT=new DataTable();
DT.Columns .Add ("hid");
DT.Columns .Add ("aid");
DT.Columns .Add ("cid");

DT.Rows.Add (new Object[]{ 1 ,1, "asdfsd" });
DT.Rows.Add (new Object[]{ 2 ,1, "fdsfse" });
DT.Rows.Add (new Object[]{ 3 ,2, "fsdfsd" });
DT.Rows.Add (new Object[]{ 4 ,5, "0" });

DataGridView DGV = new DataGridView();
DGV.Parent = this;
DGV.Dock = DockStyle.Fill;
DGV.AllowUserToAddRows = false;

DGV.Columns.Add("hid", "hid");
DGV.Columns[DGV.ColumnCount - 1].DataPropertyName = "hid";
DGV.Columns.Add("aid", "aid");
DGV.Columns[DGV.ColumnCount - 1].DataPropertyName = "aid";
DGV.Columns.Add("cid", "cid");
DGV.Columns[DGV.ColumnCount - 1].DataPropertyName = "cid";
DGV.Columns[DGV.ColumnCount - 1].Visible = false;
DGV.Columns.Add(new DataGridViewImageColumn());
DGV.Columns[DGV.ColumnCount - 1].Name = "image";

DGV.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(DGV_DataBindingComplete);
DGV.CellFormatting += new DataGridViewCellFormattingEventHandler(DGV_CellFormatting);

DGV.DataSource = DT;
}

void DGV_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView DGV = (DataGridView)sender;

if (DGV.Columns[e.ColumnIndex].Name == "image")
{
Rectangle R= DGV.GetCellDisplayRectangle(e.ColumnIndex ,e.RowIndex ,false);
e.Value = new Bitmap(R.Width, R.Height);
using (Graphics G = Graphics.FromImage((Bitmap)e.Value))
G.Clear(Color.Green);
}
}

void DGV_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
DataGridView DGV = (DataGridView)sender;

Object Value = DGV["cid", DGV.RowCount - 1].Value;
if (Value != null && Value.ToString() == "0")
DGV.Rows[DGV.RowCount - 1].Visible = false;
}
}
}
shui8iuhs 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wxm3630478 的回复:]
hid    aid        cid
1        1        asdfsd
2        1        fdsfse
3        2        fsdfsd
4        5          0

DataGridView 显示数据 是根据Column的类型来的

要么是 Text 要么是Image...  一列中怎么又能显示图片 又能显示文本呢
[/Quote]

我就是不知道了,迷茫老,晕老~

110,533

社区成员

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

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

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