?
Solved

Editable Gridview requires two clicks?

Posted on 2009-02-12
2
Medium Priority
?
262 Views
Last Modified: 2012-05-06
Have gridview with coded datasource (ie not using wizard) with "edit" column which only goes into edit mode if you click it twice. See:

Protected Sub dgPInv_RowEditing... below in vb section.
Invent4.aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="Label1" runat="server" BackColor="LightGray" Height="23px" Style="z-index: 103;
        left: 5px; top: 18px" Width="117px">Client Code/Name:</asp:Label>
    <asp:TextBox id="iClCode" style="Z-INDEX: 102; LEFT: 155px; TOP: 18px" runat="server"
		Height="18px" Width="162px"></asp:TextBox>&nbsp; <asp:Label ID="Label2" runat="server" BackColor="LightGray" Height="23px"
        Text="Units:" Width="53px"></asp:Label>
    &nbsp;<asp:TextBox ID="strUnits" runat="server" Height="18px" Width="42px"></asp:TextBox>    
    <asp:Button ID="Update" runat="server" Text="Save Selected" Width="103px" />&nbsp;
    <asp:Button ID="cmdOutput" runat="server" Text="Send to Excel/Word" Width="130px" />
    <asp:Button ID="cmdAll" runat="server" Text="Select All" Width="115px" />
    <asp:Button ID="cmdClear" runat="server" Text="Clear Selection" Width="115px" />
    <asp:GridView ID="dgPInv" runat="server" AllowPaging="True" AutoGenerateColumns="False" Width="988px" AllowSorting="True" DataKeyNames="InventNo">
            <Columns>
            <asp:CommandField ButtonType="Button" ShowSelectButton="True" ><HeaderStyle ForeColor="White" />
            </asp:CommandField>                
                <asp:CommandField ShowEditButton="True" >
                    <ItemStyle Width="65px" />
                </asp:CommandField>
                <asp:CheckBoxField DataField="iSelect" />
            <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" ReadOnly="True" />
            <asp:BoundField DataField="clInventNo" HeaderText="Client Inv No" SortExpression="clInventNo" ReadOnly="True" />
            <asp:BoundField DataField="clInventNo2" HeaderText="Cat No" SortExpression="clInventNo2" ReadOnly="True" />
            <asp:BoundField DataField="ArtistMaker" HeaderText="Artist" SortExpression="ArtistMaker" ReadOnly="True" />
            <asp:BoundField DataField="ShortDesc" HeaderText="Title" SortExpression="ShortDesc" ReadOnly="True" />
            <asp:BoundField DataField="Medium" HeaderText="Medium" SortExpression="Medium" ReadOnly="True" />
            <asp:BoundField DataField="iDate" HeaderText="Date" SortExpression="iDate" ReadOnly="True" />
            <asp:BoundField DataField="Height" HeaderText="Length" SortExpression="Height" ReadOnly="True" />
            <asp:BoundField DataField="Width" HeaderText="Width" SortExpression="Width" ReadOnly="True" />
            <asp:BoundField DataField="Depth" HeaderText="Height" SortExpression="Depth" ReadOnly="True" />
            <asp:BoundField DataField="AcquPriceEquiv" HeaderText="Value" SortExpression="AcquPriceEquiv" DataFormatString="{0:C}" HtmlEncode="False" ReadOnly="True" />
                <asp:ImageField DataImageUrlField="PicFile1" DataImageUrlFormatString="Images/A009/Thumbs/{0}" ReadOnly="True">
                </asp:ImageField>
            <asp:BoundField DataField="Pending" HeaderText="Pending" SortExpression="Pending"
                Visible="False" ReadOnly="True" />
        </Columns>
       <HeaderStyle BackColor="Navy" ForeColor="White" Font-Size="Small" />
        <PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
    </asp:GridView>
</asp:Content>
 
Invent4.aspx.vb:
Imports System.Data.SqlClient
Partial Class Invent4
    Inherits System.Web.UI.Page
    Private invconn As SqlConnection
    Private invComm As SqlCommand
    Private da As SqlDataAdapter
    Private ds As Data.DataSet
    Private dt As Data.DataTable
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Private") = False Then Response.Redirect("Default.aspx")
        If IsPostBack = False Then
            Me.iClCode.Text = Request.QueryString("txtClCode").ToString
            Using invconn = New SqlConnection(ConfigurationManager.ConnectionStrings("Connectstring").ConnectionString)
                invconn.Open()
                Using invComm = New SqlCommand("cClInv", invconn)
                    invComm.CommandType = Data.CommandType.StoredProcedure
                    invComm.Parameters.AddWithValue("@ClCode", Request.QueryString("txtClCode"))
                    Dim da As New SqlDataAdapter(invComm)
                    Dim ds As New Data.DataSet
                    Dim dt As New Data.DataTable
                    da.Fill(ds, "Inventory")
                    dt = ds.Tables("Inventory")
                    dgPInv.DataSource = ds.Tables("Inventory")
                    dgPInv.DataBind()
                    Me.strUnits.Text = dt.Rows.Count.ToString
                    Session("myds") = ds
                End Using
            End Using
        End If
    End Sub
 
    Protected Sub dgPInv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles dgPInv.PageIndexChanging
        dgPInv.PageIndex = e.NewPageIndex
        ds = CType(Session("myds"), Data.DataSet)
        dgPInv.DataSource = ds
        dgPInv.DataBind()
    End Sub
 
    Protected Sub dgPInv_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgPInv.SelectedIndexChanged
        Dim index As Integer = dgPInv.SelectedIndex
        Response.Redirect("InvPart.aspx?txtInvNo=" + Me.dgPInv.DataKeys(index).Value.ToString)
    End Sub
    Protected Sub dgPInv_RowDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgPInv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim img As Image = CType(e.Row.Cells(14).Controls(0), Image)
            Dim Pending As Integer = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Pending"))
            img.ImageUrl = img.ImageUrl.Replace("A009", Session("ClCode"))
            If Pending = 1 Then
                e.Row.BackColor = Drawing.Color.LightCoral
            ElseIf Pending = 2 Then
                e.Row.BackColor = Drawing.Color.CadetBlue
            ElseIf Pending = 3 Then
                e.Row.BackColor = Drawing.Color.Crimson
            End If
        End If
    End Sub
 
    Protected Sub dgPInv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles dgPInv.RowEditing
        dgPInv.EditIndex = e.NewEditIndex
    End Sub
 
    Protected Sub dgPInv_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles dgPInv.RowCancelingEdit
        dgPInv.EditIndex = -1
    End Sub
 
    Protected Sub dgPInv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles dgPInv.RowUpdating
        Dim c As CheckBox
        c = CType(dgPInv.Rows(e.RowIndex).Cells(2).Controls(0), CheckBox)
        ds = CType(Session("myds"), Data.DataSet)
        ds.Tables("Inventory").Rows(e.RowIndex).Item("iSelect") = c.Checked
        Session("myds") = ds
        dgPInv.EditIndex = -1
        dgPInv.DataSource = ds.Tables("Inventory")
        dgPInv.DataBind()
    End Sub
 
    Protected Sub Update_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Update.Click
        Using invconn = New SqlConnection(ConfigurationManager.ConnectionStrings("Connectstring").ConnectionString)
            invconn.Open()
            Using invComm = New SqlCommand("cClInv", invconn)
                invComm.CommandType = Data.CommandType.StoredProcedure
                invComm.Parameters.AddWithValue("@ClCode", Request.QueryString("txtClCode"))
                da = New SqlDataAdapter(invComm)
                Dim cmdB As SqlCommandBuilder = New SqlCommandBuilder(da)
                da.UpdateCommand = cmdB.GetUpdateCommand()
                Dim dsUpdated As Data.DataSet
                dsUpdated = CType(Session("myds"), Data.DataSet)
                da.Update(dsUpdated, "Inventory")
            End Using
        End Using
    End Sub
End Class

Open in new window

0
Comment
Question by:eharford
2 Comments
 
LVL 22

Accepted Solution

by:
prairiedog earned 1000 total points
ID: 23622963
Rebind your GridView after you set the editindex at line 102:
ds = CType(Session("myds"), Data.DataSet)
dgPInv.DataSource = ds
dgPInv.DataBind()
 
0
 

Author Closing Comment

by:eharford
ID: 31546087
Many thanks. Immediate response to my question (within minutes!). I am v grateful
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question