Save Display Multiple Images in Windows Form DataGridView with single CheckBox selection using C# and VB.Net

Last Reply 2 months ago By pandeyism

Posted 2 months ago

how to multiple image upload in sql server database and insert in datagrid view using checkbox and allow only one selection checkbox in data grid view using c# windows form application.

i have to choose only 1 checkbox so other checkbox are unselected.

so i have already 1 select checkbox. now i have select 2 checkbox then 2 is selected and 1 is unseleted.

You are viewing reply posted by: pandeyism 2 months ago.
Posted 2 months ago Modified on 2 months ago

Hey pujandoshi,

Please refer below sample.

I have create below sample by taking help of below article links.

Upload, Insert and Display Binary Images from database in DataGridView in Windows Application using C# and VB.Net

Select (Check) one (single) CheckBox in DataGridView using C# and VB.Net

Namespaces

C#

using System.IO;
using System.Data.SqlClient;

VB.Net

Imports System.Data.SqlClient
Imports System.IO

Code

C#

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

    dataGridView1.CellContentClick += new DataGridViewCellEventHandler(DataGridView_CellClick);
}

private void BindDataGridView()
{
    string constr = @"Data Source=.\SQL2005;Initial Catalog=dbFiles;User id = sa;password=pass@123";
    using (SqlConnection conn = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter("select Data, Name from tblFiles WHERE ContentType IN ('image/jpeg','image/gif','image/png') ORDER BY ID ASC", conn))
        {
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
        }
    }
}

private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex == 0)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.Index == e.RowIndex)
            {
                row.Cells["checkBoxColumn"].Value = !Convert.ToBoolean(row.Cells["checkBoxColumn"].EditedFormattedValue);
            }
            else
            {
                row.Cells["checkBoxColumn"].Value = false;
            }
        }
    }
}

private void Insert(object sender, EventArgs e)
{
    using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
    {
        openFileDialog1.Multiselect = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            foreach (string filePath in openFileDialog1.FileNames)
            {
                byte[] bytes = File.ReadAllBytes(filePath);
                string contentType = "";
                switch (Path.GetExtension(filePath))
                {
                    case ".jpg":
                        contentType = "image/jpeg";
                        break;
                    case ".png":
                        contentType = "image/png";
                        break;
                    case ".gif":
                        contentType = "image/gif";
                        break;
                    case ".bmp":
                        contentType = "image/bmp";
                        break;
                }
                string constr = @"Data Source=.\SQL2005;Initial Catalog=dbFiles;uid=sa;pwd=pass@123";
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    string sql = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)";
                    using (SqlCommand cmd = new SqlCommand(sql, conn))
                    {
                        cmd.Parameters.AddWithValue("@Name", Path.GetFileName(filePath));
                        cmd.Parameters.AddWithValue("@ContentType", contentType);
                        cmd.Parameters.AddWithValue("@Data", bytes);
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
            }
        }
    }
    this.BindDataGridView();
}

VB.Net

Public Sub New()
    InitializeComponent()
    BindDataGridView()
    Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
    checkBoxColumn.HeaderText = ""
    checkBoxColumn.Width = 30
    checkBoxColumn.Name = "checkBoxColumn"
    dataGridView1.Columns.Insert(0, checkBoxColumn)
    AddHandler dataGridView1.CellContentClick, AddressOf DataGridView_CellClick
End Sub

Private Sub BindDataGridView()
    Dim constr As String = "Data Source=.\SQL2005;Initial Catalog=dbFiles;User id = sa;password=pass@123"

    Using conn As SqlConnection = New SqlConnection(constr)

        Using sda As SqlDataAdapter = New SqlDataAdapter("select Data, Name from tblFiles WHERE ContentType IN ('image/jpeg','image/gif','image/png') ORDER BY ID ASC", conn)
            Dim dt As DataTable = New DataTable()
            sda.Fill(dt)
            dataGridView1.DataSource = dt
        End Using
    End Using
End Sub

Private Sub DataGridView_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
    If e.RowIndex >= 0 AndAlso e.ColumnIndex = 0 Then
        For Each row As DataGridViewRow In dataGridView1.Rows
            If row.Index = e.RowIndex Then
                row.Cells("checkBoxColumn").Value = Not Convert.ToBoolean(row.Cells("checkBoxColumn").EditedFormattedValue)
            Else
                row.Cells("checkBoxColumn").Value = False
            End If
        Next
    End If
End Sub

Private Sub Insert(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
    Using openFileDialog1 As OpenFileDialog = New OpenFileDialog()
        openFileDialog1.Multiselect = True
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            For Each filePath As String In openFileDialog1.FileNames
                Dim bytes As Byte() = File.ReadAllBytes(filePath)
                Dim contentType As String = ""

                Select Case Path.GetExtension(filePath)
                    Case ".jpg"
                        contentType = "image/jpeg"
                    Case ".png"
                        contentType = "image/png"
                    Case ".gif"
                        contentType = "image/gif"
                    Case ".bmp"
                        contentType = "image/bmp"
                End Select

                Dim constr As String = "Data Source=.\SQL2005;Initial Catalog=dbFiles;uid=sa;pwd=pass@123"
                Using conn As SqlConnection = New SqlConnection(constr)
                    Dim sql As String = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)"
                    Using cmd As SqlCommand = New SqlCommand(sql, conn)
                        cmd.Parameters.AddWithValue("@Name", Path.GetFileName(filePath))
                        cmd.Parameters.AddWithValue("@ContentType", contentType)
                        cmd.Parameters.AddWithValue("@Data", bytes)
                        conn.Open()
                        cmd.ExecuteNonQuery()
                        conn.Close()
                    End Using
                End Using
            Next
        End If
    End Using

    Me.BindDataGridView()
End Sub

Screenshot