Home Database Mysql Tutorial 关于DataGridView控件通过dataset直接更新数据库

关于DataGridView控件通过dataset直接更新数据库

Jun 07, 2016 pm 03:06 PM
datagridview dataset about control pass

今天做了一个小功能,对一个数据库表中的信息直接进行增、删、改操作,用到了DataGridView控件。 功能描述:窗体加载时,查询出表中的内容,并绑定到DataGridView中显示。可以对表中的数据进行增/删、改操作。 开始用到了比较麻烦的方法,为DataGridView添加

今天做了一个小功能,对一个数据库表中的信息直接进行增、删、改操作,用到了DataGridView控件。
功能描述:窗体加载时,查询出表中的内容,并绑定到DataGridView中显示。可以对表中的数据进行增/删、改操作。
  开始用到了比较麻烦的方法,为DataGridView添加了contextMenuStrip,提供了三种方法。比较麻烦。下面是一部分代码:

1 privatevoid 删除ToolStripMenuItem_Click(object sender, EventArgs e)
2 {
3 int[] row =newint[dataGridView1.SelectedRows.Count]; //存储选定的行的索引号
4  int[] id =newint[dataGridView1.SelectedRows.Count]; //存储选定行的id号 用于删除时查询的条件
5  int count = dataGridView1.SelectedRows.Count; //存储开始选定了的行数
6  for (int j =0; j dataGridView1.SelectedRows.Count; j++) //利用循环将选定的行索引存入row【】数组中
7   {
8 row[j] = dataGridView1.SelectedRows[j].Index;
9 }
10 for (int i =0; i count; i++) //利用循环
11 {
12
13 dataGridView1.CurrentCell = dataGridView1[0, row[i]]; //设置当前活动的单元格的索引是【0,row【i】】,即所有选定行的第一列的值(Att_Id)
14 id[i] =Convert.ToInt32(dataGridView1.CurrentCell.Value);
15
16 }
17 bool yesno=false;
18 if (MessageBox.Show("确定要删除吗","删除确认",MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK)
19 {
20 for (int s =0; s count; s++)
21 {
22 string sql ="Delete from Attributes where Att_Id=@att_id";
23 SqlParameter[] p =new SqlParameter[]{
24 new SqlParameter("@att_id",SqlDbType.Int)};
25 p[0].Value = id[s];
26 int val = (int)sqlhelper.delete(sql, p);
27 if (val >0)
28 {
29 yesno =true;
30 }
31 else
32 {
33 yesno =false;
34 break;
35 }
36
37 }
38 if (yesno ==true)
39 {
40 MessageBox.Show("删除成功!");
41 }
42 databind();
43 }
44 else
45 {
46
47 }
48 }

上面仅仅是删除这一项功能。所以非常麻烦!!

    于是,想到了另一种比较简单而且很直观的方法,利用SqlDataAdapter的update方法,直接在DataGridView中进行编辑(添加新数据、更改数据),只需给删除功能另加个按钮或者弹出菜单,datagridview1.Rows.RemoveAt(dataGridView1.CurrentRow.Index)删除选中行,有一点每种不足就是没删除一条就要点击一下按钮,而第一种方法可以一次性删除多条数据。

关于DataGridView控件通过dataset直接更新数据库关于DataGridView控件通过dataset直接更新数据库View Code

1 privatevoid leixing_Load(object sender, EventArgs e)
2 {
3
4 sqlhelper.conn.Open();
5
6 da =new SqlDataAdapter("Select Class_Id as '编号',Class_Type as '商品类型' from Class", sqlhelper.conn);
7 dt =new DataSet();
8 da.Fill(dt, "info");
9 dataGridView1.DataSource = dt.Tables[0];
10 //dataGridView1.DataMember = "info";
11 dataGridView1.Columns[0].Width =130;
12
13
14 sqlhelper.conn.Close();
15
16 }

再添加一个按钮叫做“保存更新” 代码:

这样就实现了全部功能。比较简单。

关于DataGridView控件通过dataset直接更新数据库关于DataGridView控件通过dataset直接更新数据库View Code

1 privatevoid baocun_Click(object sender, EventArgs e)
2 {
3
4
5 if (dt.HasChanges())
6 {
7 SqlCommandBuilder scmb =new SqlCommandBuilder(da);
8 da.Update(dt.Tables[0]);
9 dt.AcceptChanges();
10 }
11
12
13
14 }

这样做的前提是,数据表必须设定主键。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Common rich text editor controls in Go language web development framework Common rich text editor controls in Go language web development framework Jun 04, 2023 am 09:10 AM

With the popularity of web applications, rich text editors have become an indispensable tool in web development. When using Go language for web development, we also need to choose a suitable rich text editor control to enrich our websites and applications. In this article, we will discuss common rich text editor controls in Go language web development. FroalaEditorFroalaEditor is a popular rich text editor control that is widely used in web development. it has modernity

How to make excel controls How to make excel controls Mar 20, 2024 am 09:40 AM

When we use the excel office software, if we can use some controls skillfully, it can help us create more professional effects in the excel form. For example, adding selection controls can make the form filler easily complete the form. Below, we will demonstrate how to make an excel selection control. We hope it will be helpful to you! 1. First, we create and open a blank excel table. 2. Add the "Development Tools" tab, click the file button on the upper left side, and find "Excel Options". After that, we find the development tools in the options of the custom ribbon and check it so that a check mark appears in front of it. 3. Return to the Excel work interface and you can see the "Development Tools" tab. Generally speaking, it is not

How to use panel control How to use panel control Oct 10, 2023 am 09:36 AM

The steps to use the panel control are to first create a Panel control and set its width, height, background color, border color, border width and padding, create two buttons and add them to the Panel control, and finally Add the Panel control to the form.

What are the common Ajax controls? Learn more about its features and capabilities What are the common Ajax controls? Learn more about its features and capabilities Jan 17, 2024 am 11:11 AM

In-depth understanding of Ajax controls: What are the common ones? Introduction: In modern Web development, Ajax (Asynchronous JavaScript and XML) has become a popular technology, which can realize partial refresh of web pages and improve user experience. In development, we usually use Ajax controls to simplify and speed up our development process. This article will take an in-depth look at Ajax controls and introduce some common controls and their functions. 1. jQueryAjax: jQueryA

ColorOS15 interface exposed. About this machine, here is a big change ColorOS15 interface exposed. About this machine, here is a big change Aug 28, 2024 pm 03:31 PM

Recently, ColorOS15 took the lead in launching internal beta testing. Some netizens exposed the relevant interface. Let’s see how it goes. As you can see in the picture above, some netizens posted the “About This Machine” interface of OPPO Find X7 after upgrading ColorOS15Beta. In addition to the big change in the top pattern, The configuration information in the lower half has also changed from the previous two columns to a single column vertical distribution. Attached is the "About This Phone/Mobile Phone" interface in the latest versions of mobile phones from six brands: Huawei, Honor, Xiaomi, OPPO, vivo, and Meizu. You can tell me which layout you like better. Regarding ColorOS 15, previous news said In addition to supporting LivePhoto live photos on a wide scale, it will also "support AirDrop&

Revealing the Essential Web Standard Controls: In-depth Analysis of Interface Elements Revealing the Essential Web Standard Controls: In-depth Analysis of Interface Elements Jan 13, 2024 pm 12:19 PM

Web Standard Controls Revealed: Essential Interface Elements Analysis Introduction: Web standard controls are an indispensable part of web design. They provide users with a rich interactive experience and a friendly interface, and play an important role. This article will reveal several essential Web standard controls and analyze their functions and usage methods for readers. 1. Button control Button control is one of the most common and basic elements in Web interface design, used to trigger certain operations. It usually has the following attributes: Text content: the text information displayed on the button. style

Solve the problem of environment detection when reinstalling the system Solve the problem of environment detection when reinstalling the system Jan 08, 2024 pm 03:33 PM

How to solve the problem that the environment test fails when reinstalling the system and needs to be rewritten. The reason is: the mobile phone is poisoned. You can install anti-virus software such as Mobile Manager for anti-virus. 2. Many junk files are stored inside the mobile phone, causing the running memory of the mobile phone to be occupied. Just clear the phone cache to solve this problem. 3. The phone memory is occupied too much by saved software and files. It is no problem to delete unnecessary files and software frequently. As long as your hardware configuration meets the installation requirements, you can use the new one directly. Reinstall the system from the system disk! You can use a USB flash drive or hard disk to install, which is very fast. But the key is to use a system disk with good compatibility (supports installation in IDE, ACHI, and RAID modes), and it can be automatically and permanently activated, which has been verified. so

What is a control What is a control Oct 28, 2022 pm 03:51 PM

Controls refer to the encapsulation of data and methods; controls can have their own properties and methods, where properties are simple visitors to the control data, and methods are some simple and visible functions of the control. The control creation process includes design, development, and debugging ;The greatest significance of creating controls is to encapsulate repetitive work, and secondly, to expand the functions of existing controls.

See all articles