1. public Form1()
  2. {
  3. InitializeComponent();
  4.  
  5. //连接数据库
  6. string str = "Data Source=IP;Initial Catalog=数据库名称;Persist Security Info=True;User ID=**; Password=";
  7. ConnectDatebase(str);
  8. }
  9.  
  10. void ConnectDatebase(string sql)
  11. {
  12. SqlConnection connection = new SqlConnection();
  13. connection.ConnectionString = sql;
  14. connection.Open();
  15.  
  16. SqlDataAdapter adapter = new SqlDataAdapter("select *from steelname", connection);
  17. DataSet dsMain = new DataSet();
  18. adapter.Fill(dsMain, "steelname");
  19. this.dataGridView1.DataSource = dsMain;
  20. this.dataGridView1.DataMember = "steelname";
  21. }

对于数据库的更新操作

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9. using System.Data.Common;
  10.  
  11. namespace DateManagerTools
  12. {
  13. public partial class Form1 : Form
  14. {
  15. private DataSet dsMain;
  16. private SqlDataAdapter adapter;
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. private SqlConnection getConnection()
  23. {
  24. SqlConnection connection = new SqlConnection();
  25. connection.ConnectionString = "Data Source=IP;Initial Catalog=数据库名称;Persist Security Info=True;User ID=**; Password=";
  26. return connection;
  27. }
  28.  
  29. private void Form1_Load(object sender, EventArgs e)
  30. {
  31. InitAdapter();
  32. getData();
  33. BindingControl();
  34. }
  35.  
  36. /// <summary>
  37. /// 初始化adapter变量
  38. /// </summary>
  39. private void InitAdapter()
  40. {
  41. SqlConnection connection = this.getConnection();
  42. adapter = new SqlDataAdapter("select * from steelname", connection);
  43. adapter.FillLoadOption = LoadOption.OverwriteChanges;
  44. //新增
  45. SqlCommand InsertCommand = new SqlCommand();
  46. InsertCommand.Connection = connection;
  47. InsertCommand.CommandText = "insert into steelname,Name) values(@ID,@Code,@Name)";
  48. InsertCommand.Parameters.Add("@ID", SqlDbType.Int, , "ID");
  49. InsertCommand.Parameters.Add("@Code", SqlDbType.Char, , "Code");
  50. InsertCommand.Parameters.Add("@Name", SqlDbType.VarChar, , "Name");
  51. adapter.InsertCommand = InsertCommand;
  52. //修改
  53. SqlCommand UpdateCommand = new SqlCommand();
  54. UpdateCommand.Connection = connection;
  55. UpdateCommand.CommandText = "update steelname set Code=@Code,Name=@Name where ID=@ID";
  56. UpdateCommand.Parameters.Add("@ID", SqlDbType.Int, , "ID");
  57. UpdateCommand.Parameters.Add("@Code", SqlDbType.Char, , "Code");
  58. UpdateCommand.Parameters.Add("@Name", SqlDbType.VarChar, , "Name");
  59. adapter.UpdateCommand = UpdateCommand;
  60. //删除
  61. SqlCommand DeleteCommand = new SqlCommand();
  62. DeleteCommand.Connection = connection;
  63. DeleteCommand.CommandText = "delete steelname where steelname_id=@steelname_id";
  64. DeleteCommand.Parameters.Add("@steelname_id", SqlDbType.Int, , "steelname_id");
  65. adapter.DeleteCommand = DeleteCommand;
  66. //添加表映射
  67. //DataTableMapping TableMapping = new DataTableMapping();
  68. //TableMapping = adapter.TableMappings.Add("Users", "Users");
  69. //TableMapping.ColumnMappings.Add("Code", "Code");
  70. //TableMapping.ColumnMappings.Add("Name", "Name");
  71. //TableMapping.DataSetTable = "SteelName";
  72. }
  73.  
  74. /// <summary>
  75. /// 把控件绑定到数据源
  76. /// </summary>
  77. private void BindingControl()
  78. {
  79. this.dataGridView1.DataSource = dsMain;
  80. this.dataGridView1.DataMember = "steelname";
  81. //this.dataGridView1.Columns[0].Width = 40;
  82. //this.txtID.DataBindings.Add("Text", dsMain, "Users.ID");
  83. //this.txtCode.DataBindings.Add("Text", dsMain, "Users.Code");
  84. //this.txtName.DataBindings.Add("Text", dsMain, "Users.Name");
  85. }
  86.  
  87. /// <summary>
  88. /// 从Sql Server中获取数据
  89. /// </summary>
  90. private void getData()
  91. {
  92. if (dsMain == null)
  93. {
  94. dsMain = new DataSet();
  95. }
  96. else
  97. {
  98. dsMain.Clear();
  99. }
  100. adapter.Fill(dsMain, "steelname");
  101. }
  102.  
  103. //新增
  104. private void button1_Click(object sender, EventArgs e)
  105. {
  106. this.BindingContext[dsMain, "steelname"].AddNew();
  107. this.BindingContext[dsMain, "steelname"].EndCurrentEdit();//结束编译
  108. //this.txtCode.Focus();
  109. }
  110.  
  111. //删除
  112. private void button2_Click(object sender, EventArgs e)
  113. {
  114. if (this.BindingContext[dsMain, "steelname"].Position > -)
  115. {
  116. if (MessageBox.Show("是否要删除此记录?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  117. {
  118. this.BindingContext[dsMain, "steelname"].RemoveAt(this.BindingContext[dsMain, "steelname"].Position);
  119. Save();
  120. }
  121. }
  122. }
  123.  
  124. //保存
  125. private void button3_Click(object sender, EventArgs e)
  126. {
  127. this.BindingContext[dsMain, "steelname"].EndCurrentEdit();
  128. Save();
  129. }
  130.  
  131. //刷新
  132. private void button4_Click(object sender, EventArgs e)
  133. {
  134. getData();
  135. }
  136.  
  137. private void Save()
  138. {
  139. try
  140. {
  141. adapter.Update(dsMain, "steelname");
  142. }
  143. catch (Exception ex)
  144. {
  145. MessageBox.Show(ex.Message);
  146. }
  147. }
  148. }
  149. }

表中的某一列有ComBox绑定在一起:

  1. this.comboBox1.DataSource = dsMain.Tables[]; //tables[0]第一列
  2. comboBox1.DisplayMember = "steeltype_id";
  3. comboBox1.ValueMember = "steeltype_id";

c# 数据库数据与DataGridView表控件的绑定的更多相关文章

  1. 将数据库数据添加到ListView控件中

    实现效果: 知识运用: ListView控件中的Items集合的Clear方法 //从listView控件的数据项集合中移除所有数据项 补充:可以使用Remove或RemoveAt方法从集合中移除单个 ...

  2. 在Bootstrap开发框架中使用dataTable直接录入表格行数据(2)--- 控件数据源绑定

    在前面随笔<在Bootstrap开发框架中使用dataTable直接录入表格行数据>中介绍了在Web页面中使用Jquery DataTable插件进行对数据直接录入操作,这种处理能够给用户 ...

  3. WinForm控件复杂数据绑定常用数据源(对Combobox,DataGridView等控件DataSource赋值的多种方法)

    开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1) 简单数据绑定 简单的数据绑定是将用户控件的某一个属性绑定至某一个类型实例上的某一属性.采用如下形式进行绑定 ...

  4. TreeView树形控件递归绑定数据库里的数据

    TreeView树形控件递归绑定数据库里的数据. 第一种:性能不好 第一步:数据库中查出来的表,字段名分别为UNAME(显示名称),DID(关联数据),UTYPE(类型) 第二步:前台代码 <% ...

  5. ASP.NET中后台数据和前台控件的绑定

    关于ASP.NET中后台数据库和前台的数据控件的绑定问题 最近一直在学习个知识点,自己创建了SQL Server数据库表,想在ASP.NET中连接数据库,并把数据库中的数据显示在前台,注意,这里的数据 ...

  6. ABAP表控件查询

    1.准备工作 首先SE11自建一个数据库表(数据元素,域信息请提前建好) 2.编写代码 2.1 新建一个子屏幕 子屏幕中需新定义一个文本输入框,命名为:key_word,新建一个表控件,命名为tab, ...

  7. C#端加载数据库,Combobox与Node控件绑定数据源demo示例

    最近一直在做网页.用的js比较多,最近需要做一个C#相关的demo,一开始还有点不适应,写了几句有点感觉了 本篇博客的主要内容是C#怎么读取数据库文件里的数据以及相关控件如何绑定数据源,所做的Demo ...

  8. Atitit..组件化事件化的编程模型--(2)---------Web datagridview 服务器端控件的实现原理and总结

    Atitit..组件化事件化的编程模型--(2)---------Web datagridview 服务器端控件的实现原理and总结 1. 服务端table控件的几个流程周期 1 1.1. 确认要显示 ...

  9. Atitit. BigConfirmTips 控件 大数据量提示确认控件的原理and总结O9

    Atitit. BigConfirmTips 控件 大数据量提示确认控件的原理and总结O9 1. 主要的涉及的技术 1 2. 主要的流程 1 3. 调用法new confirmO9t(); 1 4. ...

随机推荐

  1. C# 字典 Dictionary 遍历

    using System; using System.Collections.Generic; public class Example { public static void Main() { / ...

  2. 第二百九十八节,python操作redis缓存-Set集合类型,可以理解为不能有重复元素的列表

    python操作redis缓存-Set集合类型,可以理解为不能有重复元素的列表 sadd(name,values)name对应的集合中添加元素 #!/usr/bin/env python # -*- ...

  3. e667. 在给定图像中创建缓冲图像

    An Image object cannot be converted to a BufferedImage object. The closest equivalent is to create a ...

  4. e661. 确定图像中是否有透明像素

    // This method returns true if the specified image has transparent pixels public static boolean hasA ...

  5. win7语音识别开发(sapi)

    参考:http://msdn.microsoft.com/en-us/library/ee125663(v=vs.85).aspx    (sapi5.4 reference) http://msdn ...

  6. 使用什么工具连接MySQL Server

    字符界面:命令行终端(需MySQL Client) GUI界面:Navicat.MySQL Workbench 开发语言:使用相应语言的MySQL数据库驱动包或模块连接MySQL 我一般用的是命令行, ...

  7. C++字符串类型和数字之间的转换

    转载:http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html 1.字符串数字之间的转换 字符串---字符数组(1)string ...

  8. 反编译工具 jad

    JAD(Java Decompiler)是一个比较流行的Java反编译工具,可以从网站 http://www.varaneckas.com/jad/ 下载,有多个系统下的应用程序,包括Windows. ...

  9. SVN目录权限配置

    1.如果要使用SVN,需要有一个项目的保存目录,例如把该目录设为“C:\MyPro”文件夹 2.把该目录发布为SVN项目目录,则需要通过以下命令行 svnadmin create c:\mypro  ...

  10. DM8168 PWM驱动与測试程序

    昨天把DM8168的Timer设置给摸了一遍,为写PWM的底层驱动做好了准备,如今就要进入主题了. dm8168_pwm.c: #include <linux/module.h> #inc ...