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

[Solved] Update link is not working in grid

2 Answers 97 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mahen
Top achievements
Rank 1
Mahen asked on 27 Jun 2009, 04:36 AM

Hi Everyone,

I have a grid in aspx page like below side..


<

 

div>

 

 

<telerik:RadScriptManager ID="RadScriptManager1" runat="server">

 

 

</telerik:RadScriptManager>

 

 

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">

 

 

<script type="text/javascript">

 

 

function RowDblClick(sender, eventArgs) {

 

sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());

}

 

</script>

 

 

<

 

style type="text/css">

 

 

.EditFormHeader td

 

{

 

background: #dadec8;

 

 

padding: 5px 0px;

 

}

 

</style>

 

 

 

</telerik:RadCodeBlock>

 

 

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">

 

 

<AjaxSettings>

 

 

<telerik:AjaxSetting AjaxControlID="radGridProduct">

 

 

<UpdatedControls>

 

 

<telerik:AjaxUpdatedControl ControlID="radGridProduct" />

 

 

<telerik:AjaxUpdatedControl ControlID="divMsg" />

 

 

</UpdatedControls>

 

 

</telerik:AjaxSetting>

 

 

</AjaxSettings>

 

 

</telerik:RadAjaxManager>

 

 

<div id="divMsg" runat="server" style="float: left;">

 

 

<asp:Label ID="Label1" runat="server" EnableViewState="False" Visible="false"></asp:Label>

 

 

</div>

 

 

<br />

 

 

<telerik:RadGrid ID="radGridProduct" runat="server" ShowStatusBar="true" OnNeedDataSource="radGridProduct_NeedDataSource"

 

 

GridLines="None" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"

 

 

OnPreRender="radGridProduct_PreRender" AllowAutomaticInserts="True"

 

 

AllowAutomaticUpdates="True" HorizontalAlign="NotSet"

 

 

OnItemInserted="radGridProduct_ItemInserted" OnItemUpdated="radGridProduct_ItemUpdated">

 

 

<MasterTableView DataKeyNames="ID" GridLines="None" Width="100%">

 

 

<Columns>

 

 

<telerik:GridEditCommandColumn UniqueName="EditCommandColumn">

 

 

</telerik:GridEditCommandColumn>

 

<%

-- <telerik:GridBoundColumn Visible="False" UniqueName="ID" HeaderText="ID"

 

DataField="ID" EditFormColumnIndex="1">

</telerik:GridBoundColumn>--

 

%>

 

<%

-- <telerik:GridBoundColumn Visible="False" UniqueName="City" HeaderText="City" DataField="City"

 

EditFormColumnIndex="1">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn Visible="False" UniqueName="Region" HeaderText="Region"

DataField="Region" EditFormColumnIndex="2">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn Visible="False" UniqueName="TitleOfCourtesy" HeaderText="TOC"

DataField="TitleOfCourtesy" EditFormColumnIndex="2">

<HeaderStyle Width="60px"></HeaderStyle>

</telerik:GridBoundColumn>--

 

%>

 

 

<telerik:GridBoundColumn UniqueName="ProductName" EditFormColumnIndex="0" HeaderText="ProductName"

 

 

DataField="ProductName">

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn UniqueName="SKU" EditFormColumnIndex="0" HeaderText="SKU"

 

 

DataField="SKU">

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn UniqueName="CreatedOn" EditFormColumnIndex="0"

 

 

HeaderText="Created Date" DataField="CreatedOn">

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn UniqueName="Active" EditFormColumnIndex="1" HeaderText="Active"

 

 

DataField="Active">

 

 

</telerik:GridBoundColumn>

 

 

</Columns>

 

 

<EditFormSettings ColumnNumber="4" CaptionFormatString="Edit details for product: {0}"

 

 

CaptionDataField="ProductName">

 

 

<FormTableItemStyle Wrap="False"></FormTableItemStyle>

 

 

<FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>

 

 

<FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" Width="100%" />

 

 

<FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module"

 

 

Height="110px" Width="100%" />

 

 

<FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>

 

 

<FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>

 

 

<EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit">

 

 

</EditColumn>

 

 

<FormTableButtonRowStyle HorizontalAlign="Left" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>

 

 

</EditFormSettings>

 

 

<ExpandCollapseColumn ButtonType="ImageButton" Visible="False" UniqueName="ExpandColumn">

 

 

<HeaderStyle Width="19px"></HeaderStyle>

 

 

</ExpandCollapseColumn>

 

 

</MasterTableView>

 

 

<ClientSettings>

 

 

<ClientEvents OnRowDblClick="RowDblClick" />

 

 

</ClientSettings>

 

 

</telerik:RadGrid>

 

</

 

div>

 

</

 

asp:Content>


This is code behind code -

 

using

 

System;

 

 

 

using

 

System.Collections.Generic;

 

 

 

using

 

System.Drawing;

 

 

 

 

 

 

 

 

 

using

 

System.Data;

 

 

 

using

 

Telerik.Web.UI;

 

 

 

public

 

partial class Users_ProductList : System.Web.UI.Page

 

 

 

 

{

 

static DataTable dtProduct = null;

 

 

protected void Page_Load(object sender, EventArgs e)

 

{

 

if (!IsPostBack)

 

{

 

IList<TProduct> productlist = CacheHelper.edPortalService.GetProducts();

 

 

if (productlist != null && productlist.Count > 0)

 

dtProduct =

CollectionHelper.ConvertTo<TProduct>(productlist);

 

}

 

else

 

 

 

 

 

this.radGridProduct.MasterTableView.Items[1].Edit = true;

 

 

}

 

protected void radGridProduct_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)

 

{

 

if (e.Exception != null)

 

{

e.KeepInEditMode =

true;

 

e.ExceptionHandled =

true;

 

DisplayMessage(

true, "Product " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"] + " cannot be updated. Reason: " + e.Exception.Message);

 

}

 

else

 

 

 

 

{

DisplayMessage(

false, "Product " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"] + " updated");

 

}

}

 

protected void radGridProduct_ItemInserted(object source, GridInsertedEventArgs e)

 

{

 

if (e.Exception != null)

 

{

e.ExceptionHandled =

true;

 

e.KeepInInsertMode =

true;

 

DisplayMessage(

true, "Product cannot be inserted. Reason: " + e.Exception.Message);

 

}

 

else

 

 

 

 

{

DisplayMessage(

false, "Product inserted");

 

}

}

 

private void DisplayMessage(bool isError, string text)

 

{

 

this.Label1.Font.Bold = true;

 

 

this.Label1.Visible = true;

 

 

if (isError)

 

{

 

this.Label1.ForeColor = Color.Red;

 

}

 

else

 

 

 

 

{

 

this.Label1.ForeColor = Color.Green;

 

}

 

this.Label1.Text = text;

 

}

 

protected void radGridProduct_NeedDataSource(object source, GridNeedDataSourceEventArgs e)

 

{

BindProductGrid();

}

 

private void BindProductGrid()

 

{

 

if (dtProduct != null && dtProduct.Rows.Count > 0)

 

radGridProduct.DataSource = dtProduct;

}

 

 

protected void radGridProduct_PreRender(object sender, System.EventArgs e)

 

{

 

// if (!this.IsPostBack)

 

 

 

 

 

// {

 

 

 

 

 

// this.radGridProduct.MasterTableView.Items[1].Edit = true;

 

 

 

 

 

// this.radGridProduct.MasterTableView.Rebind();

 

 

 

 

 

// }

 

 

 

 

}

}


can someone help me where i am lacking as my update link is not working and even while i click on cancel link for any row then it always open second row in grid for cancel .

Any suggestion.

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 29 Jun 2009, 08:43 AM
Hi Mahen,

From the above post I can see that you have enabled AllowAutomaticUpdates and AllowAutomaticInserts property to true for the Grid. The automatic data source operations only work when binding the grid to a declarative data source using the DataSourceID property of the grid. You must configure the data source so that it supports the automatic operations.  You may go through the following help article for getting more details about Automatic DataSource operations with RadGrid.
Automatic DataSource operations

Also in the above given code I can see that you are setting the second row of the Grid in edit mode using the following line of code.

 
this.radGridProduct.MasterTableView.Items[1].Edit = true

You may remove that line of code if you do not want to set the second row in edit mode always.

Shinu



0
Mahen
Top achievements
Rank 1
answered on 29 Jun 2009, 09:49 PM
Thank Shinu,

It is working now . :)
Tags
Grid
Asked by
Mahen
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Mahen
Top achievements
Rank 1
Share this question
or