C# 双击GridView的行

C# 双击GridView的行,c#,asp.net,gridview,C#,Asp.net,Gridview,我希望能够双击GridView的行,并重定向到显示所单击行详细信息的页面 我有我正在使用的代码,它看起来应该可以工作,但我得到了错误“对象引用未设置为对象的实例” 由于某种原因,该行显示为空,我不确定原因。我的代码如下。有人能看出我做错了什么吗 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" EnableViewState="true" OnDataBound="GridView1_DataBound" OnRo

我希望能够双击
GridView
的行,并重定向到显示所单击行详细信息的页面

我有我正在使用的代码,它看起来应该可以工作,但我得到了错误“对象引用未设置为对象的实例”

由于某种原因,该行显示为空,我不确定原因。我的代码如下。有人能看出我做错了什么吗

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" EnableViewState="true" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound"
    AllowSorting="True" AutoGenerateColumns="False" BorderStyle="Solid" GridLines="Both" HeaderStyle-BackColor="#990033" Width="1000px" 
    DataSourceID="EntityDataSource1">
     <HeaderStyle ForeColor="White"></HeaderStyle>
    <Columns>            
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Bind("intBatchID") %>' NavigateUrl="TestPage1.aspx?intBatchID={0}" Target="_blank"></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="vcharName" HeaderText="Name" ReadOnly="True" 
            SortExpression="vcharName" />
        <asp:BoundField DataField="dtmScheduled" HeaderText="Date Scheduled" 
            ReadOnly="True" SortExpression="dtmScheduled" />
        <asp:BoundField DataField="intBatchPriorityLevel" 
            HeaderText="Priority Level" ReadOnly="True" 
            SortExpression="intBatchPriorityLevel" />
    </Columns>
    <PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" PageButtonCount="4" PreviousPageText="Previous" NextPageText="Next" FirstPageText="First" LastPageText="Last" />
    <PagerStyle HorizontalAlign="Center" />        
</asp:GridView>   
You are viewing page <%=GridView1.PageIndex + 1%> of <%=GridView1.PageCount%>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" OnSelected="EntityDataSource1_Selected"  
    ConnectionString="name=TestEntities" DefaultContainerName="TestEntities" 
    EnableFlattening="False" EntitySetName="tbl_Batch" 
    Select="it.[intBatchID], it.[vcharName], it.[dtmScheduled], it.[intBatchPriorityLevel]" OrderBy="it.[intBatchID]">
</asp:EntityDataSource>



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            System.Data.DataRowView drv = e.Row.DataItem as System.Data.DataRowView;
            e.Row.Attributes.Add("ondblclick", String.Format("window.location='TestPage1.aspx?intBatchID={0}'", drv["intBatchID"]));
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onMouseOver", "Highlight(this)");
            e.Row.Attributes.Add("onMouseOut", "UnHighlight(this)");
        }
    }

您正在查看第页,共页
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
System.Data.DataRowView drv=e.Row.DataItem作为System.Data.DataRowView;
e、 Add(“ondblclick”,String.Format(“window.location='TestPage1.aspx?intBatchID={0}'”,drv[“intBatchID]”);
}
如果(e.Row.RowType==DataControlRowType.DataRow)
{
e、 添加(“onMouseOver”、“Highlight(this)”);
e、 添加(“onMouseOut”、“UnHighlight(this)”;
}
}

首先,我会进行一些检查。其次,从您的评论来看,您似乎正在处理
物化数据记录
,因此尝试将其转换为
数据行视图
是行不通的



C# 尝试将其强制转换为
DbDataRecord

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var dr = e.Row.DataItem as System.Data.Common.DbDataRecord;

        if (dr != null && !Convert.IsDBNull(dr["intBatchID"]))
        {
            int intBatchId;
            int.TryParse(dr["intBatchID"].ToString(), out intBatchId);

            if (intBatchId > 0)
            {
                string js = "ChangeBatchId(" + intBatchId.ToString() + ");";
                e.Row.Attributes.Add("ondblclick", js);
            }
        }
        e.Row.Attributes.Add("onMouseOver", "Highlight(this);");
        e.Row.Attributes.Add("onMouseOut", "UnHighlight(this);");
    }
}


JAVASCRIPT
function ChangeBatchId(batchId)
{
    // uncomment to debug batch id.
    // console.log("BatchID: " + batchId.toString());

    window.location.href = 'TestPage1.aspx?intBatchID=' + batchId.toString();
}