Programming for Everybody

VB.net - load data from SQL database format datagridview and use statement with

 


Contents🎞
Load data from SQL database 00:56
Delete the last blank row and Prevent user to add rows 03:59
Fill the width of the available display area in datagridview 04:35
Change font column headers (Type - size - Style) 05:25
Change Colors column headers (Backcolor - Forecolor) 06:40
Change alignment column headers 08:51
Change font columns (Type - size) 09:47
Change alignment columns 10:46
With...End With Statement 11:35


🎁🎁Full course VB.net with MySQL database(Insert, Update, Delete, and Search)🎉🎉 with code




https://youtube.com/clip/UgkxaESVafvSL8Sjake6z1v2gUA9vA1Xe-gk

Code Video
Imports MySql.Data.MySqlClient
Public Class Form1
    Dim conn As New MySqlConnection("Data Source=localhost;Database=employees;Username=root;Password=55551")
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "Image|*.jpg;*.png;*.bmp"
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            TextBox4.Text = OpenFileDialog1.FileName
            PictureBox1.Image = Image.FromFile(TextBox4.Text)
        Else
            MessageBox.Show("Please Select Image")
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim cmd1 As New MySqlCommand("Insert Into names(firstname,lastname,telephone,image_path)Values(@firstname,@lastname,@telephone,@image_path)", conn)
        cmd1.Parameters.AddWithValue("firstname", TextBox1.Text.Trim)
        cmd1.Parameters.AddWithValue("lastname", TextBox2.Text.Trim)
        cmd1.Parameters.AddWithValue("telephone", TextBox3.Text.Trim)
        cmd1.Parameters.AddWithValue("image_path", TextBox4.Text.Trim)
        conn.Open()
        cmd1.ExecuteNonQuery()
        conn.Close()
        MessageBox.Show("Data Inserted Successfully")
        display_data()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        PictureBox1.Image = Nothing
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        display_data()
    End Sub
    Private Sub display_data()
        Dim cmd2 As New MySqlCommand("Select * From names Order By id desc", conn)
        Dim da As New MySqlDataAdapter(cmd2)
        Dim dt As New DataTable
        dt.Clear()
        da.Fill(dt)
        DataGridView1.DataSource = dt
    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        idlbl.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString
        TextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
        TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString
        TextBox3.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString
        TextBox4.Text = DataGridView1.CurrentRow.Cells(4).Value.ToString
        PictureBox1.Image = Image.FromFile(TextBox4.Text)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim cmd3 As New MySqlCommand("Update names Set firstname=@firstname,lastname=@lastname,telephone=@telephone,image_path=@image_path Where id=@id", conn)
        cmd3.Parameters.AddWithValue("firstname", TextBox1.Text.Trim)
        cmd3.Parameters.AddWithValue("lastname", TextBox2.Text.Trim)
        cmd3.Parameters.AddWithValue("telephone", TextBox3.Text.Trim)
        cmd3.Parameters.AddWithValue("image_path", TextBox4.Text)
        cmd3.Parameters.AddWithValue("id", idlbl.Text)
        conn.Open()
        cmd3.ExecuteNonQuery()
        conn.Close()
        MessageBox.Show("Data Updated successfully")
        display_data()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim cmd3 As New MySqlCommand("Delete From names Where id=@id", conn)
        cmd3.Parameters.AddWithValue("id", idlbl.Text)
        conn.Open()
        cmd3.ExecuteNonQuery()
        conn.Close()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        PictureBox1.Image = Nothing
        MessageBox.Show("Data Deleted successfully")
        display_data()
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim cmd2 As New MySqlCommand("Select * From names Where id=@id Order By id desc", conn)
        cmd2.Parameters.AddWithValue("id", TextBox5.Text.Trim)
        Dim da As New MySqlDataAdapter(cmd2)
        Dim dt As New DataTable
        dt.Clear()
        da.Fill(dt)
        DataGridView1.DataSource = dt
    End Sub
End Class

 

Fill dataGridview from Excel's sheet depend on comBobox in C# with source code

 




using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.OleDb;

namespace excel_import

{

    public partial class Form6 : Form

    {

        public Form6()

        {

            InitializeComponent();

        }

        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Ace.Oledb.12.0;" +

            @"Data Source=F:names2.Xlsx;Extended Properties = 'Excel 8.0;HDR=Yes'");

        private void Form6_Load(object sender, EventArgs e)

        {

            conn.Open();

            comboBox1.DataSource = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            comboBox1.DisplayMember = "Table_Name";

            comboBox1.SelectedIndex = -1;

            comboBox1.SelectedText = "Select sheet";

            conn.Close();

        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {


        }


        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)

        {

            OleDbCommand cmd = new OleDbCommand("Select * From [" + comboBox1.GetItemText(comboBox1.SelectedItem) + "]", conn);

            OleDbDataAdapter da = new OleDbDataAdapter(cmd);

            DataTable dt = new DataTable();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

        }

    }

}


With source code Excel macro VBA tutorial| insert update delete in sheet excel using entry form

 



Private Sub CommandButton1_Click()

Dim sheet_v As Worksheet

Set sheet_v = ThisWorkbook.Sheets("sheet1")

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

sheet_v.Range("A" & lrow_v + 1).Value = TextBox1.Value

sheet_v.Range("B" & lrow_v + 1).Value = TextBox2.Value

sheet_v.Range("C" & lrow_v + 1).Value = TextBox3.Value

sheet_v.Range("D" & lrow_v + 1).Value = TextBox4.Value

TextBox1.Value = ""

TextBox2.Value = ""

TextBox3.Value = ""

TextBox4.Value = ""

'MsgBox ("Data inserted successfully")

Call load_data

End Sub

Sub load_data()

Dim sheet_v As Worksheet

Set sheet_v = ThisWorkbook.Sheets("sheet1")

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

sheet_v.Range("A" & lrow_v + 1).Value = TextBox1.Value

With ListBox1

.ColumnCount = 4

.ColumnHeads = True

.ColumnWidths = "70,90,90,90"

.RowSource = "sheet1!A2:D" & lrow_v

End With

End Sub


Private Sub CommandButton2_Click()

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

Dim i As Long

For i = 2 To lrow_v

If Sheets("sheet1").Cells(i, 1).Value = id.Text Then

Sheets("sheet1").Cells(i, 1).Value = TextBox1.Text

Sheets("sheet1").Cells(i, 2).Value = TextBox2.Text

Sheets("sheet1").Cells(i, 3).Value = TextBox3.Text

Sheets("sheet1").Cells(i, 4).Value = TextBox4.Text

End If

Next i

End Sub


Private Sub CommandButton3_Click()

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

Dim i As Long

For i = 2 To lrow_v

If Sheets("sheet1").Cells(i, 1).Value = id.Text Then

Rows(i).Delete

End If

Next i

TextBox1.Value = ""

TextBox2.Value = ""

TextBox3.Value = ""

TextBox4.Value = ""

id.Value = ""

End Sub


Private Sub ListBox1_Click()


End Sub


Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

TextBox1.Value = ListBox1.Column(0)

TextBox2.Value = ListBox1.Column(1)

TextBox3.Value = ListBox1.Column(2)

TextBox4.Value = ListBox1.Column(3)

id.Value = ListBox1.Column(0)

End Sub


Private Sub UserForm_Click()


End Sub


Private Sub UserForm_Initialize()

Call load_data

End Sub


Important videos c#

Programming C# : Connect SQL server database with Visual Studio C#  with source code

https://youtu.be/8_W0pEZawYg

c# tutorial for beginners - insert update delete search in sql server database and print (With code)

https://youtu.be/mSW7FFzdQMA

Programming C#: insert, update and  delete  data in datagridview without using database

https://youtu.be/Uoz4NS15lj8

C#: update all data from datagridview to database at once

https://youtu.be/_yansR7fE7o

c# tutorial for beginners: How to store images  update and delete in sql database

https://youtu.be/AwvrH8QK7DM

C# Tutorial : Retrieve data from Sql server database

https://youtu.be/lR_Ic_u8e90

C# Tutorial import data from Excel to SQL server

https://youtu.be/NUE-Sgq6SOg

c# tutorial for beginners: Add Row Total To DataGridView Footer

https://youtu.be/aL7wIUSfb-Q

c# tutorial for beginners: Insert Only Checked rows from datagridview into SQL database

https://youtu.be/XZEXOfcue8w

C#: Get data in datagridview from two tables using inner join and (Left - right - full) outer join.

https://youtu.be/eZQmFkL7Ie8

c# tutorial for beginners: Send data from datagridview to crystalreport without database in C#

https://youtu.be/TX-r99vKM3o

c# tutorial for beginners: Print data from dataGridView In C#

https://youtu.be/7EtsdSB72p4

c# tutorial for beginners: How to connect Microsoft access database to C#

https://youtu.be/wRzOkkptwVc

c# tutorial for beginners: How to get the sum of checked RadioButton Values

https://youtu.be/-lURcHfH7Qo

How to get selected text and selected value of comboBox in C#

https://youtu.be/18Z9v7jkERw

c# tutorial for beginners: Retrieve data from access database and navigation buttons

https://youtu.be/pQlhabZPZSU

c# tutorial for beginners: Delete row from datagridview and sql server database at once in C#

https://youtu.be/SUFVWlJGt8E

c# tutorial for beginners - How to search multiple columns access database using one textBox in C#

https://youtu.be/Ni4IDcufTos

C# Tutorial - message box exit application

https://youtu.be/jVNGCBQlXlI

c# tutorial for beginners - How to get the selected items in the combobox and show them in a listbox

https://youtu.be/q-FDCz7u1DM

C#: datagridview change cell backcolor based on value

https://youtu.be/lVYvDJn-r9E

c# tutorial for beginners - How to make validation on textbox in c#

https://youtu.be/wcme5LmppU8

How to fill combobox from sql server database and get selected value from a combobox in c#

https://youtu.be/ti_zVHaRT_U

How to search data in datagridview in c# without using database

https://youtu.be/iuDRFIXvKcg

c# tutorial for beginners - Insert all data of a dataGridView to access database at once  with  code

https://youtu.be/l0pPXY5shjw

Programming C#:  How to prevent duplicate data when insert to datagridview

https://youtu.be/6Ea1XGTjFnI

C#: Search for value in DataGridView in a column(without database)

https://youtu.be/1pkcAnZNAPE

C#:Show and hide characters password

https://youtu.be/mEwFk-46l7s

Programming in C # tutorial:  Login Form in C # with SQL Server with source code

https://youtu.be/NyTnhCsYlGQ 

C# tutorial| connect with access database with password to retrieve data into textboxes -source code



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.OleDb;


namespace connect_access_with_password

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            OleDbConnection oledconn = new OleDbConnection(@"Provider=Microsoft.Ace.Oledb.12.0;Data Source=F:\ddd.accdb;Jet Oledb:Database Password=123456");

            oledconn.Open();

            OleDbCommand olecomm = new OleDbCommand("Select firstname,lastname,points from table1 Where id = @id", oledconn);

            olecomm.Parameters.AddWithValue("id", textBox1.Text);

            OleDbDataReader oledreader;

            oledreader = olecomm.ExecuteReader();

            if (oledreader.Read())

            {

                textBox2.Text = oledreader["firstname"].ToString();

                textBox3.Text = oledreader["lastname"].ToString();

                textBox4.Text = oledreader["points"].ToString();

            }

            else

            {

                textBox2.Text = "";

                textBox3.Text = "";

                textBox4.Text = "";

                MessageBox.Show("No data found");

                


            }

            oledconn.Close();

        }

    }