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.Loop through Windows Form DataGridView rows and display cells value in Label using C# and VB.Net<p>Hey <a class="username" rel="satabeach"> satabeach</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">public Form1() { InitializeComponent(); BindGrid(); } private void SaveData() { string submitresult; foreach (DataGridViewRow row in dataGridView1.Rows) { string constr = @&#34;Server=\SQL2005;DataBase=Test;UID=sa;PWD=pass&#34;; SqlConnection SqlCon = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(&#34;INSERT INTO tblDemoSave(Name, Shift) VALUES(@NAme, @Shift)&#34;, SqlCon); cmd.Parameters.AddWithValue(&#34;@Name&#34;, row.Cells[0].Value); cmd.Parameters.AddWithValue(&#34;@Shift&#34;, row.Cells[1].Value); try { cmd.Connection.Open(); cmd.ExecuteNonQuery(); SqlCon.Close(); cmd = new SqlCommand(&#34;SELECT MAX(Name) FROM tblDemoSave&#34;, SqlCon); SqlCon.Open(); submitresult = Convert.ToString(cmd.ExecuteScalar()); label1.Text = &#34;Saving &#34; + submitresult; } catch (Exception ex) { // Throw ex //Interaction.MsgBox(ex.Message); } finally { SqlCon.Close(); SqlCon.Dispose(); } } } private void BindGrid() { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;Name&#34;), new DataColumn(&#34;Shift&#34;) }); dt.Rows.Add(&#34;Ahemd&#34;, &#34;1&#34;); dt.Rows.Add(&#34;Ajith&#34;, &#34;3&#34;); dt.Rows.Add(&#34;Soumya&#34;, &#34;OFF&#34;); dataGridView1.DataSource = dt; } private void button2_Click(object sender, EventArgs e) { SaveData(); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Public Sub New() InitializeComponent() BindGrid() End Sub Private Sub SaveData() Dim submitresult As String For Each row As DataGridViewRow In dataGridView1.Rows Dim constr As String = &#34;Server=\SQL2005;DataBase=Test;UID=sa;PWD=pass&#34; Dim SqlCon As SqlConnection = New SqlConnection(constr) Dim cmd As SqlCommand = New SqlCommand(&#34;INSERT INTO tblDemoSave(Name, Shift) VALUES(@NAme, @Shift)&#34;, SqlCon) cmd.Parameters.AddWithValue(&#34;@Name&#34;, row.Cells(0).Value) cmd.Parameters.AddWithValue(&#34;@Shift&#34;, row.Cells(1).Value) Try cmd.Connection.Open() cmd.ExecuteNonQuery() SqlCon.Close() cmd = New SqlCommand(&#34;SELECT MAX(Name) FROM tblDemoSave&#34;, SqlCon) SqlCon.Open() submitresult = Convert.ToString(cmd.ExecuteScalar()) label1.Text = &#34;Saving &#34; &amp; submitresult Catch ex As Exception Finally SqlCon.Close() SqlCon.Dispose() End Try Next End Sub Private Sub BindGrid() Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;Name&#34;), New DataColumn(&#34;Shift&#34;)}) dt.Rows.Add(&#34;Ahemd&#34;, &#34;1&#34;) dt.Rows.Add(&#34;Ajith&#34;, &#34;3&#34;) dt.Rows.Add(&#34;Soumya&#34;, &#34;OFF&#34;) dataGridView1.DataSource = dt End Sub Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) SaveData() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/YHzDuem.gif" alt="" width="265" height="201" /></p>https://www.aspforums.net:443/Threads/198397/Loop-through-Windows-Form-DataGridView-rows-and-display-cells-value-in-Label-using-C-and-VBNet/https://www.aspforums.net:443/Threads/198397/Loop-through-Windows-Form-DataGridView-rows-and-display-cells-value-in-Label-using-C-and-VBNet/Mon, 26 Nov 2018 00:59:28 GMT