Change DataGridView Columns Title with Database Using C#

Change DataGridView Columns Title with Database Using C#

In this article, I will show you how to change column title in DataGridView Columns, let’s follow the tutorial below

Step 1:

Create Project And Add Reference To C# WinForms Project. (visit the link to the first tutorial on how to add reference to our C# project)

How to connect MySQL Database to C# Tutorial and Source code

Step 2:

Open MySQL Workbench, right click and create schema (new database), give database name as “sampledb” and create table in database and give a name as “information”, then create columns id, emp_id, name, designation, username and password and then insert data into table.

visit the link below on the tutorial on how to insert record in mysql and c#

Insert or Save Data into MySQL Database using C#

Change DataGridView Columns Title with Database Using C# - Step 2
Change DataGridView Columns Title with Database Using C# – Step 2

Steps 3:

Back to windows form application and design forms like this (see image below)

Add DataGridView tools

  • Dtuser
Change DataGridView Columns Title with Database Using C# - Form Design
Change DataGridView Columns Title with Database Using C# – Form Design

Code for form LOAD (Form1) events:

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 MySql.Data.MySqlClient;

namespace changecolumnnamedtview
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {       
           dtuser.DataSource = null;
           MySqlConnection con = new MySqlConnection("datasource= localhost; database=sampledb;port=3306; username = root; password= db1234");
           MySqlDataAdapter adapter = new MySqlDataAdapter("select emp_id'EMP_ID',name'NAME',designation'DESIGNATION',username'USERNAME',password'PASSWORD' from information", con);
           DataTable dt = new DataTable();
            adapter.Fill(dt);
            dtuser.DataSource = dt;
            con.Close();    
        }
    }
}

Results:

Change DataGridView Columns Title with Database Using C# - Output
Change DataGridView Columns Title with Database Using C# – Output

Code Explanation:

This code explain on how to change column header of DataGridView Columns using MySQL query

Change DataGridView Columns Title with Database Using C# Free Download Source code

Mark Jaylo

markjaylo26@gmail.com

YTC: https://www.youtube.com/c/MarkTheProgrammer

You may visit our facebook page for more information, inquiries and comments.

Hire our team to do the project.

, ,

Post navigation