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="RMAGridVie wRowOnData Bound" OnRowEditing="RMARowEditin g">
<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="ProductDescript ion" 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:Te xtBox>
<ajaxToolkit:FilteredTextB oxExtender ID="FilteredTextBoxExtende r1"
runat="server" FilterType="Custom, Numbers" TargetControlID="OverRideT B"
ValidChars=".">
</ajaxToolkit:FilteredText BoxExtende r>
</EditItemTemplate>
<ControlStyle CssClass="OverrideTemplate Field" />
</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:Te xtBox>
<ajaxToolkit:FilteredTextB oxExtender ID="FilteredTextBoxExtende r2" TargetControlID="casesTB"
FilterType="Numbers" runat="server">
</ajaxToolkit:FilteredText BoxExtende r>
</EditItemTemplate>
<ControlStyle CssClass="CasesAffectedTem plateField " />
</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:Te xtBox>
<ajaxToolkit:FilteredTextB oxExtender ID="FilteredTextBoxExtende r3" TargetControlID="piecesTB"
FilterType="Numbers" runat="server">
</ajaxToolkit:FilteredText BoxExtende r>
</EditItemTemplate>
<ControlStyle CssClass="CreditPCSTemplat eField" />
</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="IssueDataSou rce" DataTextField="DESCRIPTION " DataValueField="ISSUECODE"
OnSelectedIndexChanged="Is sueDDLSele ctedIndexC hanged">
</asp:DropDownList>
</EditItemTemplate>
<ControlStyle CssClass="IssueCodeTemplat eField" />
</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:T extBox>
</EditItemTemplate>
<ControlStyle CssClass="TotalTemplateFie ld" />
<FooterTemplate>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderImageUrl="~/Images/C LIP07.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:TextB ox>
<asp:Button ID="SaveButton" runat="server" Text="Save" OnClick="SaveNotes" />
<asp:Button ID="CancelButton" runat="server" Text="Cancel" />
</asp:Panel>
<ajaxToolkit:ModalPopupExt ender ID="NotesPopupExtender" runat="server" TargetControlID="NotesImag eButton"
PopupControlID="NotesPanel " BackgroundCssClass="modalB ackground" DropShadow="true"
CancelControlID="CancelBut ton">
</ajaxToolkit:ModalPopupEx tender>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UnitsPerCase" ItemStyle-CssClass="hidden col"
ReadOnly="True">
<HeaderStyle CssClass="hiddencol" />
<ItemStyle CssClass="hiddencol" />
<FooterStyle CssClass="hiddencol" />
</asp:BoundField>
<asp:BoundField DataField="PiecesCredited" ItemStyle-CssClass="hidden col"
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(I nvoiceLabe l.Text, i.ProductNumber)
};
RMAGridView.DataSource = rma;
RMAGridView.DataBind();
}
<asp:GridView ID="RMAGridView" runat="server" AutoGenerateColumns="False
ShowFooter="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDataBound="RMAGridVie
<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="ProductDescript
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
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
<ajaxToolkit:FilteredTextB
runat="server" FilterType="Custom, Numbers" TargetControlID="OverRideT
ValidChars=".">
</ajaxToolkit:FilteredText
</EditItemTemplate>
<ControlStyle CssClass="OverrideTemplate
</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
<ajaxToolkit:FilteredTextB
FilterType="Numbers" runat="server">
</ajaxToolkit:FilteredText
</EditItemTemplate>
<ControlStyle CssClass="CasesAffectedTem
</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
<ajaxToolkit:FilteredTextB
FilterType="Numbers" runat="server">
</ajaxToolkit:FilteredText
</EditItemTemplate>
<ControlStyle CssClass="CreditPCSTemplat
</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="IssueDataSou
OnSelectedIndexChanged="Is
</asp:DropDownList>
</EditItemTemplate>
<ControlStyle CssClass="IssueCodeTemplat
</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:T
</EditItemTemplate>
<ControlStyle CssClass="TotalTemplateFie
<FooterTemplate>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderImageUrl="~/Images/C
<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.
<asp:Panel ID="NotesPanel" runat="server" CssClass="modalPopup" Style="display: none">
<asp:TextBox ID="NotesTextBox" runat="server" Width="200px" Height="100px"></asp:TextB
<asp:Button ID="SaveButton" runat="server" Text="Save" OnClick="SaveNotes" />
<asp:Button ID="CancelButton" runat="server" Text="Cancel" />
</asp:Panel>
<ajaxToolkit:ModalPopupExt
PopupControlID="NotesPanel
CancelControlID="CancelBut
</ajaxToolkit:ModalPopupEx
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UnitsPerCase" ItemStyle-CssClass="hidden
ReadOnly="True">
<HeaderStyle CssClass="hiddencol" />
<ItemStyle CssClass="hiddencol" />
<FooterStyle CssClass="hiddencol" />
</asp:BoundField>
<asp:BoundField DataField="PiecesCredited"
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(
PiecesCredited = irma.XIRMACreditedPieces(I
};
RMAGridView.DataSource = rma;
RMAGridView.DataBind();
}
When you are using a LinqDataSource, you should set the DataSourceID to the ID for the LinqDataSource.
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?
ASKER
This gets called as a Button's OnClick event.
It sounds like you are going to need to set EditItemIndex also.
ASKER
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?
ASKER
This code gets called on the GridView OnRowCommand=
protected void GVCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName = "Delete")
{
int index = int.Parse(e.CommandArgumen t.ToString ());
RMAGridView.EditIndex = index;
UpdateRMAGridView(int.Pars e(RMAIDTex tBox.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.
protected void GVCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName = "Delete")
{
int index = int.Parse(e.CommandArgumen
RMAGridView.EditIndex = index;
UpdateRMAGridView(int.Pars
}
}
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.
ASKER
if (e.CommandName == "Edit")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all your effort.