Here i m binding the dummy data to GridView using Data Table dt
HTML:
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvFiles" runat="server" OnRowDataBound="gvFilesRowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="File" HeaderText="File" />
<asp:TemplateField HeaderText="FileImage">
<ItemTemplate>
<asp:ImageButton ID="imgDownload" Height="80px" Width="100px" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
C#: here is the code to bind the data
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[1]{
new DataColumn("File", typeof(string)) });
dt.Rows.Add("myfile.txt");
dt.Rows.Add("result.rar");
dt.Rows.Add("image.png");
gvFiles.DataSource = dt;
gvFiles.DataBind();
}
Using onRowDataBound Event first i am finding the file extension from first cell of each row in GridView and then if it contains txt or pdf or rar it will show the images according to the match condition.
protected void gvFilesRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton imgDownload = (ImageButton)e.Row.FindControl("imgDownload");
string fileExtension = e.Row.Cells[0].Text;
if (fileExtension.Contains(".txt"))
{
imgDownload.ImageUrl = "Images/Text.jpg";
}
if(fileExtension.Contains(".pdf"))
{
imgDownload.ImageUrl = "Images/pdf.jpg";
}
if (fileExtension.Contains(".zip"))
{
imgDownload.ImageUrl = "Images/zip.jpg";
}
}
}