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

How to populate Image in RadGridView by C# code

3 Answers 311 Views
GridView
This is a migrated thread and some comments may be shown as answers.
miral shah
Top achievements
Rank 2
miral shah asked on 06 Oct 2009, 01:19 PM
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
         
    { 
        if (e.Item is GridHeaderItem) 
        { 
            GridHeaderItem header = (GridHeaderItem)e.Item; 
            // to set the height of the Grid Header 
            header.Height = Unit.Pixel(20); 
 
            // to add an Image in the Grid Header 
            Image img=new Image(); 
            img.ID = "Image1"
            img.ImageUrl = "~/Image1.gif"
            header["columnUniqueName"].Controls.Add(img); 
 
        } 
    } 


Can anyone please let me know how to do above thing in WPF RADGRIDVIEW because radgridview dont have any event like ItemDataBound. I m also using Telerik.windows.control namaspace not Telerik.Web.UI. 



Below is my Code

// I m adding Columns dynamically,

GridViewDataColumn IsUnRead = new GridViewDataColumn();
IsUnRead.Header = "R";
IsUnRead.UniqueName = "IsUnRead";
IsUnRead.Width = new GridLength(40);
this.gvEmail.Columns.Add(IsUnRead);



I want to have Image in header also dynamically by code.


Thanks in advance. Your view will be highly appreciated.

MIRAL SHAH 

3 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 07 Oct 2009, 01:04 PM
Hello miral shah,

I am sending you a sample that demonstrates how you can place an image in a header. The sample demonstrates both XAML and code declaration.

Hope this helps.

Sincerely yours,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
miral shah
Top achievements
Rank 2
answered on 07 Oct 2009, 01:47 PM
Respect Sir,

Thank you very much for sending the sample. Its works as a charm.
Can you let me know how to have IMAGE with Text in the same. Because header property of the radgridview accept Image or Text , not both at a time.

Thanking you
Miral Shah
0
Accepted
Milan
Telerik team
answered on 07 Oct 2009, 03:32 PM
Hello miral shah,

You can put any kind of content in the Header property. If you would like to combine several elements you just have to use some sort of panel (StackPanel for instance). Here is how you can add both an image and some text:

<telerikGrid:RadGridView x:Name="radGridView" AutoGenerateColumns="False">  
    <telerikGrid:RadGridView.Columns> 
        <telerikGrid:GridViewDataColumn DataMemberBinding="{Binding Sender}">  
            <telerikGrid:GridViewColumn.Header> 
                <StackPanel> 
                    <Image Source="/Content/Images/red.png" Height="40" Width="40"/>  
                    <TextBlock Text="MyHeaderText"/>   
                </StackPanel> 
            </telerikGrid:GridViewColumn.Header> 
        </telerikGrid:GridViewDataColumn> 
    </telerikGrid:RadGridView.Columns> 
</telerikGrid:RadGridView> 

Or...

private void CreateSecondImageInHeader()  
{  
    GridViewDataColumn column = new GridViewDataColumn();  
    column.DataMemberBinding = new Binding("Size");  
 
    Image image = new Image() { Height=40, Width=40 };  
    image.Source = new BitmapImage(new Uri(@"/Content/Images/green.png", UriKind.RelativeOrAbsolute));  
    TextBlock textBlock = new TextBlock() { Text = "MyHeaderText" };  
 
    // add the image and the text to a panel  
    StackPanel headerPanel = new StackPanel();  
    headerPanel.Children.Add(image);  
    headerPanel.Children.Add(textBlock);  
 
    column.Header = headerPanel;  
 
    this.radGridView.Columns.Add(column);  

Hope this helps.

Regards,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
miral shah
Top achievements
Rank 2
Answers by
Milan
Telerik team
miral shah
Top achievements
Rank 2
Share this question
or