Selecting row in datagrid by clicking everyway on the item

Discussion in 'ASP .Net Datagrid Control' started by Liuc, Nov 12, 2003.

  1. Liuc

    Liuc Guest

    I would select rows in datagrid by clicking on the desired row.
    Someone csn tell me how i can do that?
    Thanks.
     
    Liuc, Nov 12, 2003
    #1
    1. Advertising

  2. Hi,

    This is addressed in this article:

    Top Questions about the DataGrid Web Server Control
    http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vbtchTopQuestionsA
    boutASPNETDataGridServerControl.asp

    Thank you, Mike
    Microsoft, ASP.NET Support Professional

    Microsoft highly recommends to all of our customers that they visit the
    http://www.microsoft.com/protect site and perform the three straightforward
    steps listed to improve your computer’s security.

    This posting is provided "AS IS", with no warranties, and confers no rights.


    --------------------
    > From: (Liuc)
    > Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
    > Subject: Selecting row in datagrid by clicking everyway on the item
    > Date: 12 Nov 2003 05:41:03 -0800
    > Organization: http://groups.google.com
    > Lines: 3
    > Message-ID: <>
    > NNTP-Posting-Host: 193.204.78.225
    > Content-Type: text/plain; charset=ISO-8859-1
    > Content-Transfer-Encoding: 8bit
    > X-Trace: posting.google.com 1068644463 2886 127.0.0.1 (12 Nov 2003

    13:41:03 GMT)
    > X-Complaints-To:
    > NNTP-Posting-Date: Wed, 12 Nov 2003 13:41:03 +0000 (UTC)
    > Path:

    cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
    e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnews1.google.com!no
    t-for-mail
    > Xref: cpmsftngxa06.phx.gbl

    microsoft.public.dotnet.framework.aspnet.datagridcontrol:7415
    > X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
    >
    > I would select rows in datagrid by clicking on the desired row.
    > Someone csn tell me how i can do that?
    > Thanks.
    >
     
    Mike Moore [MSFT], Nov 12, 2003
    #2
    1. Advertising

  3. Liuc

    Liuc Guest

    Thak you for the answer, but i can't find the article.
     
    Liuc, Nov 13, 2003
    #3
  4. Hi,

    I copied the link from my previous post, removed the carriage return, and
    pasted the result into a browser and it did get the article.

    In any case, here is the text from the click anywhere section of the
    article.

    Selecting Rows by Clicking Anywhere
    The default model for selecting rows in the grid is for you to add a Select
    button (actually, a LinkButton control) whose CommandName property is set
    to "Select." When the button is clicked, the DataGrid control receives the
    Select command and automatically displays the row in selected mode.

    Not everyone likes having an explicit Select button, and a common question
    is how to implement the feature where users can click anywhere in a grid
    row to select it. The solution is to perform a kind of sleight-of-hand in
    the grid. You add the Select LinkButton control as normal. Users can still
    use it, or you can hide it. In either event, you then inject some client
    script into the page that effectively duplicates the functionality of the
    Select button for the row as a whole.

    The example below shows how. In the grid's ItemDataBound handler, first
    make sure that you are not in the header, footer, or pager. Then get a
    reference to the Select button, which in this instance is assumed to be the
    first control in the first cell. You then call a little-known method called
    GetPostBackClientHyperlink. This method returns the name of the postback
    call for the designated control. In other words, if you pass in a reference
    to a LinkButton control, it returns the name of the client function call
    that will perform the postback.

    Finally, you assign the client-side method to the item itself. When the
    grid renders, it renders as an HTML table. By assigning the method to the
    item, it is the equivalent of adding client-side code to each row (<TR>
    element) in the table. The grid's Item object does not directly support a
    way to assign client code to it, but you can do that by using its
    Attributes collection, which passes anything you assign to it through to
    the browser.

    Note One small disadvantage of this technique is that it adds somewhat to
    the stream rendered to the browser, and it adds information for each row to
    view state.
    ' Visual Basic
    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    Handles DataGrid1.ItemDataBound
    Dim itemType As ListItemType = e.Item.ItemType
    If ((itemType = ListItemType.Pager) Or _
    (itemType = ListItemType.Header) Or _
    (itemType = ListItemType.Footer)) Then
    Return
    Else
    Dim button As LinkButton = _
    CType(e.Item.Cells(0).Controls(0), LinkButton)
    e.Item.Attributes("onclick") = _
    Page.GetPostBackClientHyperlink(button, "")
    End If
    End Sub

    // C#
    private void DataGrid1_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    ListItemType itemType = e.Item.ItemType;
    if ((itemType == ListItemType.Pager) ||
    (itemType == ListItemType.Header) ||
    (itemType == ListItemType.Footer))
    {
    return;
    }
    LinkButton button = (LinkButton)e.Item.Cells[0].Controls[0];
    e.Item.Attributes["onclick"] =
    Page.GetPostBackClientHyperlink(button, "");
    }


    Thank you, Mike
    Microsoft, ASP.NET Support Professional

    Microsoft highly recommends to all of our customers that they visit the
    http://www.microsoft.com/protect site and perform the three straightforward
    steps listed to improve your computer’s security.

    This posting is provided "AS IS", with no warranties, and confers no rights.


    --------------------
    > From: (Liuc)
    > Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
    > Subject: Re: Selecting row in datagrid by clicking everyway on the item
    > Date: 13 Nov 2003 00:13:14 -0800
    > Organization: http://groups.google.com
    > Lines: 1
    > Message-ID: <>
    > References: <>

    <>
    > NNTP-Posting-Host: 193.204.78.252
    > Content-Type: text/plain; charset=ISO-8859-1
    > Content-Transfer-Encoding: 8bit
    > X-Trace: posting.google.com 1068711194 15250 127.0.0.1 (13 Nov 2003

    08:13:14 GMT)
    > X-Complaints-To:
    > NNTP-Posting-Date: Thu, 13 Nov 2003 08:13:14 +0000 (UTC)
    > Path:

    cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
    e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnews1.google.com!no
    t-for-mail
    > Xref: cpmsftngxa06.phx.gbl

    microsoft.public.dotnet.framework.aspnet.datagridcontrol:7428
    > X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
    >
    > Thak you for the answer, but i can't find the article.
    >
     
    Mike Moore [MSFT], Nov 13, 2003
    #4
  5. Liuc

    Liuc Guest

    Great! Thank you!
     
    Liuc, Nov 14, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. misiek
    Replies:
    0
    Views:
    2,299
    misiek
    Feb 20, 2006
  2. prichards14

    Selecting related data in multiple DataGrids when clicking a row

    prichards14, Dec 17, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    130
    prichards14
    Dec 17, 2004
  3. sa5webber

    Selecting Datagrid Row by Clicking Anywhere

    sa5webber, Feb 15, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    144
    sa5webber
    Feb 15, 2005
  4. Sameeksha
    Replies:
    0
    Views:
    124
    Sameeksha
    Apr 7, 2005
  5. Vikash Kumar
    Replies:
    2
    Views:
    159
    Alex Young
    Dec 20, 2006
Loading...

Share This Page