Display Image with CheckBox from Database in Windows Form DataGridView using C# and VB.Net

Last Reply 11 days ago By pandeyism

Posted 11 days ago

insert image with checkbox in data gridview using c# windows form application

You are viewing reply posted by: pandeyism 11 days ago.
Posted 11 days ago Modified on 11 days ago

Hey pujandoshi,

Please refer below sample.

Database

For this sample i am using of a table named tblFiles whose schema is defined as follows.

You can download the database table SQL by clicking the download link below.

Download SQL file

For insert refer below article.

Save (Insert) Image in Database in Windows Application using C# and VB.Net

Namespaces

C#

using System.Data.SqlClient;

VB.Net

Imports System.Data.SqlClient

Code

C#

public Form1()
{
    InitializeComponent();
    this.GetPics();
    DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
    checkBoxColumn.HeaderText = "";
    checkBoxColumn.Width = 30;
    checkBoxColumn.Name = "checkBoxColumn";
    dataGridView1.Columns.Insert(0, checkBoxColumn);
}

private void GetPics()
{
    string constring = @"Data Source=.\SQL2005;Initial Catalog=dbFiles;User id = sa;password=pass@123";
    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand("select TOP 4 Data, Name from tblFiles WHERE ContentType='image/jpeg' ORDER BY ID ASC", con))
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
        }
    }
}

VB.Net

Public Sub New()
    InitializeComponent()
    Me.GetPics()
    Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
    checkBoxColumn.HeaderText = ""
    checkBoxColumn.Width = 30
    checkBoxColumn.Name = "checkBoxColumn"
    dataGridView1.Columns.Insert(0, checkBoxColumn)
End Sub

Private Sub GetPics()
    Dim constring As String = "Data Source=.\SQL2005;Initial Catalog=dbFiles;User id = sa;password=pass@123"
    Using con As SqlConnection = New SqlConnection(constring)
        Using cmd As SqlCommand = New SqlCommand("select TOP 4 Data, Name from tblFiles WHERE ContentType='image/jpeg' ORDER BY ID ASC", con)
            Dim dt As DataTable = New DataTable()
            Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
            sda.Fill(dt)
            dataGridView1.DataSource = dt
        End Using
    End Using
End Sub

Screenshot