DataGridView使用实例1
来源:互联网 发布:最好的网络医院平台 编辑:程序博客网 时间:2024/06/06 09:26
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
public class Form1 : System.Windows.Forms.Form
{
private DataGridView dataGridView1 = new DataGridView();
private BindingSource bindingSource1 = new BindingSource();
private SqlDataAdapter dataAdapter = new SqlDataAdapter();
private Button reloadButton = new Button();
private Button submitButton = new Button();
[STAThreadAttribute()]
public static void Main()
{
Application.Run(new Form1());
}
// Initialize the form.
// 初始化窗体
public Form1()
{
dataGridView1.Dock = DockStyle.Fill;
reloadButton.Text = "刷新";
submitButton.Text = "提交";
reloadButton.Click += new System.EventHandler(reloadButton_Click);
submitButton.Click += new System.EventHandler(submitButton_Click);
FlowLayoutPanel panel = new FlowLayoutPanel();
panel.Dock = DockStyle.Top;
panel.AutoSize = true;
panel.Controls.AddRange(new Control[] { reloadButton, submitButton });
this.Controls.AddRange(new Control[] { dataGridView1, panel });
this.Load += new System.EventHandler(Form1_Load);
this.Text = "DataGridView databinding and updating demo";
}
private void Form1_Load(object sender, System.EventArgs e)
{
// Bind the DataGridView to the BindingSource
// and load the data from the database.
// 绑定 DataGridView 到 BindingSource 并从 database 中装入数据。
dataGridView1.DataSource = bindingSource1;
GetData("select * from Customers");
}
private void reloadButton_Click(object sender, System.EventArgs e)
{
// Reload the data from the database.
// 从database中刷新数据。
GetData(dataAdapter.SelectCommand.CommandText);
}
private void submitButton_Click(object sender, System.EventArgs e)
{
// Update the database with the user's changes.
// 将用户变更数据更新到database。
dataAdapter.Update((DataTable)bindingSource1.DataSource); //这样定通用性更好。
}
private void GetData(string selectCommand)
{
try
{
// Specify a connection string. Replace the given value with a
// valid connection string for a Northwind SQL Server sample
// database accessible to your system.
// 1. 准备连接字符串
String connectionString = "Data Source=124.224.220.190;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sd12345678";
// Create a new data adapter based on the specified query.
// 2. 建立一个基于指定查询的数据适配器(adapter)。
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
// 3. 建立命令工厂(SqlCommandBuilder)。
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
// 4. 移民一个新表并绑定到 绑定源BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
// Resize the DataGridView columns to fit the newly loaded content.
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (SqlException)
{
MessageBox.Show("To run this example, replace the value of the " +
"connectionString variable with a connection string that is " +
"valid for your system.");
}
}
}
- DataGridView使用实例1
- DataGridView使用实例2
- DataGridView实例
- DataGridView使用
- DataGridView使用
- DataGridView使用
- DataGridView 使用
- datagridview的一些编程实例
- WINFORM 使用datagridview取值时出现"未将对象引用设置到对象的实例
- dataGridView使用模版列
- DataGridView控件的使用
- DataGridView控件的使用
- DataGridView控件使用
- DataGridView的使用
- 使用DataGridView更新数据库
- DataGridView集成Combox使用
- DataGridView使用小结
- DataGridView控件的使用
- C#代码规范 程序员必备的秘笈
- 软件架构的过程
- wireshark for mac
- 触发器例子,insert into all的用法
- 字符串截取-笔试题
- DataGridView使用实例1
- [转] FLAC vs APE (二)
- .NET企业级架构解决方案:架构师和架构
- 软件构架师的特点
- QT获取本地ip
- 新来的!
- RGB "Bayer" Color and MicroLenses
- 对JDO的架构作一个简单的介绍
- V5Shop网店系统更新 版本V8.30.0606 RC1