Solved

selecting a row in a gridview

Posted on 2008-10-20
2
339 Views
Last Modified: 2013-11-07
Okay, I will attempt to make this question a bit more simple.  I have a gridview on a webpage that displays user information. What I am trying to do is allow that person to click on that row and it redirects them to another page to edit that partcular row.
0
Comment
Question by:smitty68521
  • 2
2 Comments
 
LVL 6

Accepted Solution

by:
M3mph15 earned 500 total points
ID: 22765113
Ok first step is to hide the select button column in the gridview by setting it's css-class:
<Columns>
            <asp:CommandField ShowSelectButton="True" ItemStyle-CssClass="HiddenColumn" HeaderStyle-CssClass="HiddenColumn">
                <HeaderStyle CssClass="HiddenColumn"></HeaderStyle>
                <ItemStyle CssClass="HiddenColumn"></ItemStyle>
            </asp:CommandField>
 </Columns>

Then in the code behind we place this code in the RowDataBound event of the gridView:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
'This removes the use of the Select option
'To select a row jsut click on it
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';")
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString))
End If
End Sub
And then you can find which row has been selected via code in the SelectedIndexChanged event of the GridView:
For example maybe you wanted to get the data in Cell 5 to add into the querystring when you redirect to the other page so you know what data to display:
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

Dim s As String = GridView1.SelectedRow.Cells(5).Text
'Then redirect user to different page
Response.Redirect("otherPage.aspx?item=" & s
End Sub
Hope this helps.
-M3mph15
0
 
LVL 6

Expert Comment

by:M3mph15
ID: 22765116
oh forgot he code to the HiddenColumn css Class:
<style type="text/css">
.HiddenColumn
{
      display:none;
}
</style>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reactjs with .NET 3 75
IDE for Python 5 69
Consume a webservice via VB in Visual Studio 2015 3 20
VB.Net Data Class 1 21
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question