Home > DeveloperSection > Forums > Double Click Row of GridView
Tanuj Kumar

Total Post:134

Points:940
Posted on    December-17-2013 12:54 AM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 1436  View(s)
Rate this:

I want to double click the row of a GridView and be redirected to a page that shows the details of the row that was clicked.

I have code that I'm working with and it seems like it should work, but I'm getting the error "Object reference not set to an instance of an object".

For some reason the row comes up null and I'm not sure why.

 My code is below, can someone see what I'm doing wrong?

<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)");

        }

    }



Pravesh Singh

Total Post:411

Points:2881
Posted on    December-17-2013 1:30 AM

Hi Tanuj,

Try this :

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;

        if (drv != null && Convert.IsDBNull(drv["intBatchID"]))

            e.Row.Attributes.Add("ondblclick", String.Format("window.location='TestPage1.aspx?intBatchID={0}'", drv["intBatchID"]));

        e.Row.Attributes.Add("onMouseOver", "Highlight(this)");

        e.Row.Attributes.Add("onMouseOut", "UnHighlight(this)");

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick