Drag & Drop Image from RadGridView

2 posts, 1 answers
  1. Anthony Korkmaz
    Anthony Korkmaz avatar
    2 posts
    Member since:
    Mar 2010

    Posted 23 Apr 2010 Link to this post

    Hello,

    Q1 2010 VS2008 SL3
    I have a GridView that contains an image in the first column, when I drag the row I don't want it to display the PrimaryKey of the row I'm dragging but I want it to show the image that is displayed in the first column.
    The problem is that the image is stored in Bytes[] in the database so I cant give the field as a Source to the image control and put it in the DragCue...
    I tried something like this in OnContactDragInfo :

                if (e.Options.Status == DragStatus.DragInProgress)
                {            
                    ContentControl cue = new ContentControl();
                    cue.ContentTemplate = ((Party)(gvContacts.SelectedItem)).Photo as DataTemplate;
                    cue.Content = gvContacts.SelectedItem;
                    e.Options.DragCue = cue;
                }

    But VS is giving this error "Cannot convert type 'byte[]' to 'System.Windows.DataTemplate' " on ((Party)(gvContacts.SelectedItem)).Photo as DataTemplate
    NB: The image is displaying fine in the grid view... so can I just take the first item from the GridView whatever it is and give it to the DragCue?

    Thanks for your help,

    Anthony



  2. Answer
    Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 28 Apr 2010 Link to this post

    Hi Anthony,

    I assume the Photo property of the Party business object is the one containing the byte array. If that is the case, you will have to convert the byte array to a BitmapImage. Then create an Image control and set its Source to be the BitmapImage. Finally you can pass the Image as a Content to the cue:

    if (e.Options.Status == DragStatus.DragInProgress)
    {           
        ContentControl cue = new ContentControl();
        byte[] bytes = ((Party)(gvContacts.SelectedItem)).Photo;
        BitmapImage bmpImage = ImageFromBuffer(bytes);
        Image image = new Image();
        image.Source = bmpImage;
        cue.Content = image;
        e.Options.DragCue = cue;
    }
     
     
     
    // Method to convert byte array to BitmapImage
    public BitmapImage ImageFromBuffer(Byte[] bytes)
    {
        MemoryStream stream = new MemoryStream(bytes);
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = stream;
        image.EndInit();
        return image;
    }

    Give it a try and let me know how it goes.

    Kind regards,
    Kiril Stanoev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
Back to Top