日期:2014-05-19  浏览次数:20948 次

如何在GridView显示出来的数据中任意单击一行数据的任一地方就可以选中此行
想在GridView查询出来的数据中单击一下任何一行就能选中此行.请教一下代码怎么写,该写在哪个地方

------解决方案--------------------
沙发 我要分哦
------解决方案--------------------
DataView的实现方法应该跟DataGrid控件的实现方法相同的!下面是我用DataGrid实现的一个例子
在页面的Load函数中绑定下面两个函数:
BindDataToDataGrid();//邦定数据到DataGrid中,你也可在此函数中绑定数据到你的GridView中
DataGridStateControl();//此方法主要就是实现你想要的功能(任意单击一行数据的任一地方就可以选中此行)
private void DataGridStateControl()
{
DataGridTableStyle ts = new DataGridTableStyle();
DataGridNoActiveCellColumn aColumnTextColumn;
//DataGridNoActiveCellColumn 是下面另外定义的一个类。
ts.AlternatingBackColor = Color.LightGray;
ts.MappingName = tempTable.TableName;
ts.AllowSorting = true;//允许进行排序
int numCols = tempTable.Columns.Count;
for (int i = 0;i < numCols;i++) //从第二列开始,不显示第一列的“职员编号”
{
aColumnTextColumn = new DataGridNoActiveCellColumn();
aColumnTextColumn.MappingName = tempTable.Columns[i].ColumnName;
aColumnTextColumn.HeaderText = tempTable.Columns[i].ColumnName;
aColumnTextColumn.NullText = " ";
aColumnTextColumn.Format = "D ";
ts.GridColumnStyles.Add(aColumnTextColumn);
}
this.dataGrid1.TableStyles.Add(ts);

}


using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace CustomerInfo
{
internal class DataGridNoActiveCellColumn : 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);
}
}
}

------解决方案--------------------
http://www.cnblogs.com/Jinglecat/archive/2007/07/15/818394.html