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

OnItemDataBound

5 Answers 167 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Minh
Top achievements
Rank 1
Minh asked on 19 Aug 2011, 09:37 PM
Hi,

I'm using the OnItemDataBound to split my code into methods as follows....during the update statement, it gave an error of Unable to cast object of type 'GHG_MCA.WellVentEvents' to type 'System.Data.DataRowView'. I think it is because while calling the update method the grid was unable to locate the datasource?

protected

 

 

void OnItemDataBoundHandler(object sender, GridItemEventArgs e)

 

{

 

 

    if (e.Item is GridDataItem)

 

    {

 

 

    GridDataItem dataItem = e.Item as GridDataItem;

 

 

 

    string CreateDate = dataItem["CreationDate"].Text;

 

    }

 

 

    if (e.Item.IsInEditMode)

 

    {

 

 

 

 

 

        //This area is for Update existing rows

 

 

 

        if (!(e.Item is IGridInsertItem))

 

        {

        Update(sender, e);

        }

 

 

        //This area is for inserting new rows

 

 

 

        else

 

        {

        NewInsert(sender, e);

 

        }

    }

}

 

 

 

private void Update(object sender, GridItemEventArgs e)

 

{

 

 

    if (e.Item.IsInEditMode)

 

    {

 

 

        GridEditableItem item = (GridEditableItem)e.Item;

 

 

 

        //This area is for update existing rows

 

 

 

        if (!(e.Item is IGridInsertItem))

 

    {

 

 

        //For the Area section

 

 

 

        RadComboBox combo = (RadComboBox)item.FindControl("RadComboBoxArea");

 

 

 

        RadComboBoxItem selectedItem = new RadComboBoxItem();

 

        selectedItem.Text = ((

 

DataRowView)e.Item.DataItem)["AreaName"].ToString();

 

        combo.Items.Add(selectedItem);

        selectedItem.DataBind();
    }
}

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 20 Aug 2011, 10:13 AM
Hello,

Sorry but can you explain (If possible then also provide ScreenShot) which type of functionality you want in RadGrid.

Thanks,
Jayesh Goyani
0
Minh
Top achievements
Rank 1
answered on 21 Aug 2011, 02:58 AM
Hi,

basically, this my code. I would like to put the area that is marked as MOVE THIS TO A DIFFERENT CLASS  ....into a different class to separate the business and presentation layer.........Attached is the error message that I'm getting

using

System;

using 

System.Collections.Generic;

using 

System.Linq;

using 

System.Web;

using 

System.Web.UI;

using 

System.Web.UI.WebControls;

using 

Telerik.Web.UI;

using 

System.Configuration;

using 

System.Data;

using 

System.Data.SqlClient;

using 

System.ComponentModel;

 

 

namespace

GHG_MCA

{

public partial class WellVent : System.Web.UI.Page

{

protected void OnItemDataBoundHandler(object sender, GridItemEventArgs e)

{

if (e.Item is GridDataItem)

GridDataItem dataItem = e.Item as GridDataItem;

string CreateDate = dataItem["CreationDate"].Text;

}

if (e.Item.IsInEditMode)

{
GridEditableItem item = (GridEditableItem)e.Item;
//Change the width of the edit mode for numeric columns

RadNumericTextBox SalesLinePres = (RadNumericTextBox)item["SalesLinePressure"].Controls[0];

SalesLinePres.Width = Unit.Pixel(25);

RadNumericTextBox countofEvents = (RadNumericTextBox)item["CountofEvents"].Controls[0];

countofEvents.Width =Unit.Pixel(25);

RadNumericTextBox ShutInPressure = (RadNumericTextBox)item["ShutInPressure"].Controls[0];

ShutInPressure.Width = Unit.Pixel(25);

ShutInPressure.Visible = false;

RadNumericTextBox EventDuration = (RadNumericTextBox)item["EventDuration"].Controls[0];

EventDuration.Width = Unit.Pixel(25);

EventDuration.Visible = false;

RadDatePicker EventStartDateTime = (RadDatePicker)item["EventStartDateTime"].Controls[0];

EventStartDateTime.Width = Unit.Pixel(100);

RadDatePicker EventEndDateTime = (RadDatePicker)item["EventEndDateTime"].Controls[0];

EventEndDateTime.Width = Unit.Pixel(100);

//This area is for Update existing rows

 

if (!(e.Item is IGridInsertItem))

{
//////////////////////////////MOVE THIS TO A DIFFERENT CLASS///////////////////////////

//For the Area section

 

RadComboBox combo = (RadComboBox)item.FindControl("RadComboBoxArea");

RadComboBoxItem selectedItem = new RadComboBoxItem();

selectedItem.Text = ((DataRowView)e.Item.DataItem)["AreaName"].ToString();

combo.Items.Add(selectedItem);

selectedItem.DataBind();

Session["ID"] = selectedItem.Value; //use the same ID for all hierarchy

 

//For the FMT section

combo = (

RadComboBox)item.FindControl("RadComboBoxFMT");

selectedItem = new RadComboBoxItem();

selectedItem.Text = ((DataRowView)e.Item.DataItem)["AssetName"].ToString();

combo.Items.Add(selectedItem);

selectedItem.DataBind();

//For the Field section

combo = (

RadComboBox)item.FindControl("RadComboBoxField");

selectedItem = new RadComboBoxItem();

selectedItem.Text = ((DataRowView)e.Item.DataItem)["FieldName"].ToString();

combo.Items.Add(selectedItem);

selectedItem.DataBind();

//For the WellBore section

combo = (

RadComboBox)item.FindControl("RadComboBoxWell");

selectedItem = new RadComboBoxItem();

selectedItem.Text = ((DataRowView)e.Item.DataItem)["WellBoreName"].ToString();

combo.Items.Add(selectedItem);

selectedItem.DataBind();

}

 

 

//This area is for inserting new rows

 

 

 

else

 

{

NewInsert(sender, e);

 

}

}

}

thanks,
Minh Bui

0
Iana Tsolova
Telerik team
answered on 24 Aug 2011, 08:59 AM
Hi Minh,

Reviewing your code I observed that you have several casts of the DataItem to DataRowView. This is applicable when the grid is bound to SqlDataSource or DataTable for instance. However if the grid is bound to custom objects, you need to cast the DataItem to the particular object.

Regards,
Iana
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Minh
Top achievements
Rank 1
answered on 24 Aug 2011, 04:11 PM
Hi,

yes. The grid is bound to a custom object. I'm unsure of the way to cast using the generic that is bound to the grid....

The following is my onneeddatasource
      using (dl = new WellVentDataLayer())
            {
                BindingList<WellVentEvents> oEvents = dl.GetWellVentEvents();        
                Session["DataSource"] = oEvents;
                RadGrid1.DataSource = Session["DataSource"];
            }
...I'm thinking that the grid is already bound in memory during update....so I am not sure of the collection and syntax that is needed. the following is the area of code that requires assistance (particular on line 'selectedItem.Text')

 

//this area is for update  

 

//For the Area section 

 

RadComboBox combo = (RadComboBox)item.FindControl("RadComboBoxArea");  

RadComboBoxItem selectedItem = new RadComboBoxItem(); 

selectedItem.Text = ((CAST??) e.Item.DataItem)["AreaName"].ToString();  

combo.Items.Add(selectedItem);

selectedItem.DataBind();

}

thanks,
Minh Bui

0
Accepted
Iana Tsolova
Telerik team
answered on 25 Aug 2011, 01:35 PM
Hi Minh,

Does it work when you change the problematic row as below:
selectedItem.Text = ((wellVentDataLayer) e.Item.DataItem)["AreaName"].ToString();

If not, then try debugging the line and see what is the proper type of the DataItem object in this case.

Kind regards,
Iana
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
Minh
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Minh
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or