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

problems with ItemDataBound

16 Answers 540 Views
Grid
This is a migrated thread and some comments may be shown as answers.
newbie
Top achievements
Rank 1
newbie asked on 28 Jul 2008, 04:03 PM
I always render my grid in edit mode.
I am calculating the total to be displayed in the footer in the ItemdataBound event.

My code looks like this:

protected

void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)

{

if
(e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)

{

GridEditableItem editItem = e.Item as GridEditableItem;

RadNumericTextBox

txt01 = (editItem.FindControl("txt01") as RadNumericTextBox);

sum += (

string.IsNullOrEmpty(txt01.Text) ? 0 : double.Parse(txt01.Text));

}

}

The problem is after an update the total in the footer gets doubled.

I am not sure if my understanding is right but I suspect that my ItemDataBound event is called and then it's called again after the NeedDataSource event is fired. And since I am storing the total in a global variable sum it gets doubled.

What can i do to avoid it?

16 Answers, 1 is accepted

Sort by
0
newbie
Top achievements
Rank 1
answered on 28 Jul 2008, 04:44 PM
I think the problem was because i was invoking a Rebind() after my update explicitly.
I guess I don't need a Rebind() with the inserts/updates at all.
0
Princy
Top achievements
Rank 2
answered on 29 Jul 2008, 05:52 AM
Hi Newebie,

Refer the following help article which lists the Grid commands that make an implicit call to the Rebind() method of the RadGrid.
Commands that invoke Rebind() implicitly

Thanks
Princy.
0
newbie
Top achievements
Rank 1
answered on 30 Jul 2008, 12:26 AM
sorry ...there's something in my code that I don't understand.
I would be grateful if you can tell me what's wrong.

I need to do a Rebind in the PreRender event where I am setting all the rows in edit mode.

I calculate the total in the footers in my ItemDatbound event.

My ItemDataBound event is called after I do an insert and then it's called again after the rebind(). And since I am storing the total in a global variable sum it gets doubled.

I am unable to figure out how to solve this.
Please help
0
Sebastian
Telerik team
answered on 30 Jul 2008, 06:38 AM
Hello newbie,

I suggest you remove the Rebind() call along with the relevant code logic to put all items in edit mode from the PreRender event and switch the records in edit mode without additional rebind using the EditIndexes collection of the grid.

Review the following topic from the documentation for further reference:

http://www.telerik.com/help/aspnet-ajax/grdputallitemsineditmodenoadditionalrebind.html

Best,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
newbie
Top achievements
Rank 1
answered on 30 Jul 2008, 07:45 PM
I tried doing it using Editindexes but that doesn't work for me as well.

My grid has some bound columns. I create a pivot and change some columns headers in my need datasource event.
Then i put my rows in edit mode and do a rebind.

If i don't do this then after an insert the grid doesn't work as desired.

In my ItemdataBound event i change the headers for a few bound columns.

below is my code to give you an idea:

public partial class MyTimesheets_TimeSheetDetail : EditPageBase
{

private int timesheetID = -1;
private Timesheet timesheet;
private double sum01 = 0;
private double sum02 = 0;

protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{

TimesheetID =

Convert.ToInt32(Request.QueryString["timesheetID"]);

}

}

protected override void InitializeForm()

{

base.InitializeForm();

 

/// <summary>

/// Get the data and pivot the rows to columns before binding to the grid

/// </summary>

protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)

{

RadGrid1.DataSource = Timesheet.PivotTimesheetDetailsDataTable;

}

protected void RadGrid1_PreRender(object sender, EventArgs e)

{

SetGridEditMode();

}

/// <summary>

/// Sets all rows and columns into edit mode

/// </summary>

protected void SetGridEditMode()

{

foreach (GridDataItem item in RadGrid1.MasterTableView.Items)

{

item.Edit =

true;

}

RadGrid1.Rebind();

}

 

/// <summary>

/// Batch updates are handled here to commiting all changes to the database. And for adding a new row.

/// </summary>

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)

{

if (e.CommandName == RadGrid.PerformInsertCommandName)

{

SaveTimesheetDetails(

true);

}

}

 

/// <summary>

/// Saves the timesheetdetails and also adds the timesheet if it does not exist yet.

/// </summary>

protected bool SaveTimesheetDetails(bool isNewLine)

{

bool isSuccess = false;

//Updates the timesheet

UpdateTimesheetDetail();

if (isNewLine)

{

//add a dummy row and bind to the grid

Timesheet.UpdateTimesheetDetail(

new TimesheetDetail(TimesheetID, 0, 0, 0, 0, 0, "Regular"));

}

isSuccess =

true;

Timesheet.LoadTimesheetDetail();

return isSuccess;

protected void UpdateTimesheetDetail()
{

foreach

(GridEditableItem editedItem in RadGrid1.EditItems)

{

Update();
editedItem.Edit =

true;

}

}

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)

{

if (e.Item is GridHeaderItem)

{

string sequence = "";

string dateheaderdisplaytext = "";

GridHeaderItem header = (GridHeaderItem)e.Item;

foreach (DataRow dr in Timesheet.DateRangeDataTable.Rows)

{

sequence = dr[

"sequence"].ToString();

dateheaderdisplaytext = dr[

"datedisplay"].ToString();

if (dateheaderdisplaytext == "hide")

{

//hide the columns that are not in this reporting date range.

RadGrid1.MasterTableView.GetColumn(sequence).Visible =

false;

}

else

{

header[sequence].Text = dateheaderdisplaytext;

}

}

}

 

if (e.Item is GridFooterItem)

{

GridFooterItem footer = (GridFooterItem)e.Item;

footer[

"Assignment"].Controls.Add(new LiteralControl("<span style='color: Black; font-weight: bold;'>Total:</span> "));

(footer[

"01"].FindControl("total01") as RadNumericTextBox).Value = Double.Parse(sum01.ToString());

(footer[

"02"].FindControl("total02") as RadNumericTextBox).Value = Double.Parse(sum02.ToString());

}

 

if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)

{

GridEditableItem editItem = e.Item as GridEditableItem;

RadComboBox ddlAssignment = (RadComboBox)(editItem.FindControl("ddlAssignment"));

ddlAssignment.Text =

DataBinder.Eval(e.Item.DataItem, "AssignmentDisplayValue").ToString();

ddlAssignment.SelectedValue =

DataBinder.Eval(e.Item.DataItem, "AssignmentID").ToString();

RadNumericTextBox txt01 = (editItem.FindControl("txt01") as RadNumericTextBox);

sum01 += (

string.IsNullOrEmpty(txt01.Text) ? 0 : double.Parse(txt01.Text));

RadNumericTextBox txt02 = (editItem.FindControl("txt02") as RadNumericTextBox);

sum02 += (

string.IsNullOrEmpty(txt02.Text) ? 0 : double.Parse(txt02.Text));

}

}

 

 

protected void ddlAssignment_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
TimesheetDetail.LoadAssignments((RadComboBox)sender);
}

protected void lnkSave_Click(object sender, ImageClickEventArgs e)
{

bool result = SaveTimesheetDetails(false);

}

protected int TimesheetID
{

get

{

if (timesheetID == -1)

{

timesheetID =

Convert.ToInt32(Session["TimesheetID"]);

}

return timesheetID;

}

set

{

Session[

"TimesheetID"] = value;

timesheetID =

value;

}

public Timesheet Timesheet
{

get

{

if (timesheet == null)

{

if (TimesheetID == 0)

{

timesheet =

new Timesheet();

}

else

{

timesheet =

new Timesheet(TimesheetID);

}

}

return timesheet;

}

set { timesheet = value; }

}

}


Can you help?
0
Sebastian
Telerik team
answered on 31 Jul 2008, 06:38 AM
Hello newbie,

Have you tried removing the SetGridInEditMode() method and the call to it from within the PreRender handler and test the solution suggested in my last post? If this does not help, I suggest you assemble a stripped working version of your project, demonstrating the issue, and send it enclosed to a support ticket. I will examine your complete code logic in detail and will get back to you with my findings.

Kind regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
newbie
Top achievements
Rank 1
answered on 31 Jul 2008, 04:42 PM
I tried removing the SetGrideditMode and setting the grid in EditMode using the EditIndexes as described by you earlier, but that didn't help.

The doubling issue gets resolved but the problem that I have with this approach is... that I change the  header text in the ItemDataBound based on certain conditions.
I have a save button outside the grid that saves the entire grid data.
The problem I have here is if I do an add new(from the grid) eberything is fine but if i click the save button or a dropdown outside the grid that does a postback my column header goes back to what I had specified in the grid declaration in the aspx page.

I assume this is because the grid needdatasource and ItemdataBound events are fired on an insert from the grid but not the save from outside the grid.

Unfortunately the database and the project is too complex to submit as a stripped down version. Would it be helpful if I just send you the aspx page?
0
Sebastian
Telerik team
answered on 01 Aug 2008, 06:51 AM
Hello newbie,

Does moving the column header text change from the ItemDataBound handler of the grid inside the ItemCreated hander produces the desired result. Just make sure that you do not set the HeaderText property of the corresponding column declaratively and define the header text within the ItemCreated event. Note that this event will be raised after each postback operation.

Further information about the RadGrid event lifecycle you can gather from these topics:

http://www.telerik.com/help/aspnet-ajax/grdeventsequence.html
http://www.telerik.com/help/aspnet-ajax/grddistinguishdifferencesbetweenitemcreateditemdatabound.html

Regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
newbie
Top achievements
Rank 1
answered on 01 Aug 2008, 06:04 PM
Thanks.

Somehow I tried the removing the SetGridEditMode and trying with EditIndexes as you mentioned in the earlier post again and it seems to be working fine now.
Maybe I missed something earlier, I am not sure about that.

I have a different problem with that now.
I am setting the edit mode in Page_load as mentioned in the post.
On the sort command my grid goes into view mode. Any idea why that might be happening?
0
Sebastian
Telerik team
answered on 04 Aug 2008, 06:59 AM
Hello newbie,

If the code which adds the edit indices is wrapped inside !Page.IsPostBack conditional block, the grid rows will be switched in regular mode when you perform a command which rebinds the grid implicitly or invoke the Rebind() method explicitly. To have the grid rows in edit mode at all times, remove the conditional check from the PageLoad handler.
 
Kind regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
newbie
Top achievements
Rank 1
answered on 05 Aug 2008, 04:03 PM
I don't have the code that sets the grid in edit mode inside the conditional check for (IsPostback)

I even tried putting that code in the sort command event handler but it doesn't work.
0
Sebastian
Telerik team
answered on 06 Aug 2008, 07:13 AM
Hello newbie,

Can you debug your code to see whether the EditIndexes are added each time on PageLoad? This should be sufficient to have your items in edit mode after each submit to the server.

Regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
newbie
Top achievements
Rank 1
answered on 06 Aug 2008, 05:01 PM
The code that sets the grid in edit mode is called on each page load.

I am unable to understand why the sort command renders the grid in edit mode for me.
0
Sebastian
Telerik team
answered on 07 Aug 2008, 08:45 AM
Hello newbie,

I thought that your intention was to have the grid in edit mode once again when sorting operation is completed. Am I missing something? Please clarify in order to provide further directions.

Regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
newbie
Top achievements
Rank 1
answered on 07 Aug 2008, 03:18 PM
sorry, it was typing error.
 I meant it doesn't render it in edit mode for me.

I want the grid in edit mode at all times.
0
Sebastian
Telerik team
answered on 08 Aug 2008, 06:37 AM
Hello newbie,

Does adding the EditIndexes (intercepting the SortCommand event of the grid and inserting the indices in its handler) makes a difference? If this does not help, please isolate a stripped working version of your project, demonstrating the unexpected behavior, and send it enclosed to a formal support ticket. We will examine it in detail and will get around to you with more info on the subject.

Regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
newbie
Top achievements
Rank 1
Answers by
newbie
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Sebastian
Telerik team
Share this question
or