http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q689q5.11 How can I select the entire row when the user clicks on a cell in the row? Call the DataGrid.Select method from within its mouseup event.
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q785q5.31 How do I get the row or column that has been clicked on? You can use the DataGrid's HitTest method, passing it a point in the grid's client coordinate system, and returning a HitTestInfo object that holds all the row and column information that you want.
[C#]
// X & Y are in the grid' coordinates. If they are in screen coordinates, call dataGrid1.PointToClient method
[C#]
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);
}
}
[VB/NET]
Private Sub dataGrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dataGrid1.MouseUp
Dim pt = New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(pt)
If hti.Type = DataGrid.HitTestType.Cell Then
dataGrid1.CurrentCell = New DataGridCell(hti.Row, hti.Column)
dataGrid1.Select(hti.Row)
End If
End Sub
http://blog.blogchina.com/websites/1038449.html
[C#]
// X & Y are in the grid' coordinates. If they are in screen coordinates, call dataGrid1.PointToClient method
System.Drawing.Point pt = new Point(X, Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if(hti.Type == DataGrid.HitTestType.Cell)
{
MessageBox.Show(dataGrid1[hti.Row, hti.Column].ToString());
}
else if(hti.Type == DataGrid.HitTestType.ColumnHeader)
{
MessageBox.Show(((DataView) DataGrid1.DataSource).Table.Columns[hti.Column].ToString());
}
[VB.NET]
' X & Y are in the grid' coordinates. If they are in screen coordinates, call dataGrid1.PointToClient method
Dim pt = New Point(X, Y)
Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(pt)
If hti.Type = DataGrid.HitTestType.Cell Then
MessageBox.Show(dataGrid1(hti.Row, hti.Column).ToString())
Else
If hti.Type = DataGrid.HitTestType.ColumnHeader Then 'assumes datasource is a dataview
MessageBox.Show(CType(DataGrid1.DataSource, DataView).Table.Columns(hti.Column).ToString())
End If
End If
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q869q
void handleCommands(object sender,datagridCommandeventargs e)
{
e.item.itemindex;
}
在DataGrid的ItemDataBound方法里加上一句:
e.Item.Attributes.Add("ID",e.Item.ItemIndex.ToString())
e.Item.Attributes.Add("onclick","javascript:return TRClick(this.id)");
客户端脚本
<script language=javascript>
function TRClick(value)
{
alert(value);
}
</script>
可我要处理很烦的逻辑,html 不好写的
</a>
<script language="javascript">
function setValue(strValue)
{
window.open("type.aspx?id="+strValue.innerText);}
</script>
承上:
在客户端里加一个TextBox:
<asp:TextBox ID=Text Runat=server style="display:none"></asp:TextBox>
客户端脚本
<script language=javascript>
function TRClick(value)
{
document.all("Test",value);
__doPostBack("Test","");
}
</script>
在TextBox里的TextChanged要注意在使用完TextBox的内容时一定要把它清空,否则如果再次单击这一行时因为内容相同将不会提交的.
同时如果在客户端脚本中出现找到不到__doPostBack这个方法的话,可以把刚才的TextBox的定义改改为:
<asp:TextBox ID=Text Runat=server style="display:none" AutoPostBack=True ></asp:TextBox>
系统就会自动给你生成__doPostBack这个方法的了(__为两个下划线).