In this article I will show you how you can bind and display image in a datagridview using c# windows application. In this article I will show you how you can bind image in
datagridview on conditional bases using c#.net and windows application.
After this we will add our images in project resource. For this we will do Right click on Project property -> Properties -> Resources. Now add the images.
Now your grid view will look as shown.
Now we have done we will add the below code in our form code
Some of my previous articles are as follows: WaterMark
TextBox In Windows Applications Using C# and VB.Net, Pass
Value From One Form to Another Form in Windows Application C#.Net,VB.Net,
How
To Make a Single Row of DataGridview Bold Using C#.Net in Windows Application,
Bind
DataGridView In Windows Application Using C#, How
to Minimize an Application to the Taskbar Tray in C#.Net | System Tray
Application C#, Displaying
More than One Month in the Windows Forms MonthCalendar Control Using C#.Net,
How
to get the selected date of a MonthCalendar control in C#.
So for this article
first we will create a new windows application and add a datagridview control on it. After that we will add new
columns in our datagridview. For
adding column first we will add select the add column option.
A window will open
in this windows we will select DataGridViewImageCell, and add column header and
table field name and click on
After this we will add our images in project resource. For this we will do Right click on Project property -> Properties -> Resources. Now add the images.
Now your grid view will look as shown.
Now we have done we will add the below code in our form code
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace DemoWindowsApplication
{
/// <summary>
///how you can bind the image
in a datagridview in c# windows
application
/// </summary>
public partial class MainForm : Form
{
public
string connectionstring = "<-----Your constion string--------->";
/// <summary>
/// Default constructor for main form
/// </summary>
public
MainForm()
{
InitializeComponent();
}
/// <summary>
/// Load data when form loads first time
/// </summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
void
MainFormLoad(object sender, EventArgs e)
{
DataTable
_objdt = new DataTable();
_objdt = GetDataFromDataBase();
if
(_objdt.Rows.Count > 0)
{
dataGridView1.DataSource =
_objdt;
for
(int row = 0; row < dataGridView1.Rows.Count
- 1; row++)
{
decimal
retailPrice = Convert.ToDecimal(((DataGridViewTextBoxCell)dataGridView1.Rows[row].Cells[3]).Value);
if
(retailPrice > 18)
{
((DataGridViewImageCell)this.dataGridView1.Rows[row].Cells["Status"]).Value = Properties.Resources.down_icon;
}
else
{
((DataGridViewImageCell)this.dataGridView1.Rows[row].Cells["Status"]).Value = Properties.Resources.up_icon;
}
}
}
}
/// <summary>
/// Function for binding retriving the data from database
/// </summary>
/// <returns></returns>
public
DataTable GetDataFromDataBase()
{
DataTable
_objdt = new DataTable();
string
querystring = "select
author_name,title,retail_price from Books;";
SqlConnection
_objcon = new SqlConnection(connectionstring);
SqlDataAdapter
_objda = new SqlDataAdapter(querystring,
_objcon);
_objcon.Open();
_objda.Fill(_objdt);
return
_objdt;
}
}
}
|
In above code we
have checked the value and on the bases of value we are displaying the image. Now
we have done run the application for output.
0 comments:
Please let me know your view