c# 数据库数据与DataGridView表控件的绑定
public Form1()
{
InitializeComponent(); //连接数据库
string str = "Data Source=IP;Initial Catalog=数据库名称;Persist Security Info=True;User ID=**; Password=";
ConnectDatebase(str);
} void ConnectDatebase(string sql)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = sql;
connection.Open(); SqlDataAdapter adapter = new SqlDataAdapter("select *from steelname", connection);
DataSet dsMain = new DataSet();
adapter.Fill(dsMain, "steelname");
this.dataGridView1.DataSource = dsMain;
this.dataGridView1.DataMember = "steelname";
}
对于数据库的更新操作
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;
using System.Data.Common; namespace DateManagerTools
{
public partial class Form1 : Form
{
private DataSet dsMain;
private SqlDataAdapter adapter;
public Form1()
{
InitializeComponent();
} private SqlConnection getConnection()
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Data Source=IP;Initial Catalog=数据库名称;Persist Security Info=True;User ID=**; Password=";
return connection;
} private void Form1_Load(object sender, EventArgs e)
{
InitAdapter();
getData();
BindingControl();
} /// <summary>
/// 初始化adapter变量
/// </summary>
private void InitAdapter()
{
SqlConnection connection = this.getConnection();
adapter = new SqlDataAdapter("select * from steelname", connection);
adapter.FillLoadOption = LoadOption.OverwriteChanges;
//新增
SqlCommand InsertCommand = new SqlCommand();
InsertCommand.Connection = connection;
InsertCommand.CommandText = "insert into steelname,Name) values(@ID,@Code,@Name)";
InsertCommand.Parameters.Add("@ID", SqlDbType.Int, , "ID");
InsertCommand.Parameters.Add("@Code", SqlDbType.Char, , "Code");
InsertCommand.Parameters.Add("@Name", SqlDbType.VarChar, , "Name");
adapter.InsertCommand = InsertCommand;
//修改
SqlCommand UpdateCommand = new SqlCommand();
UpdateCommand.Connection = connection;
UpdateCommand.CommandText = "update steelname set Code=@Code,Name=@Name where ID=@ID";
UpdateCommand.Parameters.Add("@ID", SqlDbType.Int, , "ID");
UpdateCommand.Parameters.Add("@Code", SqlDbType.Char, , "Code");
UpdateCommand.Parameters.Add("@Name", SqlDbType.VarChar, , "Name");
adapter.UpdateCommand = UpdateCommand;
//删除
SqlCommand DeleteCommand = new SqlCommand();
DeleteCommand.Connection = connection;
DeleteCommand.CommandText = "delete steelname where steelname_id=@steelname_id";
DeleteCommand.Parameters.Add("@steelname_id", SqlDbType.Int, , "steelname_id");
adapter.DeleteCommand = DeleteCommand;
//添加表映射
//DataTableMapping TableMapping = new DataTableMapping();
//TableMapping = adapter.TableMappings.Add("Users", "Users");
//TableMapping.ColumnMappings.Add("Code", "Code");
//TableMapping.ColumnMappings.Add("Name", "Name");
//TableMapping.DataSetTable = "SteelName";
} /// <summary>
/// 把控件绑定到数据源
/// </summary>
private void BindingControl()
{
this.dataGridView1.DataSource = dsMain;
this.dataGridView1.DataMember = "steelname";
//this.dataGridView1.Columns[0].Width = 40;
//this.txtID.DataBindings.Add("Text", dsMain, "Users.ID");
//this.txtCode.DataBindings.Add("Text", dsMain, "Users.Code");
//this.txtName.DataBindings.Add("Text", dsMain, "Users.Name");
} /// <summary>
/// 从Sql Server中获取数据
/// </summary>
private void getData()
{
if (dsMain == null)
{
dsMain = new DataSet();
}
else
{
dsMain.Clear();
}
adapter.Fill(dsMain, "steelname");
} //新增
private void button1_Click(object sender, EventArgs e)
{
this.BindingContext[dsMain, "steelname"].AddNew();
this.BindingContext[dsMain, "steelname"].EndCurrentEdit();//结束编译
//this.txtCode.Focus();
} //删除
private void button2_Click(object sender, EventArgs e)
{
if (this.BindingContext[dsMain, "steelname"].Position > -)
{
if (MessageBox.Show("是否要删除此记录?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.BindingContext[dsMain, "steelname"].RemoveAt(this.BindingContext[dsMain, "steelname"].Position);
Save();
}
}
} //保存
private void button3_Click(object sender, EventArgs e)
{
this.BindingContext[dsMain, "steelname"].EndCurrentEdit();
Save();
} //刷新
private void button4_Click(object sender, EventArgs e)
{
getData();
} private void Save()
{
try
{
adapter.Update(dsMain, "steelname");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
表中的某一列有ComBox绑定在一起:
this.comboBox1.DataSource = dsMain.Tables[]; //tables[0]第一列
comboBox1.DisplayMember = "steeltype_id";
comboBox1.ValueMember = "steeltype_id";
c# 数据库数据与DataGridView表控件的绑定的更多相关文章
- 将数据库数据添加到ListView控件中
实现效果: 知识运用: ListView控件中的Items集合的Clear方法 //从listView控件的数据项集合中移除所有数据项 补充:可以使用Remove或RemoveAt方法从集合中移除单个 ...
- 在Bootstrap开发框架中使用dataTable直接录入表格行数据(2)--- 控件数据源绑定
在前面随笔<在Bootstrap开发框架中使用dataTable直接录入表格行数据>中介绍了在Web页面中使用Jquery DataTable插件进行对数据直接录入操作,这种处理能够给用户 ...
- WinForm控件复杂数据绑定常用数据源(对Combobox,DataGridView等控件DataSource赋值的多种方法)
开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1) 简单数据绑定 简单的数据绑定是将用户控件的某一个属性绑定至某一个类型实例上的某一属性.采用如下形式进行绑定 ...
- TreeView树形控件递归绑定数据库里的数据
TreeView树形控件递归绑定数据库里的数据. 第一种:性能不好 第一步:数据库中查出来的表,字段名分别为UNAME(显示名称),DID(关联数据),UTYPE(类型) 第二步:前台代码 <% ...
- ASP.NET中后台数据和前台控件的绑定
关于ASP.NET中后台数据库和前台的数据控件的绑定问题 最近一直在学习个知识点,自己创建了SQL Server数据库表,想在ASP.NET中连接数据库,并把数据库中的数据显示在前台,注意,这里的数据 ...
- ABAP表控件查询
1.准备工作 首先SE11自建一个数据库表(数据元素,域信息请提前建好) 2.编写代码 2.1 新建一个子屏幕 子屏幕中需新定义一个文本输入框,命名为:key_word,新建一个表控件,命名为tab, ...
- C#端加载数据库,Combobox与Node控件绑定数据源demo示例
最近一直在做网页.用的js比较多,最近需要做一个C#相关的demo,一开始还有点不适应,写了几句有点感觉了 本篇博客的主要内容是C#怎么读取数据库文件里的数据以及相关控件如何绑定数据源,所做的Demo ...
- Atitit..组件化事件化的编程模型--(2)---------Web datagridview 服务器端控件的实现原理and总结
Atitit..组件化事件化的编程模型--(2)---------Web datagridview 服务器端控件的实现原理and总结 1. 服务端table控件的几个流程周期 1 1.1. 确认要显示 ...
- Atitit. BigConfirmTips 控件 大数据量提示确认控件的原理and总结O9
Atitit. BigConfirmTips 控件 大数据量提示确认控件的原理and总结O9 1. 主要的涉及的技术 1 2. 主要的流程 1 3. 调用法new confirmO9t(); 1 4. ...
随机推荐
- C语言 字面量
在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(notation). 几乎所有计算机编程语言都具有对基本值的字面量表示,诸如:整数.浮点数以及字符串: 而有很多也对布尔类 ...
- 关于Unity中物理引擎的使用
物理引擎控制刚体,刚体上面贴上图片,所以看起来就是游戏运行起来的状态 物理世界计算刚体的运行状态,通过贴图显现出运算结果. 一.物理引擎 1:Unity 2D物理引擎基于Box2D封装而成;2: 物理 ...
- 全局结果集,带参数的结果集和动态结果集(struts2)
全局结果集: 当许多action都有共同的结果时,如果每个package都存在一个相同结果,会使得struts.xml比较臃肿,所以使用全局的结果集.一个包内的全局结果集可以通过包的继承而被其它包使用 ...
- HttpClient学习之 客户端HTTP编程入门
说明 本文存在的原因,是想深入的学习下HttpClient.对应的网址是: http://hc.apache.org/httpcomponents-client-4.5.x/primer.html h ...
- Java运行时,各种类型存储介绍
Java的内存分配 Java程序运行时的内存结构分成:方法区.栈内存.堆内存.本地方法栈几种. 方法区 存放装载的类数据信息,包括:基本信息:每个类的全限定名.每个类的直接超类的全限定 ...
- Linux基础回想(1)——Linux系统概述
1. 什么是操作系统?它与硬件以及其它软件之间的关系是如何的? 操作系统是控制和管理计算机系统内各种硬件和软件资源.有效组织多道程序执行的系统软件(或程序集合),是用户和计算机之间的接口.详细的说: ...
- 【python】\\u的字符编码问题
Str = "\\u559c\\u6b22\\u4e00\\u4e2a\\u4eba";Str = Str.decode("unicode-escape")
- 动态提交使用jQuery 完成ajax 文件下载----后端php
1.js代码 // Ajax 文件下载 //当不用传参时,可以将data去掉 jQuery.download = function(url, data, method){ // 获得url和data ...
- 第四章 事务(MYBatis)
一个使用 MyBatis-Spring 的主要原因是它允许 MyBatis 参与到 Spring 的事务管理中.而不是给 MyBatis 创建一个新的特定的事务管理器,MyBatis-Spring 利 ...
- 总结ASP.NET C#中经常用到的13个JS脚本代码
1.按钮前后台事件 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click ...