Wednesday, 9 July 2014

Bind And Display Image in a DatagridView Using C#.Net in Windows Application

7/09/2014 - By Pranav Singh 0

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.



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.




About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top