如何隐藏datagridview中没有数据那行前面的checkbox复选框?
就是说比如datagridview里面只显示了一行数据,为了美观期间我插入了一些空行。怎么让那些空行前面的checkbox复选框隐藏或者去掉?
上回我问过这个问题,以为解决了,后来发现里面提供的方法findcontrol是属于System.Web.UI.WebControls 命名空间里的checkboxlist类里的,貌似不能引用。
哪位打个还有什么办法啊?
就是说比如datagridview里面只显示了一行数据,为了美观期间我插入了一些空行。怎么让那些空行前面的checkbox复选框隐藏或者去掉?
上回我问过这个问题,以为解决了,后来发现里面提供的方法findcontrol是属于System.Web.UI.WebControls 命名空间里的checkboxlist类里的,貌似不能引用。
哪位打个还有什么办法啊?
可以参考如下两个绘制的方法:
CheckBoxRenderer.DrawCheckBox;
ControlPaint.DrawCheckBox
其实主要的问题是我不能获取某一行的checkbox..
我用的GridView就可以。
我试了 根本及不认识findcontrol
DataGridView.Rows[RowIndex ].Cells[ColumnIndex]就可以设置相应位置的控件的属性.
可以参考如下两个绘制的方法:
CheckBoxRenderer.DrawCheckBox;
ControlPaint.DrawCheckBox
==================
对`
就是写个方法`没什么麻烦的`
datagridview1.rows[i].cells[0]
不过好像不能设置是否显示
dataGridView1[0, dataGridView1.CurrentRow.Index].Value = true;0为CheckBox所在的IndexdataGridView1.CurrentRow.Index=現在所在的RowIndex
参数根据实际的需求进行修改
vlaue是用来指明是否被选中的,就是是不是打了勾的。隐藏的Visible说是只读的不能改变.
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 1)
{
e.Handled = true;
if (e.Value != null)
{ ControlPaint.DrawCheckBox(e.Graphics, e.CellBounds, ButtonState.Checked);
}
}
}
现在已经实现了。我把部分代码贴出来。可能有点麻烦。没好好做。效果不是很好看哦。兄弟自己试试,完善一下了。
新建的类
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;namespace WindowsControlLibrary1
{
public class MyDataViewCheckBoxCell:DataGridViewCheckBoxCell
{
private bool showCell = true;
protected override void Paint( Graphics graphics,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates elementState,
Object value,
Object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{ if (showCell)
{
//if (showCell)
//{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
//}
else
{ }
} public bool ShowCheckBox
{
get
{
return showCell;
}
set
{
showCell = value;
}
} }
}
在form的Load事件和DataGridView的CellPainting事件里作如下处理
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
DataGridView gdv = sender as DataGridView;
if (e.RowIndex > 0)
{
if (e.RowIndex == gdv.Rows.Count - 1)
{
MyDataViewCheckBoxCell cell = gdv.Rows[e.RowIndex].Cells[0] as MyDataViewCheckBoxCell;
cell.ShowCheckBox = false;
}
}
} private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Columns[0].CellTemplate = new MyDataViewCheckBoxCell();
for (int i = 0; i < 5; i++)
{
dataGridView1.Rows.Add(new DataGridViewRow());
}
}
这样就隐藏了最后一行的CheckBox