Click here to Skip to main content
13,044,705 members (63,477 online)
Rate this:
Please Sign up or sign in to vote.
Hello programmers, got a few twitching i need to resolve here.
I am creating an excel like updating wherein if the user click on a specific column/row it will change to textbox and when he click it another it will return to label but it is already been updated. By the way I am using usercontrol that will bi bind based on the row of a gridview
My codes are as follows:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="CourseUserControl.ascx.vb"
    Inherits="CourseUserControl" %>
        <asp:GridView ID="grdCourse" runat="server" Width="800px" HorizontalAlign="Left"
            AutoGenerateColumns="False" align="center" DataKeyNames="TrainingPlanID" Font-Names="verdana,arial,helvetica,sans-serif"
            Font-Size="8pt" BackColor="White" GridLines="Vertical" OnRowCommand="grdCourse_RowCommand">
                <asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="False" />
                <asp:TemplateField HeaderText="TrainingPlanID" SortExpression="TrainingPlanID" HeaderStyle-HorizontalAlign="Left">
                        <asp:Label ID="lblTrainingPlanID" runat="server" Text='<%# Eval("TrainingPlanID")%>'
                            Width="30px" Height="10px"> </asp:Label>                            
                <asp:TemplateField HeaderText="CourseName" SortExpression="CourseName" HeaderStyle-HorizontalAlign="Left">
                        <asp:Label ID="lblCoursename" runat="server" Text='<%# Bind("CourseName")%>' Width="300px"
                            Height="10px"> </asp:Label>
                        <asp:TextBox ID="txtCourseName" runat="server" Text='<%# Bind("CourseName") %>' Font-Size="XX-Small"
                            Font-Names="Verdana" Width="300px" Visible="false"></asp:TextBox>                            
                <asp:TemplateField HeaderText="Course Code" HeaderStyle-HorizontalAlign="Left">
                        <asp:Label ID="lblCourseCode" runat="server" Text='<%# Eval("CourseCode")%>' Height="10px"> </asp:Label>
                        <asp:TextBox ID="txtCourseCode" runat="server" Text='<%# Eval("CourseCode") %>' Font-Size="XX-Small"
                            Font-Names="Verdana" Width="300px" Visible="false"></asp:TextBox>
                <asp:TemplateField HeaderText="Duration" HeaderStyle-HorizontalAlign="Left">
                        <asp:Label ID="lblDuration" runat="server" Text='<%# Eval("Duration")%>' Height="10px"> </asp:Label>
                        <asp:TextBox ID="txtDuration" runat="server" Text='<%# Eval("Duration")%>' Font-Size="XX-Small"
                            Font-Names="Verdana" Visible="false"></asp:TextBox>
                <asp:TemplateField HeaderText="EditDetail" HeaderStyle-HorizontalAlign="Left">
                        <asp:ImageButton ID="EditDetail" Height="15px" ImageUrl="Images/edit2.png" runat="server"
                            CommandName="cmdlink" CommandArgument='<%# Eval("TrainingPlanID")%>' PostBackUrl='<%#"TrainingPlanDetails.aspx?TPID=" & Eval("TrainingPlanID")%>' />
                    <ItemStyle HorizontalAlign="Center" />
                <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Left">
                        <asp:ImageButton ID="ImageDeleteCourse" Height="15px" ImageUrl="Images/Delete.png"
                            runat="server" CommandName="cmdDelete" CommandArgument='<%# Eval("TrainingPlanID")%>'
                            OnClick="ImageDeleteCourse_Click" OnClientClick="return confirm('Are you sure to delete this Course?')" /><!-- onclick="ImageDeleteCourse_Click" -->
                    <ItemStyle HorizontalAlign="Center" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:TemplateField HeaderText="DisciplineID" HeaderStyle-HorizontalAlign="Left">
                        <asp:Label ID="DisciplineCourseID" runat="server" Text='<%# Eval("DisciplineID")%>'
                            Height="10px"> </asp:Label>
                <asp:TemplateField HeaderText="EditCourse" HeaderStyle-HorizontalAlign="Left">
                        <asp:ImageButton ID="ImageEdit" Height="15px" ImageUrl="Images/edit.png" runat="server"
                            CommandName="cmdlink" CommandArgument='<%# Eval("TrainingPlanID")%>' />
                    <ItemStyle HorizontalAlign="Center" />
                <asp:CommandField ShowEditButton="true" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />

this is the client side now on my usercontrol gridview updating here is my code
Protected Sub grdCourse_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdCourse.RowUpdating
        Dim _gridView As GridView = DirectCast(sender, GridView)
        Dim key As String = ""
        Dim value As String = ""
        ' The keys for the NewValues collection
        Dim _columnKeys As String() = New String() {"CourseName", "CourseCode", "Duration"}
        If e.RowIndex > -1 Then
            'Course Name column = 2
            'Course Code column = 3
            ' Loop though the columns
            For i As Integer = _firstEditCellIndex To 4
                ' Get the controls in the cell
                Dim _displayControl As Control = _gridView.Rows(e.RowIndex).Cells(i).Controls(1)
                Dim _editControl As Control = _gridView.Rows(e.RowIndex).Cells(i).Controls(3)
                ' Get the column key
                key = _columnKeys(i - _firstEditCellIndex)
                ' If a cell in edit mode get the value of the edit control
                If _editControl.Visible Then
                    If TypeOf _editControl Is TextBox Then
                        value = DirectCast(_editControl, TextBox).Text
                        'error since jan12 2012(the value cannot get DATA<value null>)-------------------------------------------------------
                        'try to debug
                        End If
                    ' Add the key/value pair to the NewValues collection
                    e.NewValues.Add(key, value)
                    ' else get the value of the display control
                    value = DirectCast(_displayControl, Label).Text.ToString()
                    ' Add the key/value pair to the NewValues collection
                    e.NewValues.Add(key, value)
                End If
            ' Clear the selected index to prevent 
            ' another update on the next postback
            _gridView.SelectedIndex = -1
        End If
    End Sub

as you see that is my error the value doesn't give what i typed in. It is null

any ideas?

My bad. My goal si this;
Switching from label to textbox
when I clicked on label it will change to textbox with the text of the label on the textbox
now when I change the content of the textbox that is the problem cause even if I have a text on my textbox
If TypeOf _editControl Is TextBox Then<br />
                        value = DirectCast(_editControl, TextBox).Text

the value is Null or "" which mean i cant get the text
By the way here is my code on rowcommand and rowdatabound so that you could understand what I am doing

Protected Sub grdCourse_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdCourse.RowCommand
        Dim _gridView As GridView = DirectCast(sender, GridView)
        Select Case e.CommandName
            Case ("SingleClick")
                ' Get the row index
                Dim _rowIndex As Integer = Integer.Parse(e.CommandArgument.ToString())
                ' Parse the event argument (added in RowDataBound) to get the selected column index
                Dim _columnIndex As Integer = Integer.Parse(Request.Form("__EVENTARGUMENT"))
                ' Set the Gridview selected index
                _gridView.SelectedIndex = _rowIndex
                ' Bind the Gridview
                ' Get the display control for the selected cell and make it invisible
                Dim _displayControl As Control = _gridView.Rows(_rowIndex).Cells(_columnIndex).Controls(1)
                _displayControl.Visible = False
                ' Get the edit control for the selected cell and make it visible
                Dim _editControl As Control = _gridView.Rows(_rowIndex).Cells(_columnIndex).Controls(3)
                _editControl.Visible = True
                ' Clear the attributes from the selected cell to remove the click event
                ' Set focus on the selected edit control
                ScriptManager.RegisterStartupScript(Me, [GetType](), "SetFocus", "document.getElementById('" + _editControl.ClientID + "').focus();", True)
                ' If the edit control is a dropdownlist set the
                ' SelectedValue to the value of the display control
                'If TypeOf _editControl Is DropDownList AndAlso TypeOf _displayControl Is Label Then
                '    DirectCast(_editControl, DropDownList).SelectedValue = DirectCast(_displayControl, Label).Text
                'End If
                ' If the edit control is a textbox then select the text
                If TypeOf _editControl Is TextBox Then
                    DirectCast(_editControl, TextBox).Attributes.Add("onfocus", "")
                End If
                Exit Select
        End Select

    End Sub

Protected Sub grdCourse_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdCourse.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim disID As Label = DirectCast(e.Row.FindControl("DisciplineCourseID"), Label)
            Dim myImage As ImageButton = DirectCast(e.Row.FindControl("ImageDeleteCourse"), ImageButton)
            If disID.Text = "" Then
                myImage.Visible = False
                myImage.Visible = True
                ' Get the LinkButton control in the first cell
                Dim _singleClickButton As LinkButton = DirectCast(e.Row.Cells(0).Controls(0), LinkButton)
                ' Get the javascript which is assigned to this LinkButton
                Dim _jsSingle As String = Page.ClientScript.GetPostBackClientHyperlink(_singleClickButton, "")
                ' If the page contains validator controls then call
                ' Page_ClientValidate before allowing a cell to be edited
                If Page.Validators.Count > 0 Then
                    _jsSingle = _jsSingle.Insert(11, "if(Page_ClientValidate())")
                End If
                ' Add events to each editable cell
                For columnIndex As Integer = _firstEditCellIndex To 5 - 1
                    ' Add the column index as the event argument parameter
                    Dim js As String = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString())
                    ' Add this javascript to the onclick Attribute of the cell
                    e.Row.Cells(columnIndex).Attributes("onclick") = js
                    ' Add a cursor style to the cells
                    e.Row.Cells(columnIndex).Attributes("style") += "cursor:pointer;cursor:hand;"
            End If
        End If
    End Sub
Posted 15-Jan-12 15:27pm
Updated 15-Jan-12 17:50pm
SAKryukov 15-Jan-12 22:26pm
Ideas on what? How to read this code dump? Who would be interested to do so if you don't even explain properly what's your problem? Value null -- so what? (You are saying like it's something bad :-). What value, why it should be no null? What's your goal?
janwel 16-Jan-12 1:05am
@SAKryukov Sir I already udpated my code

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can't compare a desktop application to a web application. Unless you use a 3rd party control you will not get Excel-like functionality from the built-in controls. You can, however, use the controls as they were designed. Put the row you want to edit into edit mode, then update the values when finised updating.

There are many examples available on DataGridView editing.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 15 Jan 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100