110,533
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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);
}
}
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;
}
}
}