Loop through Windows Form DataGridView rows and display cells value in Label using C# and VB.Net

Last Reply 3 months ago By pandeyism

Posted 3 months ago

How display saving status into label from datagridview.

iterate cells value into label datagridview vb.net

Private Sub SaveData()
    For Each row As DataGridViewRow In DataGridView1.Rows
        Dim cmd As New SqlCommand
        cmd.Connection = SqlCon 'Active Connection
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "insert"
        cmd.Parameters.AddWithValue("@Name", Trim(row.Cells(0).Value.ToString()))
        cmd.Parameters.AddWithValue("@Shift", Trim(row.Cells(1).Value.ToString()))
        Try
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            'Throw ex
            MsgBox(ex.Message)
        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("Name"), New DataColumn("Shift")})
    dt.Rows.Add("Ahemd", "1")
    dt.Rows.Add("Ajith", "3")
    dt.Rows.Add("Soumya", "OFF")
    dataGridView1.DataSource = dt
End Sub

 

You are viewing reply posted by: pandeyism 3 months ago.
Posted 3 months ago

Hey satabeach,

Please refer below sample.

Namespaces

C#

using System.Data.SqlClient;

VB.Net

Imports System.Data.SqlClient

Code

C#

public Form1()
{
    InitializeComponent();
    BindGrid();
}
private void SaveData()
{
    string submitresult;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        string constr = @"Server=\SQL2005;DataBase=Test;UID=sa;PWD=pass";
        SqlConnection SqlCon = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("INSERT INTO tblDemoSave(Name, Shift) VALUES(@NAme, @Shift)", SqlCon);
        cmd.Parameters.AddWithValue("@Name", row.Cells[0].Value);
        cmd.Parameters.AddWithValue("@Shift", row.Cells[1].Value);
        try
        {
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            SqlCon.Close();
            cmd = new SqlCommand("SELECT MAX(Name) FROM tblDemoSave", SqlCon);
            SqlCon.Open();
            submitresult = Convert.ToString(cmd.ExecuteScalar());
            label1.Text = "Saving " + 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("Name"), new DataColumn("Shift") });
    dt.Rows.Add("Ahemd", "1");
    dt.Rows.Add("Ajith", "3");
    dt.Rows.Add("Soumya", "OFF");
    dataGridView1.DataSource = dt;
}
private void button2_Click(object sender, EventArgs e)
{
    SaveData();
}

VB.Net

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 = "Server=\SQL2005;DataBase=Test;UID=sa;PWD=pass"
        Dim SqlCon As SqlConnection = New SqlConnection(constr)
        Dim cmd As SqlCommand = New SqlCommand("INSERT INTO tblDemoSave(Name, Shift) VALUES(@NAme, @Shift)", SqlCon)
        cmd.Parameters.AddWithValue("@Name", row.Cells(0).Value)
        cmd.Parameters.AddWithValue("@Shift", row.Cells(1).Value)
        Try
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            SqlCon.Close()
            cmd = New SqlCommand("SELECT MAX(Name) FROM tblDemoSave", SqlCon)
            SqlCon.Open()
            submitresult = Convert.ToString(cmd.ExecuteScalar())
            label1.Text = "Saving " & 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("Name"), New DataColumn("Shift")})
    dt.Rows.Add("Ahemd", "1")
    dt.Rows.Add("Ajith", "3")
    dt.Rows.Add("Soumya", "OFF")
    dataGridView1.DataSource = dt
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
    SaveData()
End Sub

Screenshot