Navin Kumar Sinha Said..
Tip 1: Change the color of a GridView Row based on some condition
C#
protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null )
{
DataRowView drv = ( DataRowView )e.Row.DataItem;
string catName = Convert .ToString(drv[ "CategoryName" ]);
if (catName.Trim() == "Confections" )
e.Row.BackColor = System.Drawing. Color .LightBlue;
}
}
VB.NET
Protected Sub GridView1_RowCreated( ByVal sender As Object , ByVal e As GridViewRowEventArgs)
If Not e.Row.DataItem Is Nothing Then
Dim drv As DataRowView = CType (e.Row.DataItem, DataRowView)
Dim catName As String = Convert.ToString(drv("CategoryName"))
If catName.Trim() = "Confections" Then
e.Row.BackColor = System.Drawing.Color.LightBlue
End If
End If
End Sub
Tip 2: How to create an Image Command Field Column and add to the GridView at runtime
C#
if (!Page.IsPostBack)
{
CommandField cmdField = new CommandField ();
cmdField.ButtonType = ButtonType .Image;
cmdField.SelectImageUrl = "~/Images/Home_Np1.GIF" ;
cmdField.ShowSelectButton = true ;
cmdField.HeaderText = "Select" ;
GridView1.Columns.Add(cmdField);
GridView1.DataBind();
}
VB.NET
If ( Not Page.IsPostBack) Then
Dim cmdField As CommandField = New CommandField()
cmdField.ButtonType = ButtonType.Image
cmdField.SelectImageUrl = "~/Images/Home_Np1.GIF"
cmdField.ShowSelectButton = True
cmdField.HeaderText = "Select"
GridView1.Columns.Add(cmdField)
GridView1.DataBind()
End If
Tip 3: How to display images in the GridView from Filesystem based on an existing Column
Let us imagine that you have a folder ‘Images’ where you have stored images for each category. Eg: 1.GIF, 2.GIF, 3.GIF and so on. Now you want to display a different image based on each CategoryID. So for CategoryID = 1, the image is 1.GIF; for CategoryID=2, the image is 2.GIF and so on.
< asp : TemplateField >
< ItemTemplate >
< asp : Image runat ="server" ImageUrl =' <% # "~/Images/"+ Eval("CategoryID") + ".GIF" %> ' >
asp : Image >
ItemTemplate >
asp : TemplateField >
Tip 4: How to Retrieve Images from the database and display it in a GridView
I will assume that we have a image column called CatImg inthe Categories table.
The first step would be to create an ImageHandler. In such scenarios such as the gridview, usually prefer to go in for a handler when I have to return binary data directly from the database. It gives more control on the resource returned. Moreover it is a preferred solution when you have to set the image programmatically.
To add a handler, right click project > Add New Item > Generic Handler > ShowImage.ashx. The code shown below, uses the Request.QueryString[“id”] to retrieve the CategoryID from it. The ID is then passed to the ‘ShowCatImage()’ method where the image is fetched from the database and returned in a MemoryStream object. We then read the stream into a byte array. Using the OutputStream.Write(), we write the sequence of bytesto the current stream and you get to see your image.
C#
<% @ WebHandler Language ="C#" Class ="ShowImage" %>
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public class ShowImage : IHttpHandler
{
public void ProcessRequest( HttpContext context)
{
Int32 catid;
if (context.Request.QueryString[ "id" ] != null )
catid = Convert .ToInt32(context.Request.QueryString[ "id" ]);
else
throw new ArgumentException ( "No parameter specified" );
context.Response.ContentType = "image/jpeg" ;
Stream strm = ShowCatImage(catid);
byte [] buffer = new byte [4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);
}
public Stream ShowCatImage( int catid)
{
string conn = ConfigurationManager .ConnectionStrings[ "NorthwindConnectionString" ].ConnectionString;
SqlConnection connection = new SqlConnection (conn);
string sql = "SELECT catImg FROM Categories WHERE CategoryID = @ID" ;
SqlCommand cmd = new SqlCommand (sql, connection);
cmd.CommandType = CommandType .Text;
cmd.Parameters.AddWithValue( "@ID" , catid);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream (( byte [])img);
}
catch
{
return null ;
}
finally
{
connection.Close();
}
}
public bool IsReusable
{
get
{
return false ;
}
}
}
VB.NET
<%@ WebHandler Language="vb" Class ="ShowImage" %>
Imports System
Imports Syste