---
title: "在C#中使用控件DataGridView实现数据库增删改查"
author: "foible"
date: "2008-02-17T17:21:00Z"
tags: []
slug: "c-datagridview"
draft: false
lastmod: "2013-01-14T08:58:33Z"
id: 1769175
url: "https://foible.iteye.com/blog/1769175"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Collections.Generic;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.ComponentModel;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Data;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Data.SqlClient;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Drawing;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Text;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Windows.Forms;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)namespaceDataSource  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicpartialclassForm1:Form  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicForm1()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)InitializeComponent();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateDataSetds=newDataSet();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateSqlConnectionconn=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateSqlDataAdapterda=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateconststringDRIVER="server=.;database=northwind;uid=sa;pwd=sa";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateconststringsql\_select="select\*fromregion";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*此方法为将数据库northwind中的region表的数据查询出来并放入DataSet中  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatevoidForm1\_Load(objectsender,EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)conn=newSqlConnection(DRIVER);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da=newSqlDataAdapter(sql\_select,conn);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da.Fill(ds,"table");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.dataGridView1.DataSource=ds.Tables\["table"].DefaultView;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateboolBtnInsert()//此方法作用于添加  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da.InsertCommand=conn.CreateCommand();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da.InsertCommand.CommandText="insertintoregionvalues(@id,@ption)";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da.InsertCommand.Parameters.Add("@id",SqlDbType.Int,4,"regionid");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da.InsertCommand.Parameters.Add("@ption",SqlDbType.VarChar,10,"regiondescription");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intcount=da.Update(ds);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)boolresult=count&gt;0?true:false;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnresult;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatevoidbutton1\_Click(objectsender,EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(this.BtnInsert())//调用此方法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MessageBox.Show("添加成功!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MessageBox.Show("添加失败!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateboolBtnDelect()//此方法作用于删除  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SqlParametersp=newSqlParameter();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da.DeleteCommand=conn.CreateCommand();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da.DeleteCommand.CommandText="deleteregionwhereregionid=@id";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sp=da.DeleteCommand.Parameters.Add("@id",SqlDbType.Int,4,"regionid");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sp.SourceVersion=DataRowVersion.Original;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ds.Tables\["table"].Rows\[this.dataGridView1.CurrentRow.Index].Delete();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intcount=da.Update(ds);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)boolresult=count&gt;0?true:false;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnresult;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatevoidbutton2\_Click(objectsender,EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(this.BtnDelect())//调用删除方法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MessageBox.Show("删除成功!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MessageBox.Show("删除失败!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateboolBtnUpdate()//此方法作用于修改  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SqlParametersp=newSqlParameter();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da.UpdateCommand=conn.CreateCommand();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da.UpdateCommand.CommandText="updateregionsetregionid=@id,regiondescription=@ptionwhereregionid=@oldid";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da.UpdateCommand.Parameters.Add("@id",SqlDbType.Int,4,"regionid");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)da.UpdateCommand.Parameters.Add("@ption",SqlDbType.VarChar,10,"regiondescription");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sp=da.UpdateCommand.Parameters.Add("@oldid",SqlDbType.Int,4,"regionid");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sp.SourceVersion=DataRowVersion.Original;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intcount=da.Update(ds);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)boolresult=count&gt;0?true:false;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnresult;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatevoidbutton3\_Click(objectsender,EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(this.BtnUpdate())//调用修改方法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MessageBox.Show("修改成功!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MessageBox.Show("修改失败!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)