This is a migrated thread and some comments may be shown as answers.

RadGrid Edits not saving

2 Answers 129 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Iron
Paul asked on 03 Aug 2016, 01:28 AM

So I have a grid with a "custom" editform.  The form opens fine, the cancel button works but when I click on "Update"... nothing. Setting a breakpoint on the event, it's not even firing.  I'd really appreciate any suggestion on what I'm doing wrong. 

My XAML is:

<telerik:RadGrid ID="pendingVehiclesRadGrid" runat="server" AutoGenerateColumns="False"
CellSpacing="0" GridLines="None" AllowPaging="True"
onneeddatasource="pendingVehiclesRadGrid_NeedDataSource"
OnUpdateCommand="pendingVehiclesRadGrid_UpdateCommand"
onitemcommand="pendingVehiclesRadGrid_ItemCommand" >
<MasterTableView DataKeyNames="status,charity,make,model,year,vin,date,id,salePrice,bid,note"
EditMode="EditForms" PageSize="20">
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridButtonColumn CommandName="History" Text="History" UniqueName="detailBtn" HeaderText="History" HeaderButtonType="PushButton" AutoPostBackOnFilter="True">
</telerik:GridButtonColumn>
<telerik:GridBoundColumn HeaderText="Charity" ItemStyle-Width="140px" FilterControlAltText="Filter column2 column"
UniqueName="column3" DataType="System.String" DataField="charity" ReadOnly="True">
<ItemStyle Width="140px"></ItemStyle>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="VIN" ItemStyle-Width="140px" FilterControlAltText="Filter column6 column"
UniqueName="column7" DataType="System.String" DataField="vin" >
<ItemStyle Width="140px"></ItemStyle>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Year" ItemStyle-Width="140px" FilterControlAltText="Filter column2 column"
UniqueName="column2" DataType="System.Int32" DataField="year">
<ItemStyle Width="140px"></ItemStyle>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Make" ItemStyle-Width="140px" FilterControlAltText="Filter column column"
UniqueName="column" DataType="System.String" DataField="make">
<ItemStyle Width="140px"></ItemStyle>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Model" ItemStyle-Width="140px" FilterControlAltText="Filter column1 column"
UniqueName="column1" DataType="System.String" DataField="model">
<ItemStyle Width="140px"></ItemStyle>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Last Update" ItemStyle-Width="140px" FilterControlAltText="Filter column1 column"
UniqueName="column5" DataType="System.DateTime" DataField="date" ReadOnly="True">
<ItemStyle Width="140px"></ItemStyle>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Min. Bid" ItemStyle-Width="140px" FilterControlAltText="Filter column1 column"
UniqueName="column8" DataType="System.Decimal" DataField="bid" DataFormatString="{0:C}" >
<ItemStyle Width="140px"></ItemStyle>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Sale Price" ItemStyle-Width="140px" FilterControlAltText="Filter column1 column"
UniqueName="column6" DataType="System.Decimal" DataField="salePrice" DataFormatString="{0:C}" Display="False">
<ItemStyle Width="140px"></ItemStyle>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Notes" ItemStyle-Width="140px" FilterControlAltText="Filter column1 column"
UniqueName="unsoldNotesColumn" DataType="System.String" DataField="note" Display="True">
<ItemStyle Width="140px"></ItemStyle>
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="Latest Status" ItemStyle-Width="240px">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "Status")%>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox RenderMode="Lightweight" runat="server" ID="pendingVehiclesRadGridupdateComboBox" DataTextField="Status"
DataValueField="ID" DataSourceID="LinqDataSource1" >
</telerik:RadComboBox>
</EditItemTemplate>
<ItemStyle Width="240px"></ItemStyle>
</telerik:GridTemplateColumn>
<telerik:GridEditCommandColumn FooterText="EditCommand footer" UniqueName="pendingVehiclesRadGridEditCommandColumn"
HeaderText="Edit" HeaderStyle-Width="100px" UpdateText="Update">
<HeaderStyle Width="100px"></HeaderStyle>
</telerik:GridEditCommandColumn>


</Columns>
<EditFormSettings EditColumn-DataType="System.String">
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
</MasterTableView>
<FilterMenu EnableImageSprites="False"></FilterMenu>
</telerik:RadGrid>

My codebehind looks like this:

private void LoadpendingVehicleRadGrid()
{

CAAData2DataContext db = new CAAData2DataContext();
var validStatuses = new[] { "Assigned", "Donor Called", "Dispatched", "Waiting",
"Title Rec'd", "Sending Title", "Cancelled", "Waiting"};
var ud = db.Updates
.Where(c => !c.Sold)
.GroupBy(c => c.Vehicle, x => x, (x, gr) => new
{
key = x,
list = gr.ToList().OrderByDescending(z => z.TimeStamp).First()
})
.Where(x => validStatuses.Contains(x.list.Status))
.Select(x => x.list).ToList();
var unSoldVehicles = (from u in ud
join v in db.Vehicles on
u.Vehicle equals v.ID
join c in db.Customers on
v.Charity equals c.ID
select new
{
status = u.Status,
make = v.Make,
model = v.Model,
Year = v.Year,
vin = v.VIN_,
charity = c.Name,
date = u.TimeStamp,
id = u.Vehicle,
salePrice = v.SalePrice,
bid = v.MinimumBid,
note = u.Note
}).OrderBy(z => z.charity).ToList();
pendingVehiclesRadGrid.DataSource = unSoldVehicles;



}

protected void pendingVehiclesRadGrid_UpdateCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName == "Update")
{
GridEditableItem item = (GridEditableItem)e.Item;
RadComboBox combo = (RadComboBox)item.FindControl("pendingVehiclesRadGridupdateComboBox");
String note = (item["unsoldNotesColumn"].Controls[0] as TextBox).Text;
var status = (string)item.GetDataKeyValue("status");
if (combo.SelectedItem.Text != status)
{
if (combo.SelectedItem.Text == "Vehicle Sold")
{
CAAData2DataContext dc = new CAAData2DataContext();
Update soldUpdate = new Update();
soldUpdate.Note = note;
soldUpdate.Status = combo.SelectedItem.Text.ToString();
soldUpdate.TimeStamp = DateTime.Now;
soldUpdate.Vehicle = (int)item.GetDataKeyValue("id");
soldUpdate.UpdatedBy = "Paul";
dc.Updates.InsertOnSubmit(soldUpdate);
dc.SubmitChanges();
var vehicleSold = from u in dc.Updates
where u.Vehicle == (Int32)item.GetDataKeyValue("id")
select u;
foreach (Update u in vehicleSold.ToList())
{
u.Sold = true;
}
dc.SubmitChanges();
var vehiclePrice = from v in dc.Vehicles
where v.ID == (Int32)item.GetDataKeyValue("id")
select v;
Vehicle soldVehicle = vehiclePrice.ToList().FirstOrDefault();
GridEditableItem editedItem = e.Item as GridEditableItem;
String price = (editedItem["Column6"].Controls[0] as TextBox).Text;
decimal salePrice = Convert.ToDecimal(price);
soldVehicle.SalePrice = salePrice;
dc.SubmitChanges();

}
else
{
CAAData2DataContext db = new CAAData2DataContext();
Update newUpdate = new Update();
newUpdate.Status = combo.SelectedItem.Text.ToString();
newUpdate.TimeStamp = DateTime.Now;
newUpdate.Vehicle = (int)item.GetDataKeyValue("id");
newUpdate.UpdatedBy = "Paul";
newUpdate.Note = note;
db.Updates.InsertOnSubmit(newUpdate);
db.SubmitChanges();

}


}

}
}

protected void pendingVehiclesRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item.IsInEditMode)
{
GridEditableItem editItem = (GridEditableItem)e.Item;
RadComboBox combo = (RadComboBox)editItem.FindControl("pendingVehiclesRadGridComboBox");
combo.DataBind();
combo.SelectedValue = DataBinder.Eval(editItem.DataItem, "ID").ToString();
}
}

Thanks in advance... I'm under a lot of pressure to get this working and beats me what's wrong...



2 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Tachev
Telerik team
answered on 05 Aug 2016, 12:17 PM
Hi Paul,

I have examined the provided code and it looks line you are using an auto generated EditForm for the Grid. Nevertheless, the editing should work as expected and the UpdateCommand should fire.

One thing I noticed is that you are checking the CommandName in the UpdateCommand handler. This is not necessary as the event will be raised only when an item is updated.

Also, you are setting the DataSource for the grid in the LoadpendingVehicleRadGrid() method. Where is this method called?

Note that you should set the DataSource for the grid only in the NeedDataSource handler. If you need to rebind the grid explicitly you can use the Rebind() method.


Regards,
Viktor Tachev
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Paul
Top achievements
Rank 1
Iron
answered on 05 Aug 2016, 08:31 PM
Thanks Victor!  It's absolutely bizaare... I knew that the grids (there are three on the page) once worked so I just started with a fresh page and pasted them in with the same code etc and they work fine.  No clue what's different but it's working... knock on wood.

Thank you!
Tags
Grid
Asked by
Paul
Top achievements
Rank 1
Iron
Answers by
Viktor Tachev
Telerik team
Paul
Top achievements
Rank 1
Iron
Share this question
or