Link to home
Start Free TrialLog in
Avatar of jschnei555
jschnei555

asked on

How do I edit a Grid View with a programatic data source?

I have a grid view with Template Fields, which specify both the ItemTemplate and EditItemTemplate.  I then create and bind to a LINQ datasource programatically.  I have added the Edit command button, but when I press it, my GridViewRow does not change to the EditItemTemplate (nor does it show Update or Cancel).   Here is the relevant code:


<asp:GridView ID="RMAGridView" runat="server" AutoGenerateColumns="False" CssClass="RMAGridView"
                        ShowFooter="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDataBound="RMAGridViewRowOnDataBound" OnRowEditing="RMARowEditing">
                        <Columns>
                            <asp:BoundField DataField="RMALineID" HeaderText="Line" ReadOnly="True">
                                <ItemStyle CssClass="InvoiceLineNum" />
                            </asp:BoundField>
                            <asp:BoundField DataField="ProductNumber" HeaderText="Item #" ReadOnly="True">
                                <ItemStyle CssClass="InvoiceItemNum" />
                            </asp:BoundField>
                            <asp:BoundField DataField="ProductDescription" HeaderText="Description"
                                ReadOnly="True">
                                <ItemStyle CssClass="InvoiceItemDesc" />
                            </asp:BoundField>
                            <asp:BoundField DataField="CasePack" HeaderText="CS/PK" ReadOnly="True">
                           
                                <ItemStyle CssClass="InvoiceCSPK" />
                            </asp:BoundField>
                            <asp:BoundField DataField="InvoicePrice" DataFormatString="{0:c}" HeaderText="Price"
                                HtmlEncode="False" ReadOnly="True">
                                <ItemStyle CssClass="InvoicePrice" />
                            </asp:BoundField>
                            <asp:BoundField DataField="InvoiceQuantity" DataFormatString="{0:N0}" HeaderText="Qty"
                                HtmlEncode="False" ReadOnly="True">
                                <ItemStyle CssClass="InvoiceQuantity" />
                            </asp:BoundField>
                            <asp:TemplateField HeaderText="Adj. Price">
                                <ItemTemplate>
                                    <asp:Label ID="AdjustedPriceLabel" runat="server"  ><%# Eval("OverridePrice", "{0:c}") %></asp:Label>
                                 
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="OverRideTB" runat="server" AutoPostBack="true"
                                        CssClass="OverRideTB" OnTextChanged="updateTotal"></asp:TextBox>
                                    <ajaxToolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender1"
                                        runat="server" FilterType="Custom, Numbers" TargetControlID="OverRideTB"
                                        ValidChars=".">
                                    </ajaxToolkit:FilteredTextBoxExtender>
                                </EditItemTemplate>
                                <ControlStyle CssClass="OverrideTemplateField" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Affected Cases">
                                <ItemTemplate>
                                    <asp:Label ID="AffectedCasesLabel" runat="server"  ><%#Eval("CasesAffected") %></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                <asp:TextBox ID="casesTB" runat="server" CssClass="casesTB" AutoPostBack="true" OnTextChanged="updateTotal"></asp:TextBox>
                                    <ajaxToolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender2" TargetControlID="casesTB"
                                        FilterType="Numbers" runat="server">
                                    </ajaxToolkit:FilteredTextBoxExtender>
                                </EditItemTemplate>
                                <ControlStyle CssClass="CasesAffectedTemplateField" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Pieces to Credit">
                                <ItemTemplate>
                                   <asp:Label ID="PiecesLabel" runat="server"  ><%#Eval("PieceQuantity") %></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                 <asp:TextBox ID="piecesTB" runat="server" CssClass="piecesTB" AutoPostBack="true"
                                        OnTextChanged="updateTotal"></asp:TextBox>
                                    <ajaxToolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender3" TargetControlID="piecesTB"
                                        FilterType="Numbers" runat="server">
                                    </ajaxToolkit:FilteredTextBoxExtender>
                                </EditItemTemplate>
                                <ControlStyle CssClass="CreditPCSTemplateField" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Issue Code">
                                <ItemTemplate>
                                     <asp:Label ID="IssueLabel" runat="server"  ><%#Eval("Description") %></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                <asp:DropDownList ID="IssueDDL" AutoPostBack="true" runat="server" CssClass="IssueDDL"
                                        DataSourceID="IssueDataSource" DataTextField="DESCRIPTION" DataValueField="ISSUECODE"
                                        OnSelectedIndexChanged="IssueDDLSelectedIndexChanged">
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <ControlStyle CssClass="IssueCodeTemplateField" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Total">
                                <ItemTemplate>
                                    <asp:Label ID="TotalLabel" runat="server"><%# Eval("LineTotal", "{0:c}") %></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                <asp:TextBox ID="TotalTB" runat="server" CssClass="TotalTB"></asp:TextBox>
                                </EditItemTemplate>
                                <ControlStyle CssClass="TotalTemplateField" />
                                <FooterTemplate>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderImageUrl="~/Images/CLIP07.ICO" HeaderText="Note">
                                <ItemTemplate>
                                    <!--<img id="image" runat="server" alt="no note" src="Images/CLIP08.ICO" />
                            <img id="imageYellow" runat="server" alt="note" src="Images/CLIP07.ICO" style="display: none" />
                            <input id="hidden" runat="server" enableviewstate="true" name="hidden" type="hidden" /> -->
                                    <asp:ImageButton ID="NotesImageButton" runat="server" Height="25px" Width="25px"
                                        ImageUrl="~/Images/CLIP08.ICO" />
                                    <asp:Panel ID="NotesPanel" runat="server" CssClass="modalPopup" Style="display: none">
                                        <asp:TextBox ID="NotesTextBox" runat="server" Width="200px" Height="100px"></asp:TextBox>
                                        <asp:Button ID="SaveButton" runat="server" Text="Save" OnClick="SaveNotes" />
                                        <asp:Button ID="CancelButton" runat="server" Text="Cancel" />
                                    </asp:Panel>
                                    <ajaxToolkit:ModalPopupExtender ID="NotesPopupExtender" runat="server" TargetControlID="NotesImageButton"
                                        PopupControlID="NotesPanel" BackgroundCssClass="modalBackground" DropShadow="true"
                                        CancelControlID="CancelButton">
                                    </ajaxToolkit:ModalPopupExtender>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="UnitsPerCase" ItemStyle-CssClass="hiddencol"
                                ReadOnly="True">
                                <HeaderStyle CssClass="hiddencol" />
                                <ItemStyle CssClass="hiddencol" />
                                <FooterStyle CssClass="hiddencol" />
                            </asp:BoundField>
                            <asp:BoundField DataField="PiecesCredited" ItemStyle-CssClass="hiddencol"
                                ReadOnly="True">
                                <HeaderStyle CssClass="hiddencol" />
                                <ItemStyle CssClass="hiddencol" />
                                <FooterStyle CssClass="hiddencol" />
                            </asp:BoundField>
                            <asp:CommandField ShowEditButton="True" />
                        </Columns>
                        <FooterStyle BackColor="#003366" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="White" ForeColor="#333333" Font-Size="Small" />
                        <EditRowStyle BackColor="#999999" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#003366" Font-Bold="True" ForeColor="White" Font-Size="Small" />
                        <AlternatingRowStyle BackColor="#D9E1F0" ForeColor="#284775" />
                    </asp:GridView>    

    private void UpdateRMAGridView(int RMAID)
    {
        GLWDataContext glw = GLWDC;
        IRMADataContext irma = IRMADC;
        var rma = from i in irma.RMADetails
                  where i.RMAID == RMAID
                  select new
                  {
                      i.BinID,
                      i.Buyer,
                      i.CasePack,
                      i.InvoiceLineNum,
                      i.InvoiceLineSeqNum,
                      i.InvoicePrice,
                      i.PieceQuantity,
                      i.InvoiceQuantity,
                      i.PCAT,
                      i.ProductDescription,
                      i.ProductNumber,
                      i.RMAID,
                      i.RMALineID,
                      i.VoidStatus,
                      i.ZoneID,
                      i.OverridePrice,
                      i.CasesAffected,
                      i.IssueCode,
                      i.Issue.Description,
                      i.LineTotal,
                      UnitsPerCase = irma.SOPX_fn_UnitsPerCase(i.CasePack),
                      PiecesCredited = irma.XIRMACreditedPieces(InvoiceLabel.Text, i.ProductNumber)

                  };


        RMAGridView.DataSource = rma;
        RMAGridView.DataBind();

    }


Avatar of Bob Learned
Bob Learned
Flag of United States of America image

When you are using a LinqDataSource, you should set the DataSourceID to the ID for the LinqDataSource.
Avatar of jschnei555
jschnei555

ASKER

My data source doesn't have an ID because it is programatically generated.  I can't make my LINQ code a LinqDataSource object becuase the query is a bit too tricky (subqueries and all).  
How are you calling UpdateRMAGridView?
This gets called as a Button's OnClick event.
It sounds like you are going to need to set EditItemIndex also.
OK.  I just noticed that the Edit command button does work...although it doesn't work on the first try.  It works the second time clicked.  It executes the same code twice (doing a roundtrip), but only renders the EditItemTemplate the second time.  Any ideas?
This code gets called on the GridView OnRowCommand=

    protected void GVCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName = "Delete")
        {
            int index = int.Parse(e.CommandArgument.ToString());
            RMAGridView.EditIndex = index;
            UpdateRMAGridView(int.Parse(RMAIDTextBox.Text));
        }
       
    }

The funny things are 1. It updates the GridView the second time it gets called and 2. I fixed it by calling UpdateRMAGridView() which rebinds the data.  The problem is solved, but I will still award points if someone can explain why this works this way.
if (e.CommandName == "Edit")
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all your effort.