how can i get index of row in grid by clicking on grid row

how can i get index of row in grid by clicking on grid row
0
shafqat
4/27/2007 11:07:35 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

7 Replies
750 Views

Similar Articles

[PageSpeed] 51

Hi there,

Under the selectedindexchanged event of the gridview put this:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

int index= GridView1.SelectedIndex;//to get the index

GridViewRow row= GridView1.SelectedRow; //to get the selected row

thanks,

Murthy 


Dont forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
0
Murthy
4/27/2007 12:00:51 PM

Once you get the index of the row, how do you access elements in the row, like the key field?

 -smc


~ Remember To Mark The Posts Which Helped You As The ANSWER ~
0
smcirish
4/27/2007 1:23:06 PM

I added this method, SelectedIndexChange, and put a breakpoint here.   This subroutine is never accessed on click during debug.

I added a hyperlink column RC on Grid > Property builder.  I am using datagrid, not gridview

 

Please advise,

-smcirish

 

 


~ Remember To Mark The Posts Which Helped You As The ANSWER ~
0
smcirish
4/29/2007 1:34:01 AM

Hi:

  For now I just have a version for GridView, but I think they are similar, try to refer to this to write your own code. Just change each "Row" to "Item":

 the JS:

<head>

<script type="text/javascript">
    function Clk(t)
    {
    document.getElementById("Hidden1").value=t;
    form1.submit();
    }
 </script>

</head>

controls in aspx:

 

<form id="form1" action="Default.aspx" runat="server">

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <input id="Hidden1" name="Hidden1"  value="false" type="hidden" />
  <asp:GridView AllowPaging="true" PageSize="5" ID="GridView3" runat="server" DataSourceID="SqlDataSource1" OnRowDataBound="GridView3_RowDataBound" OnPageIndexChanging="GridView3_PageIndexChanging">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="Label5" runat="server" Text='<%# Eval("theName") %>'></asp:Label>
    </ItemTemplate></asp:TemplateField></Columns></asp:GridView>

in code behind:

  protected void Page_Load(object sender, EventArgs e)
    {
     
        this.GridView3.DataBind();

//others

}

 protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            if (Request.Form["Hidden1"] != null && e.Row.RowIndex == Convert.ToInt32(Request.Form["Hidden1"]))
                this.Label1.Text = ((Label)e.Row.FindControl("Label5")).Text;          
            e.Row.Attributes.Add("onclick", "Clk(" + e.Row.RowIndex + ")");
            e.Row.Style["cursor"] = "hand";
        }


    }

If you still can not understand, just inform us, I'll show you a DataGrid version.

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
5/1/2007 7:51:00 AM

I don't have RowDataBound method available.  Could you send a DataGrid example in Visual Basic?

 thanks,

smc


~ Remember To Mark The Posts Which Helped You As The ANSWER ~
0
smcirish
5/1/2007 12:52:46 PM

Hi:

  Here's the VB version:

    Protected Sub GridView3_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid3.ItemDataBound

 

        If (Not Request.Form("Hidden1") Is Nothing) And e.Item.ItemIndex = Convert.ToInt32(Request.Form("Hidden1")) Then

            Dim l As Label = CType(e.Item.FindControl("Label5"), Label)
            Me.Label1.Text = l.Text
        End If
        e.Item.Attributes.Add("onclick", "Clk(" & e.Item.ItemIndex & ")")
        e.Item.Style("cursor") = "hand"

 


    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.DataGrid3.DataBind()
    End Sub

 <head runat="server">
    <title>Untitled Page</title>
<script type="text/javascript">
    function Clk(t)
    {
    document.getElementById("Hidden1").value=t;
    form1.submit();
    }
 </script>

</head>

 <form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <input id="Hidden1" name="Hidden1"  value="false" type="hidden" />
  <asp:DataGrid AutoGenerateColumns="false" AllowPaging="true" PageSize="5" ID="DataGrid3" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="GridView3_ItemDataBound" >
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
        <asp:Label ID="Label5" runat="server" Text='<%# Eval("theName") %>'></asp:Label>
    </ItemTemplate></asp:TemplateColumn></Columns></asp:DataGrid>

 

If it doesn't work, please inform us.

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
5/3/2007 4:07:02 AM

Extra bit ...

Do you actually need the Item Index? Is ItemDataBound apporpriate?

  ' Where your DataGrid has Id DataGrid1
    Private Sub Datagrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles DataGrid1.ItemDataBound

        Dim index As Integer = e.Item.ItemIndex

        Dim cell1Text As String = e.Item.Cells(0).Text
        ' Do some work 
        '......

    End Sub

 ..................

' Where your DataGrid has ID DataGrid1
    Private Sub Datagrid_ItemCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles DataGrid1.ItemCommand

        Dim index As Integer = e.Item.ItemIndex

        Select Case e.CommandName.ToLower()

            Case "update"
                ' do work
            Case "delete"
                ' do work

        End Select
    End Sub
 
Rgds,
Martin.

For the benefit of all users please mark any post answers as appropriate.
0
mokeefe
5/3/2007 6:54:17 AM
Reply:

Similar Artilces:

To get the row index of selected row in grid view
Hi I have a grid view with a column of checkboxes. if I check a checkbox all the remaining checkboxes should be unchecked except the current one.protected void chkHeadCoach_CheckedChanged(object sender, EventArgs e) {foreach (GridViewRow rowitem in grdv.Rows) {CheckBox chkHeadCoach = (CheckBox)(rowitem.FindControl("chkHeadCoach"));if (chkHeadCoach.Checked) {if(rowitem.RowIndex != //row index of the row I checked now) {chkHeadCoach.Checked = false; } } } how can I get the row index of the row I checked now? }Thanks & Regards,Abhiram Reddy Mekha. AbhiramReddyMekh...

How to delete row from GridView control
Hello,  I am working on an ASP.NET 2.0 app, and I am trying to wire up a "Delete" button to a gridview control on a form. Here is the code for the ASP :  <asp:GridView ID="gvInConditions" runat="server" AutoGenerateColumns="true" OnRowDeleting="gvInConditions_RowDeleting" DataKeyNames="Condition_ID" >         <Columns>                <asp:CommandField ButtonType="Button" SelectText=&...

vb.net ... I click the Select in my grid but previous row gets passed to my variable.
Hello all. I'm a newbie to asp.net/vb.net. The dropdown part works fine but I included it so you get an overall idea: Steps:  - select value from dropdown - pass value from dropdown to variable "kb" -use variable "kb" in SQL String "strSOKICKBACKS" -use "strSOKICKBACKS" to build grid "SOKickbacksGridView" -user Selects row in grid  "SOKickbacksGridView" - pass sales order from selected row in "SOKickbacksGridView" to variable "fsono" -use variable "fsono" in SQ...

How to get the row index of a selected row in listview control
Can someone please tell me how to get the index of a selected row in a listview.  <ItemTemplate> <asp:LinkButton runat="server" class="linkButton" id="xxxxx" Text='<%#Eval("documenttype") %>' CommandName="OpenFile" CommandArgument='<%#Eval("DocumentKey") %>'></asp:LinkButton></ItemTemplate>what you have to do is, in itemcommand event of ListView, you have to get the value of row indexif(e.CommandName == "corrospondingcommaandname"){     &nb...

How to get grid view row index .......
i have grid view. One of the columns contain radio buttons only. i want to handle the check changed event of the radio button. i m coding for it. every radio button has the same ID as they are created at runtime using a template field. Can i know the grid view row index whose radio button is checked/unchecked while handling the event? Please help! Like this: protected void RadioButton1_CheckedChanged(object sender, EventArgs e) { RadioButton rad = sender as RadioButton; GridViewRow row = rad.NamingContainer as GridViewRow; int rowIndex = row.RowIndex; }Thanks, EdMicroso...

How to get the count of rows in a grid control
How can I check a grid control to see if there are rows of data in it, using VB.NET? Is the MyGrid.Items.Count >0 the best way to do this? Thanks, Thank youJackxxx That's what I use... report_DatGrid.Items.Count > 0 ...

Getting Control Values from my Dynamic Grid Rows
We have a products database with over 400 fields.  To allow our users to edit these products they select "Groups" of fields to edit.  A grid is dynamically generated from a database driven list of field objects (field name, dbase name, header, format, datatype, etc).  It is then have a DataBind method to bind the data to our Products object which olds the actual data.  One other thing that distinguishes this grid is that every field is a control in a TemplateField.  There are no BoundField columns.  The reason we do this is so that our users can edit the product...

how i can get all the information of specific row on clicking of any place of that row in datagrid
in my datagrid i have 4 columns. Among these one column is Type. Suppose this column has 3 different type like 1. question 2. section 3. option. so if i click any row of datagrid then i want to call different aspx page according to columnType.for example if type is question thenon clicking on it. i want to call Response.Redirect("question.aspx?id=" + _id.toString);i. e. i want to call different aspx according to type of that column and also i want to sent querystring id to that specific id belonging to first column of datagrid.ex:-'id'     'create date'    ...

Grid rows scroll out of grid
Drop a TTable, TDataSource, TDBGrid, and TButton on a form. Align the grid to the bottom and have it cover about half the form. Put the button above it. Hook the grid, data source, and table to the Biolife.db table in the DBDEMOS database. Put the following in the button's OnClick event: Table1.First; while not Table1.Eof do Table1.Next; Run the app and click the button. On Vista Home Premium, it works fine. On Windows 7 64-bit Home Premium on a Toshiba laptop, the records scroll up to cover the header row and a few lines above that. I tried Delphi 2006 and Delphi XE. Same resul...

Get clicked row datas and updating that from Grid View
I want Grid view datas of clicked row datas to other forms and I change and saved that datas. How can i possible this?Endendrum Priyamudan P m R Hi Mohanet, I suggest you can make row clickable first in GridView by this: protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)    {        if (e.Row.RowType == DataControlRowType.DataRow)        {            e.Row.Attributes.Add("onmouseover", "this.style.curso...

Get the index of selected columns and rows in grid DW
Hello! Is it possible to evaluate the row and column number in grid DW which user has selected? Thanks in advance, Vladimir Kolykhalov Vlad; Check the arguments on many of the DW Control events. The Row and column (DWO pointer) are given. Also, at any time you can use the GetRow ( ) and GetColumn ( ) methods. regards ... Chris "Vladimir A. Kolykhalov" <kolykhalov@yandexDOTru> wrote in message news:414818c3$1@forums-2-dub... > Hello! > > Is it possible to evaluate the row and column number in grid DW which user > has selected? > ...

How to Redirect the Button Clicked One Row in the Grid View to Another Page Grid View
 Please any one can help me out in this problem.I Have an Grid View in The Page1.aspx ,if i Click the Button in The GridView it should only redirect the Button Clicked Row to Another Page Page2.aspx and itShould only display the Button Clicked Row of Grid in Page1.aspx to Grid in Page2.aspx i.e only one row should redirect from Page1.aspx to Page2.aspx Grid. Pass the primary key value of the gridview row in the querysing to the page 2. In the page2 use the querystring value to fetch the data. in Page 2 use where clause to fetch only data for the row whcih matches the value in que...

select row grid with arrow key (up&down) and get datakey this row whit enter key
hi friends how i can select row grid with arrow key (up&down) and get datakey this row whit enter key but that in client side code plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz  help me thanks a lot     this article talks about how to bind keyboard shortcut to Click event of the botton http://www.vikramlakhotia.com/Binding_keyboard_shortcut_in_AspNet.aspx also this article discusses the same http://forums.asp.net/p/1119365/1748317.aspx#1748317Vikram www.vikramlakhotia.comPlease mark the answer if it helped you hi vik20000in  tanks a ...

ADO.net problem: i can not view image data for the rest of my table rows except the image for the first row but i can read all the other non image fields for all rows.
Hi i have a table with the following fields imgid............int imgdata........image imgtitle.........varchar(max) imgType........varchar(max) imgLength.....bigint incident_id.....int i have no problem with uploading the image but viewing the uploaded image(s) in a gridview has caused me great pain. The probelm is i have two pages. page1 grabs the image to display and page2 has a gridview that displays the grabed image. I wanted to have an sql statement like SELECT * FROM mytable WHERE incident_id=someValue. IF i have 5 records in this table and i view page2 ...

Web resources about - how can i get index of row in grid by clicking on grid row - asp.net.presentation-controls

WARNING: Unhappy Meals for Facebook Users Clicking Links to $250 McDonald’s Gift Cards
Facebook users who click on links promising them $250 gift cards to McDonald’s are becoming victims of The Hamburglar, in the form of online ...

Facebook tests subscribe button for pages to allow users to receive updates without clicking Like
... Marketing Land reader Craig Smith: We do not have the button on our own accounts, but here is the way to connect with a page without clicking ...

Before they noticed the camera clicking - Flickr - Photo Sharing!
I spotted Kara grooming Melba and got 3 photos: this one, a blurred one and the "After" shot. I'm thinking maybe a web cam focused on their hangout ...

GreenEDGE's cogs clicking into place for Tour debut
GreenEDGE's cogs clicking into place for Tour debut

Shutting down and clicking on (04:36)
While the American government shut down explored absurdity, we have had to make do with mere bemusement over Harvey Norman chief Gerry Harvey’s ...

Want a top Australian Public Service job? Get clicking on Facebook
Click 'Like' if you have what it takes to be a Canberra mandarin.

Shoes of Prey not the only e-tailer clicking onto bricks and mortar
Aussie internet merchants are realising that an old-school shop front is still vital for building brand and sales.

Shutting down and clicking on (04:36)
While the American government shut down explored absurdity, we have had to make do with mere bemusement over Harvey Norman chief Gerry Harvey’s ...

Shoes of Prey not the only e-tailer clicking onto bricks and mortar
Aussie internet merchants are realising that an old-school shop front is still vital for building brand and sales.

Shutting down and clicking on (04:36)
While the American government shut down explored absurdity, we have had to make do with mere bemusement over Harvey Norman chief Gerry Harvey’s ...

Resources last updated: 11/26/2015 7:43:00 PM