locked
Problem to cast selected row to my entityObject. RRS feed

  • Question

  • User-79977429 posted

    Hi

    i've used a third party dataGridView to (DevExpress AspxGridView control) to display my data using ef 4.

    now i want to get selected row and cast to my expected entityObject to perform some actions like displaying or editing.

    here is my code to retrieving data :

    using (TestEntities db = new TestEntities())
    {
                    var query = from c in db.Contacts select new { c.FullName, c.Category.CategoryName, Time = EntityFunctions.CreateTime(c.DateCreated.Value.Hour, c.DateCreated.Value.Minute, c.DateCreated.Value.Second) };                
                    
                    this.ASPxGridView1.DataSource = query;
                    this.ASPxGridView1.DataBind();
    }


    and here is my code to get selected row and cast to my entityObject :

    var row = (Contact)this.ASPxGridView1.GetRow(e.VisibleIndex);

    but i'm facing this exception when i cast to Contact object :

    Unable to cast object of type
    '<>f__AnonymousType0`3[System.String,System.String,System.Nullable`1[System.TimeSpan]]'

    if i skip casting, i can't access my selected object properties.
    can anybody help me to solve this problem ?
    thanks in advance
    to type 'DevexpressWebEF1.Contact'.


    Wednesday, November 17, 2010 6:52 AM

Answers

  • User-79977429 posted

    Hi sansan

    i've solved my problem via converting anonymous types to an object via this article :

    http://www.codeproject.com/KB/linq/AnonymousTypeTransform.aspx

    thanks for your attention


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 17, 2010 1:23 PM

All replies

  • User1224194097 posted

    SelectedRow will be of GridViewRow Type or probably row type of the DevX gridview. You cannot cast that item directly to the object. You have to retrieve values from boundfields or Controls such as TextBox, Label using Find Control.

    Contact contact=new Contact()
        {
          FullName=gridview.SelectedRow.Cells[0].Text,
          CategoryName=((Label)gridview.SelectedRow.FindControl("LabelID")).Text
        };

    That should give you the contact object and retrieve properties from it.

    Wednesday, November 17, 2010 9:58 AM
  • User-79977429 posted

    Hi sansan

    i've solved my problem via converting anonymous types to an object via this article :

    http://www.codeproject.com/KB/linq/AnonymousTypeTransform.aspx

    thanks for your attention


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 17, 2010 1:23 PM