Solved

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

Posted on 2008-10-06
10
674 Views
Last Modified: 2013-12-17
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();

    }


0
Comment
Question by:jschnei555
  • 6
  • 4
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22652698
When you are using a LinqDataSource, you should set the DataSourceID to the ID for the LinqDataSource.
0
 

Author Comment

by:jschnei555
ID: 22653820
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).  
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22654049
How are you calling UpdateRMAGridView?
0
 

Author Comment

by:jschnei555
ID: 22654168
This gets called as a Button's OnClick event.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22654218
It sounds like you are going to need to set EditItemIndex also.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:jschnei555
ID: 22654262
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?
0
 

Author Comment

by:jschnei555
ID: 22654412
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.
0
 

Author Comment

by:jschnei555
ID: 22654432
if (e.CommandName == "Edit")
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 22654517
As an outsider, I am still a bit in the dark about your page life-cycle, and the processing steps that you took.  That would make it difficult to explain why it works for you now.  Understanding the page life-cycle is crucial to make a robust web page.
0
 

Author Closing Comment

by:jschnei555
ID: 31503520
Thanks for all your effort.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now