Changing Column Title of DataGridView When Connecting SQL Server in Windows Application

Step 1: Form

Drag down a data GridView and a Button from the tool box.



Step 2: Code

Now, click on the Button (Load Button). And write this code.

 
 

private void Load_button_Click(object sender, EventArgs e)

{

    SqlConnection con = new SqlConnection("server = MUNESH;Database=datastore;UID=sa;Password=123;");

    SqlCommand cmd = new SqlCommand("Select Eid as 'Employee Id', Name as 'Employee name' , surname as 'Employee surname' ,Age as 'Employee surname' from data1", con);

    try

    {

        SqlDataAdapter da = new SqlDataAdapter();

        da.SelectCommand = cmd;

        DataTable dt = new DataTable();

        da.Fill(dt);

        BindingSource bsource = new BindingSource ();

        bsource .DataSource = dt;

        dataGridView1.DataSource = bsource ;

    }

    catch (Exception ec)

    {

        MessageBox.Show(ec.Message);

    }

 

Step 3: Run

Now run your application.



You can also visit my blog_munesh  to learn more.