如何在DataGrid中进行添加、删除和修改操作

/lib//libclntsh.so: file format not recognized; treating as linker script centos7.7+oracle19.3 建库过程报错:预检查都是通过的 /oracle/product/19.3.0/db_1/lib/libasmperl19.a -Wl,--no-whole-archive -lclntsh -lclntshcore -lasmclntsh19 -lhasgen19 -lskgxp19 -locr19 -lcell19 -lirc INFO: /usr/bin/ld:/home/app/oracle/product/19.3.0/db_1/li... 阅读详情

见于好多人在优快云上问如何在DataGrid中进行添加、删除和修改操作,我最近作了如下一个例子。

 

首先,例子所用的数据库是SQL Server2000,数据库表格如下:

字段名

类型

备注

EmployeeID

Int

自增字段

EmployeeName

Varchar(20)

 

Salary

Int

 

CellPhone

Varchar(20)

 

EmailAddress

Varchar(20)

 

 

程序的代码如下:

//------------------------Datagrid Demo------------------------------------

//-------------------------------------------------------------------------

//---File:frmDataGridDemo.cs

//---Description:The main form file to operate datagrid

//---Author:Knight

//---Date:Mar.17, 2006

//-------------------------------------------------------------------------

//----------------------{ Datagrid Demo }----------------------------------

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.SqlClient;

 

namespace CSDataGrid

{

    /// <summary>

    /// Summary description for frmDataGridDemo.

    /// </summary>

    public class frmDataGridDemo : System.Windows.Forms.Form

    {

        private System.Windows.Forms.DataGrid dtgUserInfo;

        private System.Windows.Forms.Button btnUpdate;

        private System.Windows.Forms.Button btnExit;

 

        protected SqlConnection sqlConn = new SqlConnection();

        protected SqlDataAdapter sqlDAdapter = null;

        protected DataSet sqlRecordSet = null;

 

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.Container components = null;

 

        public frmDataGridDemo()

        {

            //

            // Required for Windows Form Designer support

            //

            InitializeComponent();

 

            //

            // TODO: Add any constructor code after InitializeComponent call

            //

        }

 

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        protected override void Dispose( bool disposing )

        {

            if( disposing )

            {

                if (components != null)

                {

                    components.Dispose();

                }

            }

            base.Dispose( disposing );

        }

 

        #region Windows Form Designer generated code

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.dtgUserInfo = new System.Windows.Forms.DataGrid();

            this.btnUpdate = new System.Windows.Forms.Button();

            this.btnExit = new System.Windows.Forms.Button();

            ((System.ComponentModel.ISupportInitialize)(this.dtgUserInfo)).BeginInit();

            this.SuspendLayout();

            //

            // dtgUserInfo

            //

            this.dtgUserInfo.DataMember = "";

            this.dtgUserInfo.HeaderForeColor = System.Drawing.SystemColors.ControlText;

            this.dtgUserInfo.Location = new System.Drawing.Point(8, 16);

            this.dtgUserInfo.Name = "dtgUserInfo";

            this.dtgUserInfo.Size = new System.Drawing.Size(528, 480);

            this.dtgUserInfo.TabIndex = 0;

            //

            // btnUpdate

            //

            this.btnUpdate.Location = new System.Drawing.Point(544, 16);

            this.btnUpdate.Name = "btnUpdate";

            this.btnUpdate.TabIndex = 1;

            this.btnUpdate.Text = "&Update";

            this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);

            //

            // btnExit

            //

            this.btnExit.Location = new System.Drawing.Point(544, 472);

            this.btnExit.Name = "btnExit";

            this.btnExit.TabIndex = 2;

            this.btnExit.Text = "E&xit";

            //

            // frmDataGridDemo

            //

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

            this.ClientSize = new System.Drawing.Size(634, 511);

            this.Controls.Add(this.btnExit);

            this.Controls.Add(this.btnUpdate);

            this.Controls.Add(this.dtgUserInfo);

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;

            this.MaximizeBox = false;

            this.Name = "frmDataGridDemo";

            this.Text = "DataGrid Demo";

            this.Load += new System.EventHandler(this.frmDataGridDemo_Load);

            ((System.ComponentModel.ISupportInitialize)(this.dtgUserInfo)).EndInit();

            this.ResumeLayout(false);

 

        }

        #endregion

 

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.Run(new frmDataGridDemo());

        }

 

        private void frmDataGridDemo_Load(object sender, System.EventArgs e)

        {

            //Set connection string

            sqlConn.ConnectionString = yourDBConnectionString;

 

            //Connect to DB

            if( ConnectDB() )

            {

                //Bind data to datagrid

                BindData();

            }

        }

 

        private void AddDGStyle()

        {

            DataGridTableStyle ts1 = new DataGridTableStyle();

 

            //specify the table from dataset (required step)

            ts1.MappingName = "EmployeeInfo";

            PropertyDescriptorCollection pdc = this.BindingContext

                [sqlRecordSet, "EmployeeInfo"].GetItemProperties();

 

            DataGridColumnStyle TextCol = new DataGridTextBoxColumn( pdc["EmployeeID"], "i" );

            TextCol.MappingName = "EmployeeID";

            TextCol.HeaderText = "EmployeeID";

            TextCol.Width = 0;

            TextCol.ReadOnly = true;

            ts1.GridColumnStyles.Add(TextCol);

 

            TextCol = new DataGridTextBoxColumn();

            TextCol.MappingName = "EmployeeName";

            TextCol.HeaderText = "Employee Name";

            TextCol.Width = 100;

            ts1.GridColumnStyles.Add(TextCol);

           

            TextCol = new DataGridTextBoxColumn( pdc["Salary"], "i" );

            TextCol.MappingName = "Salary";

            TextCol.HeaderText = "Salary";

            TextCol.Width = 80;

            ts1.GridColumnStyles.Add(TextCol);

           

            TextCol = new DataGridTextBoxColumn();

            TextCol.MappingName = "CellPhone";

            TextCol.HeaderText = "Cell Phone";

            TextCol.Width = 80;

            ts1.GridColumnStyles.Add(TextCol);

 

            TextCol = new DataGridTextBoxColumn();

            TextCol.MappingName = "EmailAddress";

            TextCol.HeaderText = "Email Address";

            TextCol.Width = 100;

            ts1.GridColumnStyles.Add(TextCol);

 

            dtgUserInfo.TableStyles.Add(ts1);

 

        }

 

        private void SetDAdapterCommands()

        {

            string strQuery = "";

            SqlParameter sqlParm = null;

            // Create data adapter with select command

            strQuery = "SELECT EmployeeID, EmployeeName, Salary, CellPhone, EmailAddress "

                + " FROM EmployeeInfo";

            sqlDAdapter = new SqlDataAdapter( strQuery, sqlConn );

           

            //Set update command

            strQuery = "Update EmployeeInfo SET "

                + " EmployeeName = @EmployeeName, "

                + " Salary = @Salary, "

                + " CellPhone = @CellPhone, "

                + " EmailAddress = @EmailAddress "

                + " WHERE EmployeeID = @EmployeeID ";

 

            sqlDAdapter.UpdateCommand = new SqlCommand( strQuery, sqlConn );

            sqlDAdapter.UpdateCommand.Parameters.Add( "@EmployeeName", SqlDbType.VarChar,

                20, "EmployeeName" );

            sqlParm = sqlDAdapter.UpdateCommand.Parameters.Add("@Salary", SqlDbType.Int);

            sqlParm.SourceColumn = "Salary";

            sqlParm.SourceVersion = DataRowVersion.Current;

            sqlDAdapter.UpdateCommand.Parameters.Add( "@CellPhone", SqlDbType.VarChar,

                20, "CellPhone" );

            sqlDAdapter.UpdateCommand.Parameters.Add( "@EmailAddress", SqlDbType.VarChar,

                20, "EmailAddress" );

            sqlParm = sqlDAdapter.UpdateCommand.Parameters.Add("@EmployeeID", SqlDbType.Int);

            sqlParm.SourceColumn = "EmployeeID";

            sqlParm.SourceVersion = DataRowVersion.Original;

 

            //Set insert command

            strQuery = "INSERT INTO EmployeeInfo ("

                + " EmployeeName, "

                + " Salary, "

                + " CellPhone, "

                + " EmailAddress) "

                + " VALUES ( "

                + " @EmployeeName, "

                + " @Salary, "

                + " @CellPhone, "

                + " @EmailAddress)";

            sqlDAdapter.InsertCommand = new SqlCommand( strQuery, sqlConn );

            sqlDAdapter.InsertCommand.Parameters.Add( "@EmployeeName", SqlDbType.VarChar,

                20, "EmployeeName" );

            sqlParm = sqlDAdapter.InsertCommand.Parameters.Add("@Salary", SqlDbType.Int);

            sqlParm.SourceColumn = "Salary";

            sqlParm.SourceVersion = DataRowVersion.Current;

            sqlDAdapter.InsertCommand.Parameters.Add( "@CellPhone", SqlDbType.VarChar,

                20, "CellPhone" );

            sqlDAdapter.InsertCommand.Parameters.Add( "@EmailAddress", SqlDbType.VarChar,

                20, "EmailAddress" );

 

            strQuery = "DELETE FROM EmployeeInfo "

                + " WHERE EmployeeID = @EmployeeID ";

            sqlDAdapter.DeleteCommand = new SqlCommand( strQuery, sqlConn );

            sqlParm = sqlDAdapter.DeleteCommand.Parameters.Add("@EmployeeID", SqlDbType.Int);

            sqlParm.SourceColumn = "EmployeeID";

            sqlParm.SourceVersion = DataRowVersion.Original;

        }

 

        private void BindData()

        {

            SetDAdapterCommands();

 

            //Fill dataset

            sqlRecordSet = new DataSet();

            sqlDAdapter.Fill( sqlRecordSet, "EmployeeInfo" );

 

            //Bind datagrid with dataset

            dtgUserInfo.SetDataBinding( sqlRecordSet, "EmployeeInfo");

 

            //Add datagrid style

            AddDGStyle();

        }

 

        /// <summary>

        /// Connect to DB

        /// </summary>

        /// <returns>If connected, return True; else return False</returns>

        private bool ConnectDB()

        {

            //Check current connection's state

            try

            {

                if(     sqlConn.State == ConnectionState.Closed

                    ||  sqlConn.State == ConnectionState.Broken )

                {

                    //Connection is not available

                    sqlConn.Close();

                }

                else

                    //Connection is available

                    return true;

            }

            catch{};

 

            //Re-connect

            try

            {

                sqlConn.Open();

            }

            catch(SqlException e)

            {

                //Sql's exception

                MessageBox.Show( e.Message );

            }

            catch(Exception e)

            {

                //Other exception

                MessageBox.Show( e.Message );

            }

 

            if(     sqlConn.State == ConnectionState.Closed

                ||  sqlConn.State == ConnectionState.Broken )

                //Connection is not available

                return false;

            else

                //Connection is available

                return true;

        }

 

        private void btnUpdate_Click(object sender, System.EventArgs e)

        {

            sqlDAdapter.Update( sqlRecordSet, "EmployeeInfo" );

            sqlRecordSet.Tables["EmployeeInfo"].Rows.Clear();

            sqlDAdapter.Fill( sqlRecordSet, "EmployeeInfo" );

        }

 

    }

}

 

       上面的代码有很多细节没有作处理,因此仅仅演示如何对DataGrid操作。

优快云 2 2 阅读详情

相关推荐

添加odbc数据源时的一些问题和解决办法

pl/sql

Mr_Syh的博客 9642

EasyUI之消息窗口messager

1.样式 消息窗口提供了不同的消息框风格,包含alert(警告框), confirm(确认框), prompt(提示框), progress(进度框)等。所有的消息框都是异步的。用户可以在交互消息之后使用回调函数去处理结果或做一些自己需要处理的事情。 2.练习1:生成消息窗口 messager              

sunpy 1761

AD9361 RSSI配置实战:从寄存器到精准功率测量的避坑指南

本文详细解析了AD9361芯片的RSSI配置实战,从寄存器设置到精准功率测量的全流程避坑指南。通过优化模式选择、测量持续时间及高级参数配置,实现高精度信号强度测量,并分享实际项目中的校准流程与性能优化技巧,帮助工程师提升射频接收链路的测量精度。

weixin_38166557的博客 442

WPF往DataGrid添加数据

属性会让 DataGrid 自动根据数据源中的属性生成列。2.在后台添加数据并绑定在DataGrid上。1.在主页添加DataGrid。

Ronion123的博客 1649

4、I3C协议详解

I3C最初的设计目的是为移动设备创建一个能够使用多个传感器的单一接口。随着现代化移动设备对于传感器数量的增加以及对性能的提高,I2C和SPI已经达到了他们所能支持的临界点,而I3C的出现就是为了解决这一问题,I3C可以在同一根主线上支持更多的传感器设备,而且不会因为要支持中断或睡眠模式而增加额外的逻辑信号。I3C可以在更低功耗的情况下提供更快的传输速率,该特性的提升不止是对移动设备,对其他的嵌入式设备也都是很大的帮助。例如穿戴设备,需要在有限的物理空间中使用多个传感器并且要求更小的耗电。 一、简述..

迟子涵925的博客 2万+

销售预测的方法与模型(三)丨安全库存与再订货(补货)

较高的 Z 值对应较高的服务水平(例如,服务水平 99% 对应 Z 值 2.33),意味着企业希望更低的缺货风险,因此需要更高的安全库存。在再订货点的管理中,考虑到供应商交货的不确定性,通常需要结合安全库存,以防止在交货期间库存用尽。要对L开根号,根号L表示交货周期越长,企业需要的安全库存越多,因为在较长的供应期内,需求波动的累积效应更明显。举例:供应商的交期服从标准差为2件/天的正态分布,顾客需求为50件/天的正态分布,若要客户服务水平达到 90%,安全库存量应该是多少?下面介绍几种核心的模型与公式。

weixin_45675709的博客 1534

datagrid 之新增删除

一、思维导图 二、新增 1、查询旁边添加新增按钮 <a id="btn-add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">新增</a> 2、添加点击事件弹出窗体 修改与新增共用一个方法,定义一个addFlag,在增加按钮点击事件里定义addFlag为1,在修改按钮点击事件里定义addFlag为2 function submitForm(){ /** *

m0_60385807的博客 228

CANOE DIVA 工程怎样在具体服务前增加前置条件

然后会在CANoe的Test Module视窗中发现一个跟系统条件相关的Test Module模块,在该模块中有生成跟系统条件相关的CAPL 脚本。这里需要根据你的实际项目进行更改,diva工程加载到canoe的工程的时候,会同步加入你创建的cdd文件,这里示例的ABS_ESP 就是你的cdd的ECU命名,后面就是具体的前置条件服务。4、配置完成后,测试用例就会如下图Trace窗口所示,在每一次10 02请求之前都会调用CAPL Function的PreconditionCheck函数中定义的脚本。

weixin_45940007的博客 1964

datagrid之修改

前言:昨天与大家分享了datagrid之查询,今天与大家分享的是datagrid之修改,也会涉及到一些控件 一、明确目标: 1、 要用dialog、form控件写出修改效果 2、效果展示: 修改之后效果: 二、思路以及具体代码实现: 1、思路: 1、将修改加入到数据表格中。 2、点击修改之后要弹出一个对话框 ...

m0_53151031的博客 754

扩展卡尔曼滤波(EKF)理论讲解与实例(matlab、python和C++代码)

扩展卡尔曼滤波(EKF)理论讲解与实例(matlab、python和C++代码) 文章目录扩展卡尔曼滤波(EKF)理论讲解与实例(matlab、python和C++代码)理论讲解KF和EKF模型对比雅可比矩阵计算计算实例应用实例线性模型CV模型:CA模型非线性模型CTRV模型:CTRV实例(python)small demo抛物线demo飞机高度demoC++实例参考文献 我们上篇提到的卡尔曼滤波是用于线性系统,预测(运动)模型和观测模型是在假设高斯和线性情况下进行的。简单的卡尔曼滤波必须应用在符合高斯分布

O_MMMM_O的博客 10万+

AI测试干货:Claude Code Skill 从入门到精通

本文介绍了Claude Code Skill功能如何解决AI重复工作问题。传统提示词方式需要人工复制粘贴,而Skill功能通过建立AI工作记忆库实现智能触发和分层加载。Skill包含工作指令、文档和资源,能自动执行任务。文章以API测试用例生成为例,展示了Skill的创建流程和结构化设计,包括测试框架、覆盖场景和输出格式,显著提升测试工程师的工作效率。

blues_C的博客 2439

easyui05(datagrid数据新增)

myDialog。

qq_73126462的博客 795

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

DataGrid的使用入门(添加、删除和修改操作)[转]

DataGrid的使用入门(添加、删除和修改操作)[转] 最近在做一个管理系统,发现DATAGRID真的很好用,不过用起来有点麻烦,所以转一下相关文章,与大家分享。  见于好多人在优快云上问如何在DataGrid中进行添加、删除和修改操作,我最近作了如下一个例子。 首先,例子所用的数据库是SQL Server2000,数据库表格如下:...

weixin_30755709的博客 198

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

DataGrid的使用入门(添加、删除和修改操作)

最近在做一个管理系统,发现DATAGRID真的很好用,不过用起来有点麻烦,所以转一下相关文章,与大家分享。  见于好多人在优快云上问如何在DataGrid中进行添加、删除和修改操作,我最近作了如下一个例子。 首先,例子所用的数据库是SQL Server2000,数据库表格如下: 字段名

johncools的博客 1376

datagrid删除所有行卡住_Jquery easyui datagrid 删除多行问题

前台代码$(function () {$('#test').datagrid({title: '风格信息',iconCls: 'icon-save',fit: true,nowrap: false,striped: true,//collapsible: true,url: '/GenerManage/LoadAllByPage/',sortName: 'GenreId',remoteSort: ...

weixin_39990660的博客 66

easyui的datagrid的js加载

//实现对DataGird控件的绑定操作 function InitGrid(queryData) { $('#grid').datagrid({ //定位到Table标签,Table标签的ID是grid url: '/Menu/FindWithPager', //指向后台的Action来获取当前菜单的信息的Json格

山鹰的专栏 5115

[转载](ASP.NET)修改和删除DataGrid行——数据库访问

(ASP.NET)修改和删除DataGrid行——数据库访问        本程序涉及到数据库的添加,修改和删除操作。        懒得写了,把界面贴出来,照着界面画就可以了。本例数据库:SqlServer2000附带的pubs数据库,看一下连接字符串就很清楚了。如果要在本机器上运行,把uid和pwd改成你自己SQL登陆用户名和密码。        创建一个WEB页面,命名为:Add.aspx。

netboyfly的专栏 1303

datagrid的初始化以及动态加载

//datagrid初始化  $(function() {     $('#tt').datagrid({     //title : '信息显示',     //iconCls : 'icon-save',     width : 'auto',     height : 'auto',     pageSize:5,     pageList : [ 5, 10, 15 ],     nowr

hulufeng1203的专栏 3848

easyui datagrid增删改

                                    json数据 {   "total": 10,   "sucess":true,   "rows": [       {         "id":"1",         "name": "dongdong",         "value": "Koi",         ...

123

用ASP模拟ASP.net的DataGrid控件,提高ASP编码效率[原创]

  在做Web项目的时候,可能有大部分时间都花在对数据库的添加、修改、删除上。做过ASP.net人都知道DataGrid控件,只需要少量代码就可以对一个数据表进行显示、修改、删除操作。于是我就想如何在ASP中也能实现类似DataGrid控件。于是花了几个晚上做了这个ASP的DataGrid控件。  基本模仿了DataGrid的功能,只需新建一个页面,再引用DataGrid控件,再设置几行代码,就可

BugDone.cn轻量化bug系统开发随笔 5156

PDA开发,DataGrid的使用

PDA开发中,你会发现DataGrid中的方法属性少得可怜,很多在datagridview能够实现的东西,在PDA中都不好实现。往往我们使用DataGridView时并没有进行数据源的绑定,而是我们手动的对DataGridView中的数据进行操作,如:添加:DataGridView.Rows.add("1","aaa","bbb");修改:DataGridView.Rows[0].Cell[0

1545

JQueryEasyUI学习之datagrid 添加、修改、删除

JQueryEasyUI学习之datagrid 添加、修改、删除 实现EasyUI中DataGrid单个表格的编辑 转载于:https://www.cnblogs.com/xiaohui1990/p/4160763.html

weixin_30639719的博客 80

JQueryEasyUI学习笔记(十)datagrid 添加 修改 删除

JQueryEasyUI学习笔记(十)datagrid 添加 修改 删除

qq_43747015的博客 381
上一篇: 如何用C#创建Windows Service
下一篇: 如何用C#来部署数据库
Knight94
博客等级 码龄25年 1013粉丝 87原创
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值