sorry,而不能用!
也就是说不让用户选中多行纪录!
也就是说不让用户选中多行纪录!
解决方案 »
- C# WinForm中DataGridView自动匹配拼音简码问题
- datatable创建行,并为行赋值(列已固定)
- 一个奇怪的问题
- 这样的功能用C#应该怎么写?
- 100分求助:水晶报表如何打印类似超市小票,每次长度不固定?
- 关于C# 中combobox selectedvalue 的问题
- 请问谁有winForm下的Treeview的可以新增节点,修改,删除的源代码参考一下?
- --如何用控件上的按钮控制应用窗口关闭--
- 大家还记得 三楼楼主 那套OA系统吗?有谁明白那个日历控件的用法,我怎么按原样粘过去都不行,大家试试看~~~
- 为什么我的版本没有变
- 两个线程调用一个类中的读库怎么写,才能不会出现datareader已打开的问题
- ???checkedListBox怎么删除列表中的某一项???
是因为我在mouseup中作了处理,使当前行变蓝
测试部门看到多条蓝数据,认为选中了多行?
public class MyDataGrid : System.Windows.Forms.DataGrid
2. 定义一个属性AllowMultiSelect, (假如你需要控制是否多选)
private bool allowMultiSelect = false;
public bool AllowMultiSelect
{
get
{
return this.allowMultiSelect;
}
set
{
this.allowMultiSelect = value;
}
}
3. 定义一个变量储存原来选中的行号
private int oldSelectedRow = -1;
4. 重载OnMouseDown
protected override void OnMouseDown(MouseEventArgs e)
{
// Record the time mouse is clicked
dataGridMouseDownTime = DateTime.Now; //don't call the base class if in header
DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X, e.Y)); if(hti.Type == DataGrid.HitTestType.Cell)
{
if( !allowMultiSelect && oldSelectedRow > -1)
{
this.UnSelect(oldSelectedRow);
oldSelectedRow = hti.Row;
} base.OnMouseDown(e);
}
else if(hti.Type == DataGrid.HitTestType.RowHeader)
{
if( allowMultiSelect )
{
base.OnMouseDown(e);
}
else
{
if(oldSelectedRow > -1 && oldSelectedRow < this.BindingContext[this.DataSource, this.DataMember].Count) this.UnSelect(oldSelectedRow);
if((Control.ModifierKeys & Keys.Shift) == 0)
base.OnMouseDown(e);
else
this.CurrentCell = new DataGridCell(hti.Row, hti.Column);
this.Select(hti.Row);
oldSelectedRow = hti.Row;
}
}
else
{
base.OnMouseDown(e);
}
}
5. 重载OnMouseMove
protected override void OnMouseMove(MouseEventArgs e)
{
//don't call the base class if left mouse down
if( allowMultiSelect
|| (!allowMultiSelect && e.Button != MouseButtons.Left) )
base.OnMouseMove(e);
}
6. 重载OnCurrentCellChanged
protected override void OnCurrentCellChanged(EventArgs e)
{
if( this.RowCount > 0 && !allowMultiSelect )
{
for(int i = 0; i < this.RowCount; i++)
{
if( this.IsSelected(i) )
this.UnSelect(i);
}
} int rowIndex = this.CurrentRowIndex;
if( rowIndex == this.oldCellIndex )
return; this.oldCellIndex = rowIndex;
base.OnCurrentCellChanged(e);
}
修改如下:
6. 重载OnCurrentCellChanged
protected override void OnCurrentCellChanged(EventArgs e)
{
if( this.RowCount > 0 && !allowMultiSelect )
{
for(int i = 0; i < this.RowCount; i++)
{
if( this.IsSelected(i) )
this.UnSelect(i);
}
} base.OnCurrentCellChanged(e);
}
4. 去掉一下代码:
// Record the time mouse is clicked
dataGridMouseDownTime = DateTime.Now;
另外上面用到了this.RowCount, 可以参考如下:
public int RowCount
{
get
{
int count = 0;
if( this.DataSource != null )
{
Type type = this.DataSource.GetType();
if( type == typeof(System.Data.DataSet)
|| type.BaseType == typeof(System.Data.DataSet) )
{
count = this.BindingContext[this.DataSource, this.DataMember].Count;
}
else if( type == typeof(System.Data.DataView) )
{
count = ((System.Data.DataView)this.DataSource).Count;
}
else if( type == typeof(System.Data.DataTable) )
{
count = ((System.Data.DataTable)this.DataSource).Rows.Count;
}
} return count;
}
}
还没有使你的代码,但无论如何,非常感谢
其实我是想改变DataGrid的每一行的颜色,颜色很多种哦
datagrid.select(i);好像不行吧哪位能见教