ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Save Display Multiple Images in Windows Form DataGridView with single CheckBox selection using C# and VB.Net<p>Hey <a class="username" rel="pujandoshi"> pujandoshi</a>,</p> <p>Please refer below sample.</p> <p>I have create below sample by taking help of below article links.</p> <h1 class="header"><a title="Upload, Insert and Display Binary Images from database in DataGridView in Windows Application using C# and VB.Net'a" href="https://www.aspsnippets.com/Articles/Upload-Insert-and-Display-Binary-Images-from-database-in-DataGridView-in-Windows-Application-using-C-and-VBNet.aspx">Upload, Insert and Display Binary Images from database in DataGridView in Windows Application using C# and VB.Net</a></h1> <h1 class="header"><a title="Select (Check) one (single) CheckBox in DataGridView using C# and VB.Net'a" href="https://www.aspsnippets.com/Articles/Select-Check-one-single-CheckBox-in-DataGridView-using-C-and-VBNet.aspx">Select (Check) one (single) CheckBox in DataGridView using C# and VB.Net</a></h1> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.IO</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">public Form1() { InitializeComponent(); BindDataGridView(); DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn(); checkBoxColumn.HeaderText = &#34;&#34;; checkBoxColumn.Width = 30; checkBoxColumn.Name = &#34;checkBoxColumn&#34;; dataGridView1.Columns.Insert(0, checkBoxColumn); dataGridView1.CellContentClick += new DataGridViewCellEventHandler(DataGridView_CellClick); } private void BindDataGridView() { string constr = @&#34;Data Source=.\SQL2005;Initial Catalog=dbFiles;User id = sa;password=pass@123&#34;; using (SqlConnection conn = new SqlConnection(constr)) { using (SqlDataAdapter sda = new SqlDataAdapter(&#34;select Data, Name from tblFiles WHERE ContentType IN (&#39;image/jpeg&#39;,&#39;image/gif&#39;,&#39;image/png&#39;) ORDER BY ID ASC&#34;, conn)) { DataTable dt = new DataTable(); sda.Fill(dt); dataGridView1.DataSource = dt; } } } private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex &gt;= 0 &amp;&amp; e.ColumnIndex == 0) { foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Index == e.RowIndex) { row.Cells[&#34;checkBoxColumn&#34;].Value = !Convert.ToBoolean(row.Cells[&#34;checkBoxColumn&#34;].EditedFormattedValue); } else { row.Cells[&#34;checkBoxColumn&#34;].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 = &#34;&#34;; switch (Path.GetExtension(filePath)) { case &#34;.jpg&#34;: contentType = &#34;image/jpeg&#34;; break; case &#34;.png&#34;: contentType = &#34;image/png&#34;; break; case &#34;.gif&#34;: contentType = &#34;image/gif&#34;; break; case &#34;.bmp&#34;: contentType = &#34;image/bmp&#34;; break; } string constr = @&#34;Data Source=.\SQL2005;Initial Catalog=dbFiles;uid=sa;pwd=pass@123&#34;; using (SqlConnection conn = new SqlConnection(constr)) { string sql = &#34;INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)&#34;; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue(&#34;@Name&#34;, Path.GetFileName(filePath)); cmd.Parameters.AddWithValue(&#34;@ContentType&#34;, contentType); cmd.Parameters.AddWithValue(&#34;@Data&#34;, bytes); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } } } } this.BindDataGridView(); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Public Sub New() InitializeComponent() BindDataGridView() Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn() checkBoxColumn.HeaderText = &#34;&#34; checkBoxColumn.Width = 30 checkBoxColumn.Name = &#34;checkBoxColumn&#34; dataGridView1.Columns.Insert(0, checkBoxColumn) AddHandler dataGridView1.CellContentClick, AddressOf DataGridView_CellClick End Sub Private Sub BindDataGridView() Dim constr As String = &#34;Data Source=.\SQL2005;Initial Catalog=dbFiles;User id = sa;password=pass@123&#34; Using conn As SqlConnection = New SqlConnection(constr) Using sda As SqlDataAdapter = New SqlDataAdapter(&#34;select Data, Name from tblFiles WHERE ContentType IN (&#39;image/jpeg&#39;,&#39;image/gif&#39;,&#39;image/png&#39;) ORDER BY ID ASC&#34;, 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 &gt;= 0 AndAlso e.ColumnIndex = 0 Then For Each row As DataGridViewRow In dataGridView1.Rows If row.Index = e.RowIndex Then row.Cells(&#34;checkBoxColumn&#34;).Value = Not Convert.ToBoolean(row.Cells(&#34;checkBoxColumn&#34;).EditedFormattedValue) Else row.Cells(&#34;checkBoxColumn&#34;).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 = &#34;&#34; Select Case Path.GetExtension(filePath) Case &#34;.jpg&#34; contentType = &#34;image/jpeg&#34; Case &#34;.png&#34; contentType = &#34;image/png&#34; Case &#34;.gif&#34; contentType = &#34;image/gif&#34; Case &#34;.bmp&#34; contentType = &#34;image/bmp&#34; End Select Dim constr As String = &#34;Data Source=.\SQL2005;Initial Catalog=dbFiles;uid=sa;pwd=pass@123&#34; Using conn As SqlConnection = New SqlConnection(constr) Dim sql As String = &#34;INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)&#34; Using cmd As SqlCommand = New SqlCommand(sql, conn) cmd.Parameters.AddWithValue(&#34;@Name&#34;, Path.GetFileName(filePath)) cmd.Parameters.AddWithValue(&#34;@ContentType&#34;, contentType) cmd.Parameters.AddWithValue(&#34;@Data&#34;, bytes) conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Using Next End If End Using Me.BindDataGridView() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/CS1g7hg.gif" alt="" width="316" height="351" /></p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/133125/Save-Display-Multiple-Images-in-Windows-Form-DataGridView-with-single-CheckBox-selection-using-C-and-VBNet/https://www.aspforums.net:443/Threads/133125/Save-Display-Multiple-Images-in-Windows-Form-DataGridView-with-single-CheckBox-selection-using-C-and-VBNet/Thu, 10 Jan 2019 03:57:40 GMT