All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
利用PHP的OOP特性实现数据保护_php资料_编程技术
让SQL程序更快的运行:语句优化分析_[SQL,Server教程]
探讨ASP.NET,2.0中的Web控件改进技术_[Asp.Net教程]
怎样正确理解.NET和SQL,Server中“空值”_.net资料_编程技术
ASP.NET,2.0,多文件上传小经验_[Asp.Net教程]
PHP获取远程图片并把它保存到本地_[PHP教程]
C#,中的常用正则表达式总结_[Asp.Net教程]
PHP实例程序:用PHP制作登录页面程序_php资料_编程技术
ASP.NET入门数据篇_[Asp.Net教程]
PHP,会话
Reading number is top 10 pictures
Summer is most suitable for young people to travel in China2
这才叫绝色美女1
Fan bingbing black wings for platform and DanLuoWang believes beauty1
Perfect small Laurie2
So beauty, will let you spray blood8
Female model behind the bitterness, often being overcharged5
In the world the most mysterious 21 place landscape4
The money of more than 100 countries and regions10
关于提肛的健身效果
大人物的礼物
Download software ranking
C++教程第四版
Boxer vs Yellow1
Macromedia Dreamweaver 8
Desire a peach blossom
C#程序员参考手册
matrix1
Twenty piece of palm leaf
Boxer Classic video2
株洲本地在线棋牌游戏
美女写真2
qq published in(发表于) 2014/7/9 1:40:22 Edit(编辑)
对DataGridView数据控件进行绑定应用实例

对DataGridView数据控件进行绑定应用实例

对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

{

///

/// 必需的设计器变量。

///



添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.