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

GridImageColumn + File.Exists

3 Answers 99 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marko
Top achievements
Rank 1
Marko asked on 07 Feb 2016, 02:13 PM

Hello!

I'm testing telerik:RadGrid control..

I would like to use picture (if it exists) on server. If file doesn't exists i would like to use "default_image.jpg". I suppose have to use RadGrid1_ItemDataBound but don't know how?

Right now fixing DataImageUrlFields="Artikal_ID" DataImageUrlFormatString="IMG/{0}_tiny.jpg" work without problem, but don't know to test File.Exists and use correct value.

Any advice?

Now have something like this:

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" GroupPanelPosition="Top" PageSize="20" AllowSorting="True" AllowPaging="True" OnItemCommand="RadGrid1_ItemCommand" OnItemDataBound="RadGrid1_ItemDataBound">
    <ExportSettings>
        <Pdf PageWidth="">
        </Pdf>
 
    </ExportSettings>
    <MasterTableView NoMasterRecordsText="Molimo odaberite kategoriju." AllowMultiColumnSorting="True">
        <Columns>
            <telerik:GridBoundColumn DataField="Artikal_ID" FilterControlAltText="Filter Artikal_ID column" HeaderText="┼áifra artikla" ReadOnly="True" SortExpression="Artikal_ID" UniqueName="Artikal_ID">
 
                <ColumnValidationSettings>
                    <ModelErrorMessage Text=""></ModelErrorMessage>
                </ColumnValidationSettings>
 
            </telerik:GridBoundColumn>
 
            <telerik:GridImageColumn DataImageUrlFields="Artikal_ID" DataImageUrlFormatString="IMG/{0}_tiny.jpg" AlternateText="" DataAlternateTextField="Artikal"
                ImageAlign="Middle" ImageWidth="60px" HeaderText="" HeaderButtonType="None">
                <ItemStyle CssClass="RadGrid" Height="80px" HorizontalAlign="Center" VerticalAlign="Middle" />
            </telerik:GridImageColumn>

 

 

3 Answers, 1 is accepted

Sort by
0
Marko
Top achievements
Rank 1
answered on 09 Feb 2016, 05:49 AM

Sorry all, forget to wrote known RadGrid1_ItemDataBound code:

Maybe you can now easyer give me a hint about question:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
       {
 
 
           if (e.Item is GridDataItem)
           {
               GridDataItem dataItem = e.Item as GridDataItem;
               string file_path = "";
               file_path = ("IMG\\" + dataItem["Artikal_ID"].Text + "_big.jpg");
               if (File.Exists(Server.MapPath(file_path)))
               {
                   file_path = ("IMG\\" + dataItem["Artikal_ID"].Text + "_big.jpg");
                   //missing code
               }
               else
               {
                   file_path = ("IMG\\default_big.jpg");
                   ///missing code
               }
 
           }
       }

Thank you!
0
Marko
Top achievements
Rank 1
answered on 09 Feb 2016, 12:49 PM

Finnaly find solution :-)

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
      {
 
 
          if (e.Item is GridDataItem)
          {
              GridDataItem dataItem = e.Item as GridDataItem;
              string file_path = "";
              file_path = ("IMG\\" + dataItem["Artikal_ID"].Text + "_big.jpg");
              if (File.Exists(Server.MapPath(file_path)))
              {
                  file_path = ("IMG\\" + dataItem["Artikal_ID"].Text + "_big.jpg");
                  Image img_thumb = dataItem["Artikal"].Controls[0] as Image;
                  img_thumb.ImageUrl = file_path;
              }
              else
              {
                  
                  file_path = ("IMG\\default_big.jpg");
                  Image img_thumb = dataItem["Artikal"].Controls[0] as Image;
                  img_thumb.ImageUrl = file_path;
 
              }
 
          }
      }

 Best regards!

 

 

 

0
Eyup
Telerik team
answered on 11 Feb 2016, 06:58 AM
Hello Marko,

Thank you for sharing your approach with our community. I hope it will prove helpful to other developers as well.
Alternatively, you can also use GridTemplateColumn to build-up your own custom configuration:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/columns/column-types#gridtemplatecolumn

Regards,
Eyup
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Marko
Top achievements
Rank 1
Answers by
Marko
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or