对DataGridView数据控件进行绑定应用实例
对DataGridView数据控件进行绑定
将数据绑定到DataGridView控件的操作非常简单和直观,在大多数情况下,只需设置Data Source属性即可。DataGridView控件支持标准Windows窗体数据绑定模型,因此可绑定到各种数据源,如一维数组、DataTable、DataSet和BindingList等类中。但在多数情况下,绑定到一个BindingSource组件,由该组件来管理与数据源交互的详细信息。下面通过一个简单的示例来介绍如何利用BindingSource组件对DataGridView数据控件进行绑定。
示例
对DataGridView数据控件进行绑定
本示例主要是利用BindingSource组件获取数据源信息,然后将DataGridView控件绑定到BindingSource组件上,示例运行结果如图1所示。

图1 对DataGridView数据控件进行绑定
程序代码如下。
在Form1窗体的Load事件下,调用自定义方法GetData为BindingSource组件附加数据源,并将DataGridView控件绑定到BindingSource组件上,其代码如下:
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bindingSource1;
GetData("Select * From tb_Stu");
}
自定义方法GetData,首先对SqlDataAdapter类进行初始化,并填充数据表DataTable,然后,将DataTable绑定到BindingSource组件,其代码如下:
private void GetData(string selectCommand)
{
try
{
string connectionString = "server=TIESQLEXPRESS ;DataBase=db_22; user ID=sa ; password=''";
SqlConnection myConn = new SqlConnection(connectionString);
SqlDataAdapter myAdapter=new SqlDataAdapter(selectCommand, connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(myAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
myAdapter.Fill(table);
bindingSource1.DataSource = table;
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCells);
}
catch (SqlException)
{
MessageBox.Show("获取数据失败!", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
System.Threading.Thread.CurrentThread.Abort();
}
}
完整程序代码如下:
★ ★★★★Program.cs主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _2_07
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace _2_07
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bindingSource1;
GetData("Select * From tb_Stu");
}
private void GetData(string selectCommand)
{
try
{
string connectionString = "server=TIESQLEXPRESS ;DataBase=db_22; user ID=sa ; password=''";
SqlConnection myConn = new SqlConnection(connectionString);
SqlDataAdapter myAdapter = new SqlDataAdapter(selectCommand, connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(myAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
myAdapter.Fill(table);
bindingSource1.DataSource = table;
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCells);
}
catch (SqlException)
{
MessageBox.Show("获取数据失败!", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
System.Threading.Thread.CurrentThread.Abort();
}
}
}
}
★ ★★★★Form1.Designer.cs窗体设计文件完整程序代码★★★★★
namespace _2_07
{
partial class Form1
{
///
/// 必需的设计器变量。
///