日期:2014-05-20  浏览次数:20854 次

datagrid点击显示整行被选中
我在datagrid控件里显示一个表的信息,我想实现电击其中一个字段,则该字段对应的整行都显示被选中颜色,这个用代码怎么实现。

------解决方案--------------------
protected void dtgGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header)
{
//鼠标移动到每项时颜色交替效果
e.Item.Attributes.Add( "OnMouseOut ", "this.style.backgroundColor= 'White ';this.style.color= '#003399 ' ");
e.Item.Attributes.Add( "OnMouseOver ", "this.style.backgroundColor= '#F3F3F3 ';this.style.color= '#8C4510 ' ");
}
}


我这个是鼠标移动时候的颜色,同理我想你可以用来做选定的颜色。
------解决方案--------------------
internal class DataGridNoActiveCellColumn:DataGridColoredTextBoxColumn//System.Windows.Forms.DataGridTextBoxColumn
{
//重载DataGridTextBoxColumn类的Edit 方法,以便点击DataGrid中的任一单元格都选中当前行
protected override void Edit(CurrencyManager source,int rowNum,Rectangle bounds,bool readOnly,
string instantText,bool cellIsVisible)
{
int SelectedRow=rowNum;
this.DataGridTableStyle.DataGrid.Select(SelectedRow);
}
}