C# 在gridview(非datagridview)asp.net中获取单击的单元格索引

C# 在gridview(非datagridview)asp.net中获取单击的单元格索引,c#,asp.net,gridview,C#,Asp.net,Gridview,当我在gridview(不是datagridview)中单击单元格时,我想获取单元格索引(不是行索引),而不是行索引。我使用asp.net-c#使用RowCreated事件注册一个单元格单击每个单元格并处理GridView的

当我在gridview(不是datagridview)中单击单元格时,我想获取单元格索引(不是行索引),而不是行索引。我使用asp.net-c#

使用
RowCreated
事件注册一个单元格单击每个单元格并处理
GridView的

protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++ )
        {
            TableCell cell = e.Row.Cells[i];
            cell.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
            cell.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            cell.ToolTip = "You can click this cell";
            cell.Attributes["onclick"] = string.Format("document.getElementById('{0}').value = {1}; {2}"
               , SelectedGridCellIndex.ClientID, i
               , Page.ClientScript.GetPostBackClientHyperlink((GridView)sender, string.Format("Select${0}", e.Row.RowIndex)));
        }
    }
}

protected void SelectedIndexChanged( Object sender, EventArgs e)
{
    var grid = (GridView) sender;
    GridViewRow selectedRow = grid.SelectedRow;
    int rowIndex = grid.SelectedIndex;
    int selectedCellIndex = int.Parse(this.SelectedGridCellIndex.Value);
}
您需要禁用此页面的事件验证,否则ASP.NET会投诉:

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="Grid.aspx.cs" Inherits="CSharp_WebApp.Grid" %>


ASP.NET
中没有
DataGridView
,那么你真的是指ASP.NET吗?就像我写的gridview(不是DataGridView)如果你写的是“gridview(不是datagrid)”,那就有意义了,否则这些信息是多余的,因为ASP.NET不是winforms。但我明白了。我写这篇文章是因为我写了类似的问题,我有DataGridView的答案谢谢你的回答,但我应该在SelectedIndexChangedEvent中写什么?@zvonnkko:现在我已经测试过了,你需要一个稍微不同的方法。看看我编辑过的答案。
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="Grid.aspx.cs" Inherits="CSharp_WebApp.Grid" %>