有关DATAGRID整行选择 选中一行:dataGrid1.Select(dataGrid1.CurrentRowIndex);你可以写在OnClick里面或者OnCurrentCellChanged事件里面 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 gridMain.SelectedIndex = index ; private void dgMain_Click(object sender, System.EventArgs e) { this.SetRow(); } private void dgMain_CurrentCellChanged(object sender, System.EventArgs e) { this.SetRow(); } private void SetRow() { index = this.dgMain.CurrentRowIndex; if( index > -1 ) { this.dgMain.Select( index ); } } 如果是winform可能要重写System.Windows.Forms.DataGridColumnStyle的Edit这个方法.web的没有做过. "选中一行:dataGrid1.Select(dataGrid1.CurrentRowIndex);你可以写在OnClick里面或者OnCurrentCellChanged事件里面"这样以后,还是可以选中单个单元格(背景变灰),如何真正选择整行,就像在listview里一样 private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { System.Drawing.Point pt = new Point(e.X, e.Y); DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt); if(hti.Type == DataGrid.HitTestType.Cell) { dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column); dataGrid1.Select(hti.Row); } } 问题初步解决,感谢飞鱼,不过DATAGRID第一次显示时默认为一行一列被选中变灰,如何解决,多谢各位相助!!!! 使用HitTestInfo 作楼主的工作简直就是用牛刀杀鸡的感觉 http://community.csdn.net/Expert/topic/3434/3434227.xml?temp=1.165408E-02 给你个参考: public class ViewColumnStyle : DataGridColumnStyle { // The isEditing field tracks whether or not the user is // editing data with the hosted control. private bool isEditing; private int nSelectedRowIndex; public ViewColumnStyle() : base() { nSelectedRowIndex = -1; } protected override void Abort(int rowNum) { isEditing = false; Invalidate(); } protected override bool Commit (CurrencyManager dataSource, int rowNum) { if (!isEditing) return true; isEditing = false; Invalidate(); return true; } protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) { if (!this.ReadOnly) { return; } if(nSelectedRowIndex> -1 && nSelectedRowIndex< source.List.Count + 1) { this.DataGridTableStyle.DataGrid.UnSelect(nSelectedRowIndex); } this.DataGridTableStyle.DataGrid.Select(rowNum); nSelectedRowIndex= rowNum; } protected override Size GetPreferredSize( Graphics g, object value) { return new Size(100, 4); } protected override int GetMinimumHeight() { return 4; } protected override int GetPreferredHeight(Graphics g, object value) { return 4; } protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum) { Paint(g, bounds, source, rowNum, false); } protected override void Paint( Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight) { Paint( g,bounds, source, rowNum, Brushes.Red, Brushes.Blue, alignToRight); } protected override void Paint( Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) { object objValue = GetColumnValueAtRow(source, rowNum); Rectangle rect = bounds; g.FillRectangle(backBrush,rect); rect.Offset(0, 2); rect.Height -= 2; g.DrawString(objValue.ToString(), this.DataGridTableStyle.DataGrid.Font, foreBrush, rect); } protected override void SetDataGridInColumn(DataGrid value) { base.SetDataGridInColumn(value); } } public class ListDataGrid:System.Windows.Forms.DataGrid { private Point m_CurPoint; private DataView m_ListDataView; private DataGridTableStyle tableStyle; /// <summary> /// 指定数据绑定并显示 /// </summary> /// <param name="dvw"></param> /// <param name="TemplateName"></param> public void SetListData(DataView dvw) { if (tableStyle==null) { tableStyle = new DataGridTableStyle(); tableStyle.MappingName =dvw.Table.TableName; int numCols = dvw.Table.Columns.Count; ViewColumnStyle textColumn; for (int i=0;i<dvw.Table.Columns.Count;i++) { textColumn = new ViewColumnStyle(); textColumn.ReadOnly=true; textColumn.NullText=""; textColumn.HeaderText = dvw.Table.Columns[i].Caption; textColumn.MappingName = dvw.Table.Columns[i].ColumnName; tableStyle.GridColumnStyles.Add(textColumn); } tableStyle.AllowSorting=false; tableStyle.RowHeadersVisible=false; this.TableStyles.Clear(); this.TableStyles.Add(tableStyle); } this.BeginInit(); this.SetDataBinding(dvw,null); this.EndInit(); dvw.AllowDelete=true; dvw.AllowEdit=true; m_ListDataView=dvw; } protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData) { if (this.DataSource==null) { return true; } switch (keyData) { case Keys.Space: if (OnSpace!=null) { OnSpace(this,this.CurrentRowIndex); } return true; case Keys.Return: if (OnReturn!=null) { OnReturn(this,this.CurrentRowIndex); } base.ProcessDialogKey(keyData); this.Select(this.CurrentRowIndex); return true; case Keys.Up: if (this.CurrentRowIndex==0) { this.Select(0); return true; } else { return false; } case Keys.Down: int nRowCount=0; if (this.ListDataView.AllowNew) { nRowCount=((DataView)this.DataSource).Table.Rows.Count; } else { nRowCount=((DataView)this.DataSource).Table.Rows.Count-1; } if (this.CurrentRowIndex==nRowCount && this.CurrentRowIndex!=-1) { this.Select(this.CurrentRowIndex); return true; } else { return false; } case Keys.Left: return true; case Keys.Right: return true; case Keys.Tab: base.ProcessDialogKey(keyData); return true; case Keys.Delete: return true; default: return false; } } } 传奇3的wix,wil和map文件如何用C#打开?100分求解 picturebox绘画是颜色设置 C语言的AES加密 如何改成C#语言的 不剩感激 QQ2009 C#如何登录 异常:没有为命令对象设置命令 我要用C# 开发一个小软件,用于存放网址之类的,请问该用什么数据库 通过sql语句 如何同时插入字符串信息和图片信息 特殊字符如何处理? Treeview的节点上下移动问题——非鼠标拖拽 API函数:NetMessageBufferSend的返回值问题 为什么C#不像Java一样抛出NullReferenceException异常呢? 哪位老兄去过速达软件面试过。考了些什么。
{
this.SetRow();
} private void dgMain_CurrentCellChanged(object sender, System.EventArgs e)
{
this.SetRow();
} private void SetRow()
{
index = this.dgMain.CurrentRowIndex;
if( index > -1 )
{
this.dgMain.Select( index );
}
}
dataGrid1.Select(dataGrid1.CurrentRowIndex);你可以写在OnClick里面或者OnCurrentCellChanged事件里面"这样以后,还是可以选中单个单元格(背景变灰),如何真正选择整行,就像在listview里一样
{
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if(hti.Type == DataGrid.HitTestType.Cell)
{
dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column);
dataGrid1.Select(hti.Row);
}
}
public class ViewColumnStyle : DataGridColumnStyle
{
// The isEditing field tracks whether or not the user is
// editing data with the hosted control.
private bool isEditing;
private int nSelectedRowIndex; public ViewColumnStyle() : base()
{
nSelectedRowIndex = -1;
} protected override void Abort(int rowNum)
{
isEditing = false;
Invalidate();
} protected override bool Commit
(CurrencyManager dataSource, int rowNum)
{
if (!isEditing)
return true; isEditing = false; Invalidate();
return true;
} protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
if (!this.ReadOnly)
{
return;
}
if(nSelectedRowIndex> -1 && nSelectedRowIndex< source.List.Count + 1)
{
this.DataGridTableStyle.DataGrid.UnSelect(nSelectedRowIndex);
}
this.DataGridTableStyle.DataGrid.Select(rowNum);
nSelectedRowIndex= rowNum;
} protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, 4);
} protected override int GetMinimumHeight()
{
return 4;
} protected override int GetPreferredHeight(Graphics g,
object value)
{
return 4;
} protected override void Paint(Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
object objValue = GetColumnValueAtRow(source, rowNum);
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(objValue.ToString(),
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
} protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
}
}
public class ListDataGrid:System.Windows.Forms.DataGrid
{
private Point m_CurPoint;
private DataView m_ListDataView;
private DataGridTableStyle tableStyle;
/// <summary>
/// 指定数据绑定并显示
/// </summary>
/// <param name="dvw"></param>
/// <param name="TemplateName"></param>
public void SetListData(DataView dvw)
{
if (tableStyle==null)
{
tableStyle = new DataGridTableStyle();
tableStyle.MappingName =dvw.Table.TableName;
int numCols = dvw.Table.Columns.Count;
ViewColumnStyle textColumn;
for (int i=0;i<dvw.Table.Columns.Count;i++)
{
textColumn = new ViewColumnStyle();
textColumn.ReadOnly=true;
textColumn.NullText="";
textColumn.HeaderText = dvw.Table.Columns[i].Caption;
textColumn.MappingName = dvw.Table.Columns[i].ColumnName;
tableStyle.GridColumnStyles.Add(textColumn);
} tableStyle.AllowSorting=false;
tableStyle.RowHeadersVisible=false;
this.TableStyles.Clear();
this.TableStyles.Add(tableStyle);
}
this.BeginInit();
this.SetDataBinding(dvw,null);
this.EndInit();
dvw.AllowDelete=true;
dvw.AllowEdit=true;
m_ListDataView=dvw;
}
protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData)
{
if (this.DataSource==null)
{
return true;
}
switch (keyData)
{
case Keys.Space:
if (OnSpace!=null)
{
OnSpace(this,this.CurrentRowIndex);
}
return true;
case Keys.Return:
if (OnReturn!=null)
{
OnReturn(this,this.CurrentRowIndex);
}
base.ProcessDialogKey(keyData);
this.Select(this.CurrentRowIndex);
return true;
case Keys.Up:
if (this.CurrentRowIndex==0)
{
this.Select(0);
return true;
}
else
{
return false;
}
case Keys.Down:
int nRowCount=0;
if (this.ListDataView.AllowNew)
{
nRowCount=((DataView)this.DataSource).Table.Rows.Count;
}
else
{
nRowCount=((DataView)this.DataSource).Table.Rows.Count-1;
}
if (this.CurrentRowIndex==nRowCount && this.CurrentRowIndex!=-1)
{
this.Select(this.CurrentRowIndex);
return true;
}
else
{
return false;
}
case Keys.Left:
return true;
case Keys.Right:
return true;
case Keys.Tab:
base.ProcessDialogKey(keyData);
return true;
case Keys.Delete:
return true;
default:
return false;
}
}
}