首先在html代码中加入script代码
<script>
function SelectDataGridRow(DataGridName,rowIndex)
{
var mytable=document.getElementById(DataGridName);
mytable.rows[rowIndex].oldcolor=mytable.rows[rowIndex].style.backgroundColor;
mytable.rows[rowIndex].style.backgroundColor='#C8F7FF';
for(var i=0;i<mytable.rows.length;i++)
{
if(i!=rowIndex)
{
mytable.rows[i].style.backgroundColor=mytable.rows[i].oldcolor;
}
}
}
</script>
function SelectDataGridRow(DataGridName,rowIndex)
{
var mytable=document.getElementById(DataGridName);
mytable.rows[rowIndex].oldcolor=mytable.rows[rowIndex].style.backgroundColor;
mytable.rows[rowIndex].style.backgroundColor='#C8F7FF';
for(var i=0;i<mytable.rows.length;i++)
{
if(i!=rowIndex)
{
mytable.rows[i].style.backgroundColor=mytable.rows[i].oldcolor;
}
}
}
</script>
然后在cs文件中给DataGrid的ItemDataBound事件加入代码
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item)
{
e.Item.Attributes.Add("onclick",
"SelectDataGridRow('DataGrid1',this.rowIndex);");
e.Item.Attributes.Add("ondblclick",
"alert('您选择了第:'+this.rowIndex+'行,第3列的文字内容为:'+this.cells[2].innerText);");
}
}

浙公网安备 33010602011771号