Get GridView RowIndex


D

DavidC

How can I get the RowIndex of a GridView when clicking on a checkbox in
read-only mode? I have done it with the code below in a ListView but I
cannot figure out how to do the same in a GridView. Thanks.

Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
' Gets the CheckBox object that fired the event.
Dim chkBox As CheckBox = CType(sender, CheckBox)

' Gets the item that contains the CheckBox object.
Dim item As ListViewDataItem = CType(chkBox.Parent, ListViewDataItem)
Dim intTransID As Int32 =
Convert.ToInt32(lvIncExpTrans.DataKeys(item.DataItemIndex).Value)

If VendorClass.ChangeIncExpTransPick(intTransID, chkBox.Checked) =
False Then
Dim tb As TextBox = Page.Master.FindControl("txtMsg")
' Replace ListView1 with ID of your ListView Control
'tb.Text = "TransID sent = " &
lvIncExpTrans.DataKeys(item.DataItemIndex).Value
tb.Text = "The update to the checkbox failed."
End If
lvIncExpTrans.DataBind()
End Sub
 
Ad

Advertisements

M

Mr. Arnold

DavidC said:
How can I get the RowIndex of a GridView when clicking on a checkbox in
read-only mode? I have done it with the code below in a ListView but I
cannot figure out how to do the same in a GridView. Thanks.

It's back to the ItemDataBound.

If that event is wired in the asp:DataGrid, then the bound data for the
*one* row that was selected by the user will be selected, as it rolls through
the rows.

e.Item.ItemIndex is the row that the user selected.

I suspect that you're going to have to set AutoPostBack="true" so the event
will fier and comes to the codebehind file method.


protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover", "this.style.cursor='hand'");
e.Item.Attributes.Add("onclick", "javascript:__doPostBack" +
"('" + e.Item.UniqueID + "$ctl00' , 'Select$" + e.Item.ItemIndex
+ "')");
}
}


Or you may have to do something with client OnClick and javascript.
 
D

DavidC

I do have it set as autopostback in the GridView as shown below. Could I
just add the DataKey field to the Check_Clicked function and read it from
there? Thanks.

<asp:TemplateField HeaderText="Cleared" SortExpression="Reconciled"
ItemStyle-BackColor="White" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="ckReconciled" runat="server"
Checked='<%# Bind("Reconciled") %>' onclick="fillclearedamt(this);"
AutoPostBack="True" oncheckedchanged="Check_Clicked" />
</ItemTemplate>
<ItemStyle BackColor="White" HorizontalAlign="Center" />
</asp:TemplateField>
 
M

Mr. Arnold

DavidC said:
I do have it set as autopostback in the GridView as shown below. Could I
just add the DataKey field to the Check_Clicked function and read it from
there? Thanks.

<asp:TemplateField HeaderText="Cleared" SortExpression="Reconciled"
ItemStyle-BackColor="White" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="ckReconciled" runat="server"
Checked='<%# Bind("Reconciled") %>' onclick="fillclearedamt(this);"
AutoPostBack="True" oncheckedchanged="Check_Clicked" />
</ItemTemplate>
<ItemStyle BackColor="White" HorizontalAlign="Center" />
</asp:TemplateField>

That's autopostback on the Grid control itself needs to be set to true.
I don't see how an event of the checkbox has any knowledge of an event
belonging to the Grid. They are two different controls with different
event arguments.

You need the event that happened on the Grid so that you can get
e.item.itemindex. E.item.itemindex is a grid control event. You need to
address a grid event, which should be the Grid_ItemDataBound() event.

You go to the method and do a FindControl("ckReconcile"), and if it
comes back not null on the find, then you have the hit on the row the
control is bound in the grid and capture e.itemitemindex (the row),
which can be saved to a hidden field and used later.
 
D

DavidC

I will give that a try. Thanks.
--
David


Mr. Arnold said:
That's autopostback on the Grid control itself needs to be set to true.
I don't see how an event of the checkbox has any knowledge of an event
belonging to the Grid. They are two different controls with different
event arguments.

You need the event that happened on the Grid so that you can get
e.item.itemindex. E.item.itemindex is a grid control event. You need to
address a grid event, which should be the Grid_ItemDataBound() event.

You go to the method and do a FindControl("ckReconcile"), and if it
comes back not null on the find, then you have the hit on the row the
control is bound in the grid and capture e.itemitemindex (the row),
which can be saved to a hidden field and used later.

.
 
D

DavidC

Mr. Arnold said:
That's autopostback on the Grid control itself needs to be set to true.
I don't see how an event of the checkbox has any knowledge of an event
belonging to the Grid. They are two different controls with different
event arguments.

You need the event that happened on the Grid so that you can get
e.item.itemindex. E.item.itemindex is a grid control event. You need to
address a grid event, which should be the Grid_ItemDataBound() event.

You go to the method and do a FindControl("ckReconcile"), and if it
comes back not null on the find, then you have the hit on the row the
control is bound in the grid and capture e.itemitemindex (the row),
which can be saved to a hidden field and used later.

.

I am really struggling with this. In my original post I showed how I am
doing this in a ListView and it has been working perfectly. I am confused as
to why the GridView control cannot do the exact same thing. The ListView is
inside an UpdatePanel as is the GridView. The ListView checkbox has
AutoPostback="true" and it runs the event and does exactly what I want and it
gets the datakey value that I need to do my processing. Please let me know
what is different between the 2 controls that I can't do the exact same
thing. Thanks.

David
 
Ad

Advertisements

M

Mr. Arnold

DavidC said:
I am really struggling with this. In my original post I showed how I am
doing this in a ListView and it has been working perfectly. I am confused as
to why the GridView control cannot do the exact same thing. The ListView is
inside an UpdatePanel as is the GridView. The ListView checkbox has
AutoPostback="true" and it runs the event and does exactly what I want and it
gets the datakey value that I need to do my processing. Please let me know
what is different between the 2 controls that I can't do the exact same
thing. Thanks.

Maybe, it's due to the Listview has item events and the gridview has row
events that you can get away with it on a Listview what you did. I don't
know.

I forgot what I was doing when the expert sat down with me doing a
hidden field thing on an item within the ItemTemplete to get some other
information I needed, but I could not get to it by any other means using
a RadListView.

It's in the hidden field example that used the Eval in the link. I do
remember on an HTML <TD onClick> that it called a javascript function.
<TD>'s were present in the Itemtemplete of a RadListView.

<TD>
<asp:control />
</TD>


<http://weblogs.asp.net/aghausman/archive/2009/01/08/get-primary-key-on-row-command-gridview.aspx
 
Ad

Advertisements

D

DavidC

Mr. Arnold said:
Maybe, it's due to the Listview has item events and the gridview has row
events that you can get away with it on a Listview what you did. I don't
know.

I forgot what I was doing when the expert sat down with me doing a
hidden field thing on an item within the ItemTemplete to get some other
information I needed, but I could not get to it by any other means using
a RadListView.

It's in the hidden field example that used the Eval in the link. I do
remember on an HTML <TD onClick> that it called a javascript function.
<TD>'s were present in the Itemtemplete of a RadListView.

<TD>
<asp:control />
</TD>


<http://weblogs.asp.net/aghausman/archive/2009/01/08/get-primary-key-on-row-command-gridview.aspx





.
That link had some good examples. Thank you.

David
 

Top