Binding a Data Grid View



This is an example of binding data of a table to a data grid view. Of course, you must have a database:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DataGridViewBinding
    public partial class Exercise : Form
        public Exercise()

        private void btnLoad_Click(object sender, EventArgs e)
            using (SqlConnection conExercise = 
			new SqlConnection("Data Source=(local);" +
                                          "Database='Exercise';" +
                                          "Integrated Security=SSPI;"))
                string strExercise = "SELECT * FROM Employees";
                SqlCommand cmdExercise = new SqlCommand(strExercise, conExercise);
                SqlDataAdapter sdaExercise = new SqlDataAdapter(cmdExercise);
                DataSet dsExercise = new DataSet("ExerciseSet");

                dgvExercise.DataSource = dsExercise;
                dgvExercise.DataMember = dsExercise.Tables[0].TableName;



Home Copyright © 2008-2010 FunctionX, Inc.