Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
XML
Hello,

I am attempting to determine which grid cell (i.e. row and column) is being edited but having some difficulty. The grid cells are comprised of textbox control in a template field. I'd tried following the code used in the Code Project article "Edit Individual GridView Cells in ASP.Net". Here's a sample of the code:


HTML:
<pre lang="HTML">
<asp:GridView ID="grdBusinessCodes" runat="server" style="z-index:101; font-family:'Arial'; font-size:9pt;"
CssClass="GridAppearanceStyle" HeaderStyle-CssClass="GridHeaderStyle" AutoGenerateColumns="False" RowStyle-CssClass="GridItemStyle">

    <RowStyle CssClass="GridItemStyle" />
    <Columns>                
        <asp:TemplateField HeaderText="Column0" Visible="False">
            <ItemTemplate>
                 <asp:TextBox ID="TextBox0" runat="server" Text='<%# Bind("Column0") %>' style="font-family:'Arial'; font-size:9pt;" 
                    TextMode="MultiLine" Enabled="True" ReadOnly="False" AutoPostBack="true" OnTextChanged="TextBox0_TextChanged"></asp:TextBox>                                 
            </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="Column1" Visible="False">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Column1", "{0:c}") %>' 
                    style="font-family:'Arial'; font-size:9pt;" TextMode="MultiLine" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:CommandField HeaderText="row_selector" ShowDeleteButton="False" 
            ShowEditButton="True" ShowInsertButton="False" ShowSelectButton="True" ButtonType="Link" Visible="False" />
    </Columns>
    <HeaderStyle CssClass="GridHeaderStyle" />

</asp:GridView>


Behind-code:

VB
Private Sub grdBusinessCodes_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdBusinessCodes.RowCommand

        m_RowIndex = Integer.Parse(e.CommandArgument.ToString())
        miColumnClicked = Integer.Parse(Request.Form("__EVENTARGUMENT").ToString())        

End Sub



Private Sub grdBusinessCodes_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdBusinessCodes.RowDataBound
        Dim itemType As DataControlRowType = e.Row.RowType
        Dim button As LinkButton
        Dim strPostBack As String
        Dim sJavaScriptArg As String


        If e.Row.RowType = DataControlRowType.DataRow Then
            'get the link button control in the first cell
            button = CType(e.Row.Cells(e.Row.Cells.Count - 1).Controls(0), LinkButton)    'The linkbutton is in the last cell **jf 8/1/14                                    
            strPostBack = Me.Page.ClientScript.GetPostBackClientHyperlink(button, "")

            For i = 0 To e.Row.Cells.Count - 1
                'add the column index as the event argument parameter
                sJavaScriptArg = strPostBack.Insert(strPostBack.Length - 2, i.ToString)
                

                If e.Row.Cells(i).HasControls And e.Row.Cells(i).Controls.Count > 1 Then 'TypeOf e.Row.Cells(i).Controls(1) Is TextBox Then
                    If i <> (e.Row.Cells.Count - 1) Then
                        CType(e.Row.Cells(i).Controls(1), TextBox).Attributes("onclick") = sJavaScriptArg
                        CType(e.Row.Cells(i).Controls(1), TextBox).Attributes.Add("onclick", "this.style.backgroundColor='Highlight';")
                        CType(e.Row.Cells(i).Controls(1), TextBox).Attributes.Add("onblur", "this.style.backgroundColor='White';")
                    End If
		End If
	    Next
	End If

End Sub

The cells can be edited individually but unable determine which row and column the cell belongs to.

Any help is appreciated.
Posted

1 solution

I corrected the setting the click event and style for the cells in the RowDataBound event and that seemed to do the trick:

VB
sJavaScriptArg = strPostBack.Insert(strPostBack.Length - 2, i.ToString)
e.Row.Cells(i).Attributes("onclick") = sJavaScriptArg
' Add a cursor style to the cells
e.Row.Cells(i).Attributes("style") += "cursor:pointer;cursor:hand;"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900