Get the Row Number at gridview by Client ID

Get the row number of GRIDVIEW by client ID, and manipulate its content

Hi , I just get a task to work on grid view event , its something looks like this.

Dealer Amount +/- Final Amount
Dealer a 21659141,50 // //
Dealer a 85311854,20 // //
Dealer b 21659141,50 // //
Dealer b 85311854,20 // //

As you see in the grid, the amount can be increased through a column, after fill in the Change, it show the result on its right side. The difficult part is to get the ROW NUMBER.

Im using TextBox even ONTEXTCHANGED, and make the textbox AUTOPOSTBACK on. By logic, here are the steps:

  1. Declare the sender is a textbox. The value inside it we call it AMOUNT_CHANGE
  2. Get the row number of the sender.
  3. Declare the target textbox is by FIND CONTROL of the gridview row of ROWNUMBER. We call it TARGET_TEXTBOX
  4. Get the initial amount, by taking from GRIDVIEW row of ROWNUMBER, call it INITIAL VALUE
  5. Set the TARGET_TEXTBOX value = INITIAL_VALUE + AMOUNT_CHANGE

That should be it. But I find it very difficult to take the row number. Im thinking of using javascript to get the array ID of the textbox. Oh ya, note that the gridview, I couldn’t use the event like rowchanging, or something, since this one is not edit event and all that. This one is displayed only. So the event is textbox event.

And after some exploration, I find it actually easy. There is a properties of that textbox. Called CLIENTID. And there is for the gridview row too. Well basically all ASP control are html based.

The ID of TEXTBOX of AMOUNT_CHANGE looks like this,

ctl00_cphContent_subsidi1_gvSubsidi_ctl05_txtChange

that CTL05 is when I trigger the event from row 4 (3 if from 0). So what I need to do is just create function to retrieve the 05 from that text and minus 2, there we have the ROWNUMBER. Like this :

Private Function GetRowNumber(ByVal ctrlId As String) As Integer

Dim num As Integer = ctrlId.LastIndexOf(“ctl”)

Dim index As String = ctrlId.Substring(num + 3, 2)

If IsNumeric(index) Then

Return CInt(index) – 2

End If

Return -1

End Function

There we goes. After we got the ROWNUMBER, we can set it. I will let you to take a look at the code yourself. Hope it helps,

Regards,

Satu

Code

‘Created By : Satu

‘Created on : 5 nov 2009

‘Purpose : the code behind SubsidiKondisi Usercontrol

‘—————————————————-

Partial Class UserControls_SubsidiKondisi

Inherits System.Web.UI.UserControl

‘Private word As String = “hello”

‘to set the last amount after increment or decrement

Protected Sub SetAmountAkhir(ByVal sender As Object, ByVal e As EventArgs)

Try

Dim txt As TextBox = sender

Dim rowNumber As Integer = GetRowNumber(txt.ClientID)

‘txtAmAkhir is the target textbox

Dim txtAmAkhir As TextBox = gvSubsidi.Rows(rowNumber).FindControl(“txtAmountAkhir”)

Dim valid As Boolean = False

If IsNumeric(txt.Text.Trim) Then valid = True

If valid Then

Dim value As Decimal = CDec(txt.Text.Trim)

Dim amount As Decimal = CDec(gvSubsidi.Rows(rowNumber).Cells(1).Text)

txtAmAkhir.Text = value + amount

End If

Catch ex As Exception

End Try

End Sub

‘getting row number from client ID

Private Function GetRowNumber(ByVal ctrlId As String) As Integer

Dim num As Integer = ctrlId.LastIndexOf(“ctl”)

Dim index As String = ctrlId.Substring(num + 3, 2)

If IsNumeric(index) Then

Return CInt(index) – 2

End If

Return -1

End Function

End Class

Ascx

<%@ Control Language=”VB” AutoEventWireup=”false” CodeFile=”SubsidiKondisi.ascx.vb” Inherits=”UserControls_SubsidiKondisi” %>

<asp:GridView id=”gvSubsidi” runat=”server”

AutoGenerateColumns=”false”>

<Columns>

<asp:BoundField DataField=”MSDEALER_NAME” HeaderText=”Dealer” />

<asp:BoundField DataField=”Amount” HeaderText=”Amount” ItemStyle-Width=”100″ />

<asp:TemplateField>

<ItemTemplate><div style=”width:50px”></div></ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText=”+/-” ItemStyle-Width=”100″ >

<ItemTemplate>

<%–<asp:TextBox ID=”txtChange” runat=”server”

OnTextChanged=”SetAmountAkhir” AutoPostBack=”true” >

</asp:TextBox>  –%>

<asp:textbox ID=”txtChange” runat=”server”

OnTextChanged=”SetAmountAkhir” AutoPostBack=”true” >

</asp:textbox >

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText=”Amount Akhir” ItemStyle-Width=”100″ >

<ItemTemplate>

<asp:textbox ID=”txtAmountAkhir” runat=”server” ReadOnly=”true”>

</asp:textbox >

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

<%–<asp:Button OnClientClick=”Calculate(this)” runat=”server” Text=”test js” />

–%>


Posted

in

by

Comments

One response to “Get the Row Number at gridview by Client ID”

Leave a Reply

Your email address will not be published. Required fields are marked *