winform datagridview增删改查(用C#编写的数据库管理系统,其中有DataGridview需要改动,需要把查询到的某一列或几列做变换)

本文目录
- 用C#编写的数据库管理系统,其中有DataGridview需要改动,需要把查询到的某一列或几列做变换
- 在C#的winform中怎么直接在DataGridView里面修改,添加数据.(添加,修改到数据库里)
- winform中的datagridview中添加怎么修改删除 按钮,代码怎么写
- C#怎么在datagridvied进行增删改查并将修改的数据更新到sql数据库
- c# dataGridView增删改查
- C#如何对datagridview里的数据直接增、删、改并保存到SQL2005数据库中
- C#winform关于在datagridview中添加或删除数据,并可更新到数据库里
- 使用datagridview 进行数据的增删改查.
用C#编写的数据库管理系统,其中有DataGridview需要改动,需要把查询到的某一列或几列做变换
你可以看看这个 也许有帮助
winform 中的datagridview添加、修改、删除
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;
namespace 学生成绩管理系统
{
public partial class studentshanchu : Form
{
CurrencyManager cmAmend;
public studentshanchu()
{
InitializeComponent();
}
private void studentshanchu_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“studentDataSet.studentinfo”中。您可以根据需要移动或移除它。
//this.studentinfoTableAdapter.Fill(this.studentDataSet.studentinfo);
//数据绑定
string sqlsel = "select * from studentinfo ";
DataTable dt = bangding(sqlsel);
cmAmend = (CurrencyManager)BindingContext;
this.dataGridView1.DataSource = dt;
this.textBox1.DataBindings.Add("text", dt, "sno");
this.textBox2.DataBindings.Add("text", dt, "sname");
this.textBox3.DataBindings.Add("text", dt, "ssex");
this.textBox4.DataBindings.Add("text", dt, "sage");
this.textBox5.DataBindings.Add("text", dt, "sclass");
this.textBox6.DataBindings.Add("text", dt, "sdept");
this.textBox7.DataBindings.Add("text", dt, "saddress");
this.textBox8.DataBindings.Add("text", dt, "sphone");
this.textBox9.DataBindings.Add("text", dt, "sqq");
}
DataTable bangding(string sqlsel)
{
string connstr = "server=.;uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);
da.Fill(ds, "coust");
DataTable dt = ds.Tables;
conn.Close();
return dt;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{ this.Height = 450; }
else
{ this.Height = 250; }
}
private void button1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定要删除该记录吗", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
int pos = this.dataGridView1.CurrentCell.RowIndex; //获取该行
string connstr = "server=.;uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
DataSet ds = new DataSet();
string sqlset = "select * from studentinfo";
//数据集
SqlDataAdapter da = new SqlDataAdapter(sqlset, conn);
da.Fill(ds, "coust");
DataTable dt = ds.Tables;
SqlCommandBuilder cb = new SqlCommandBuilder(da);
dt.Rows.Delete();
da.Update(ds, "coust");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
this.dataGridView1.DataSource = bangding(sqlset);
MessageBox.Show("恭喜你已成功删除","温馨提示");
conn.Close();
}
}
}
}
}
修改的代码:
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;
namespace 学生成绩管理系统
{
public partial class studentxiugai : Form
{
string connstr = "server=.;uid=sa;pwd=sa;database=sc";
string per;
string sno;
CurrencyManager cmAmend;
public studentxiugai()
{
InitializeComponent();
}
public studentxiugai(string k,string s)
{
InitializeComponent();
per = k;
sno = s;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{ //this.Height = 450;
comboBox1.Enabled = false;
textBox1.Enabled = false;
}
else
{ //this.Height = 250;
comboBox1.Enabled = true;
textBox1.Enabled = true;
}
}
private void studentxiugai_Load(object sender, EventArgs e)
{
//数据绑定
if (per == "超级用户")
{
string sqlsel = "select * from studentinfo order by sno asc ";
DataTable dt = bangding(sqlsel);
cmAmend = (CurrencyManager)BindingContext;
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "sno";
this.textBox1.DataBindings.Add("text", dt, "sname");
this.textBox2.DataBindings.Add("text", dt, "ssex");
this.textBox3.DataBindings.Add("text", dt, "sage");
this.textBox4.DataBindings.Add("text", dt, "sclass");
this.textBox5.DataBindings.Add("text", dt, "sdept");
this.textBox6.DataBindings.Add("text", dt, "saddress");
this.textBox7.DataBindings.Add("text", dt, "sphone");
this.textBox8.DataBindings.Add("text", dt, "sqq");
}
if (per == "普通用户")
{
string sqlsel = "select *from studentinfo where sno=’" + sno + "’";
DataTable dt = bangding(sqlsel);
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "sno";
this.textBox1.DataBindings.Add("text", dt, "sname");
this.textBox2.DataBindings.Add("text", dt, "ssex");
this.textBox3.DataBindings.Add("text", dt, "sage");
this.textBox4.DataBindings.Add("text", dt, "sclass");
this.textBox5.DataBindings.Add("text", dt, "sdept");
this.textBox6.DataBindings.Add("text", dt, "saddress");
this.textBox7.DataBindings.Add("text", dt, "sphone");
this.textBox8.DataBindings.Add("text", dt, "sqq");
}
}
DataTable bangding(string sqlsel)
{
// string connstr = "server=.;uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);
da.Fill(ds, "coust");
DataTable dt = ds.Tables;
conn.Close();
return dt;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
string sno = comboBox1.Text;
string sname = textBox1.Text;
string sex = textBox2.Text;
string age = textBox3.Text;
string sclass = textBox4.Text;
string sdept = textBox5.Text;
string saddress = textBox6.Text;
string sphone = textBox7.Text;
string sqq = textBox8.Text;
if (textBox2.Text == "" ¦ ¦ textBox3.Text == "" ¦ ¦ textBox4.Text == "" ¦ ¦ textBox5.Text == "")
{
if (textBox2.Text == "")
{
MessageBox.Show("性别不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox2.Focus();
return;
}
if (textBox3.Text == "")
{
MessageBox.Show("年龄不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox3.Focus();
return;
}
if (textBox4.Text == "")
{
MessageBox.Show("班级不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox4.Focus();
return;
}
if (textBox5.Text == "")
{
MessageBox.Show("系别不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox5.Focus();
return;
}
}
else
{
string connstr = "server=IT32;uid=sa;pwd=’sa’;database=sc;";
try
{
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string sqlupdate = " update studentinfo set ssex=’" + sex + "’,sage=’" + age + "’,sclass=’" + sclass + "’,sdept=’" + sdept + "’,saddress=’" + saddress + "’,sphone=’" + sphone + "’,sqq=’" + sqq + "’ where sno=’" + sno + "’";
SqlCommand sc = new SqlCommand(sqlupdate, conn);
sc.ExecuteNonQuery();
MessageBox.Show("数据已经修改成功", "温馨提示");
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
else
{
MessageBox.Show("对不起,你还没有选中’我要修改’,所以您无法修改","温馨提示");
}
}
}
}
在C#的winform中怎么直接在DataGridView里面修改,添加数据.(添加,修改到数据库里)
这个好办啊!你只需要在GataGridView的CellClick事件里写上相应的代码即可!
CellClick是当GataGridView里的单元格被点击时被调用的事件
加入你的数据表里有“姓名”和“工资”两列数据。
int rowindex = GataGridView.CurrentCell.RowIndex; //得到行号
txtName.Text = GataGridView.Rows.Value.ToString();
txtMoney.Text =GataGridView.Rows.Value.ToString();
把你所要现实的数据与数据库相应的数据绑定!
然后只需在修改,删除事件里创建一个SqlCommandBuilder对象(它能自动生成insert update delete语句)有了这个对象,就不用手写insert update delete语句,由该对象,根据数据库中数据表的结构,自动生成
SqlCommandBuilder scb = new SqlCommandBuilder(adp);
这里的adp是SqlDataAdapter的对象名,你在生成SqlCommandBuilder 之前必须创建一个数据适配器(SqlDataAdapter)即SqlDataAdapter adp = new SqlDataAdapter();
这样,adp就具备了添加,修改,删除的功能,再让adp影响数据库,当它具备了以上的这些功能,就可以让数据集所做的修改影响数据库的改变
adp.Update(DataSet对象名, "数据集里的表名");
winform中的datagridview中添加怎么修改删除 按钮,代码怎么写
《asp:datagridview ID="d1"》
《asp:itemtemplate》
《table》
《tr》
《td》《asp:Button ID="Button1" Text="修改"》《/asp:Button》《/td》
《td》《asp:Button ID="Button2" Text="删除"》《/asp:Button》《/td》
《/tr》
《/table》
《/asp:itemtemplate》
《/asp:datagridview》
C#怎么在datagridvied进行增删改查并将修改的数据更新到sql数据库
这个要看你是怎取的数据,如果是在系统里面直接通过配置dataset出来的,你可以直接配下dataset里的update方法,dataGridView更改后用AcceptChanges,就能更新了!如果查询那些是自己写的,就得自己写Update方法。
OleDbConnection conn = new OleDbConnection(连接字符串); OleDbCommand comm = new OleDbCommand(@"UPDATE Suser SET 姓名 = ’aa’, 性别 = ’bb’ WHERE (Suser.学号 = ’123’)", conn); conn.Open(); Int32 ii = comm.ExecuteNonQuery(); conn.Close()
c# dataGridView增删改查
给你点代码吧,我自己参考着书写的,你好好研究研究,有什么不懂了,随时找我吧..首先你必须确定当前行是可以的,以下代码是选择当前行是可的:protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex;
bindData();
}
然后你就可以更新了,updatestr为要更新的变量
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
string connStr = "server=localhost;uid=sa;pwd=sa;database=myWebDB";
SqlConnection myconnection = new SqlConnection(connStr);
string updatestr = "";
updatestr+="bookid=’"+((TextBox)e.Item.Cells)为取得当前行的第3个单元格也就是文本框了。把它的内容赋给字段
updatestr += ",bookname=’" + ((TextBox)e.Item.Cells).Text + "’";
updatestr += ",author=’" + ((TextBox)e.Item.Cells).Text + "’";
updatestr += ",publisher=’" + ((TextBox)e.Item.Cells).Text + "’";
updatestr += ",price=’" + ((TextBox)e.Item.Cells).Text + "’";
string updatecomm = "update book_inf set " + updatestr + "where bookid=’" + DataGrid1.DataKeys + "’";
SqlCommand myupdate = new SqlCommand(updatecomm, myconnection);
try
{
myconnection.Open();
myupdate.ExecuteNonQuery();
Response.Write("《script》alert(’更新成功!’)《/script》");
}
catch
{
Response.Write("《script》alert(’更新失败!’)《/script》");
}
finally
{
myconnection.Close();
}
bindData();
更新的原理就是通过创建sqlcommand对象,然后利用更新的语句,更新数据库。删除的原理也是这。
private void bindData()
{
string connStr = "server=localhost;uid=sa;pwd=sa;database=myWebDB";
SqlConnection myconnection = new SqlConnection(connStr);
//myconnection.Open();
string queryStr = "select * from book_inf";
SqlDataAdapter myDataAdapter = new SqlDataAdapter(queryStr, myconnection);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, "books");
if (myDataSet.Tables.Rows.Count 》 0)
{
Label1.Text = "所有图书登记";
DataGrid1.DataSource = myDataSet.Tables.DefaultView;
DataGrid1.DataBind();
}
else
{
Label1.Text = "没有图书登记";
DataGrid1.Visible = false;
}
C#如何对datagridview里的数据直接增、删、改并保存到SQL2005数据库中
SqlDataAdapter sd;
DataSet ds;
private void button1_Click(object sender, EventArgs e) //添加
{
try
{
SqlCommandBuilder scb = new SqlCommandBuilder(sd);
sd.Update(ds, "a");
ds.AcceptChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
MessageBox.Show("添加成功!!!");
}
private void button3_Click(object sender, EventArgs e) //删除
{
try
{
SqlCommandBuilder scb = new SqlCommandBuilder(sd);
int a = dataGridView1.CurrentRow.Index;
ds.Tables.Delete();
sd.Update(ds, "a");
ds.AcceptChanges();
MessageBox.Show(string.Format("删除第{0}行成功!", a + 3));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void button8_Click(object sender, EventArgs e) //修改 {
try
{
SqlCommandBuilder scb = new SqlCommandBuilder(sd);
sd.Update(ds, "a");
ds.AcceptChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
MessageBox.Show("修改成功!!!");
}
C#winform关于在datagridview中添加或删除数据,并可更新到数据库里
你绑定的肯定是个Dataset
ds对象,把他作为一个全局变量。
Datatable
dt=ds.Tables;
添加数据:
DataRow
drNew=dt.NewRow();
drNew="2015-7-3";
drNew="09:46:47";
删除数据:
DataRow
drDelete=你想要删除的行
dt.Rows.Remove(drDelete);
更新到数据库:
用IDbDataAdapter
pAdaptor接口更新
pAdaptor.Update(ds);
日期转化
string
sdate="2015-7-3";
DateTime
dtime;
DateTime.TryParse(sdate,out
dtime);
int
i=dtime.DayOfWeek;//
星期几
使用datagridview 进行数据的增删改查.
这样吧,我发一个GridView的增删改查的例子给你:
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OASys.BLL;
using OASys.Models;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
public UserInfoManager info = new UserInfoManager();
/// 《summary》
/// 窗体加载事件
/// 《/summary》
/// 《param name="sender"》《/param》
/// 《param name="e"》《/param》
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadInfo();
}
}
/// 《summary》
/// 加载用户信息的方法
/// 《/summary》
private void LoadInfo()
{
List《UserInfo》 users =new List《UserInfo》();
foreach (UserInfo user in info.GetAllUserInfo())
{
if (user.Gender == 1)
{
user.DepartId.sex = "男";
}
else
{
user.DepartId.sex = "女";
}
users.Add(user);
}
gdvUserInfo.DataSource =users;
gdvUserInfo.DataBind();
}
/// 《summary》
/// 分页事件
/// 《/summary》
/// 《param name="sender"》《/param》
/// 《param name="e"》《/param》
protected void gdvUserInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdvUserInfo.PageIndex = e.NewPageIndex;
LoadInfo();
}
/// 《summary》
/// 删除信息事件
/// 《/summary》
/// 《param name="sender"》《/param》
/// 《param name="e"》《/param》
protected void gdvUserInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id =gdvUserInfo.DataKeys.Value.ToString();
if (info.DeleteUserInfoById(id) 》 0)
{
this.lblMessage.Text = "删除成功!";
LoadInfo();
}
else
{
this.lblMessage.Text = "删除失败!";
LoadInfo();
}
}
/// 《summary》
/// 事件
/// 《/summary》
/// 《param name="sender"》《/param》
/// 《param name="e"》《/param》
protected void gdvUserInfo_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvUserInfo.EditIndex = e.NewEditIndex;
LoadInfo();
}
/// 《summary》
/// 取消事件
/// 《/summary》
/// 《param name="sender"》《/param》
/// 《param name="e"》《/param》
protected void gdvUserInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
/// 《summary》
/// 更新事件
/// 《/summary》
/// 《param name="sender"》《/param》
/// 《param name="e"》《/param》
protected void gdvUserInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string userId = gdvUserInfo.DataKeys.Value.ToString();
TextBox txtUserId = (TextBox)gdvUserInfo.Rows.FindControl("txtUserId");
TextBox txtUserName = (TextBox)gdvUserInfo.Rows.FindControl("txtUserName");
TextBox txtPassWord = (TextBox)gdvUserInfo.Rows.FindControl("txtPassWord");
if (txtUserName != null && txtPassWord != null)
{
try
{
UserInfo user = info.GetUserInfoById(userId);
user.UserName = txtUserName.Text.ToString();
user.Password = txtPassWord.Text.ToString();
if (info.ModifyUserInfo(user) 》 0)
{
this.lblMessage.Text = "修改成功!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
else
{
this.lblMessage.Text = "修改失败!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
}
catch (Exception)
{
this.lblMessage.Text = "修改失败!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
}
}
/// 《summary》
///
/// 光棒事件
/// 《/summary》
/// 《param name="sender"》《/param》
/// 《param name="e"》《/param》
protected void gdvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i 《 gdvUserInfo.Rows.Count; i++)
{
if (gdvUserInfo.Rows.RowType == DataControlRowType.DataRow)
{
gdvUserInfo.Rows.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor=’#9cf’");
gdvUserInfo.Rows.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
}

本文相关文章:
json时格式自动换行(数据库中数据偶尔有换行符、回车符,在java里取出数据拼JSON格式字符串的时候要出问题)
2025年11月1日 17:45
sqlite3读取性能(csv和sqllite数据库读取哪个快)
2025年10月24日 04:30
java数据库(java中怎么向数据库插入数据(java怎么导入数据库))
2025年10月23日 18:00
数据库逻辑运算符有哪些(智慧芽专利数据库支持哪几种逻辑运算符)
2025年10月22日 00:30
windows7不自带access怎么办(为什么Win7里面数据库access不能用)
2025年10月21日 08:00
sql server 2008合成模板库失败(SQL Server2008数据库引擎等安装失败的原因)
2025年10月9日 08:00
更多文章:
计算机网络协议指的是tcp ip协议(TCP/IP协议是什么)
2025年10月21日 07:15
oracle数据库备份文件后缀(ORACLE 数据库导出的数据文件 什么后缀)
2026年4月7日 10:45
新加坡marina square风景(新加坡Marina Bay Sands空中花园设计欣赏)
2026年4月3日 10:45
datagridview设置表头(Datagridview表头样式设置)
2026年9月3日 13:45
mysql workbench查看表结构(在使用mysqlworkbench6.3怎么查看mysql数据库的关系图)
2025年7月31日 04:30
curl访问地址(php curl访问本地测试地址 链接超时,什么原因.公网可以)
2026年2月15日 19:15
object后面接什么(高中英语 object 意思为“拒绝“时的用法 后面跟什么)
2026年6月30日 19:00
win10系统java环境变量设置(win10怎么配置java环境变量path)
2025年6月16日 13:45
jquerytrigger参数(怎么给jquery方法传值 就是点击第2个button后执行第一个button的方式 怎么给s传值)
2025年11月5日 00:15
selective tool教程(selective tool能卸载吗)
2025年5月30日 06:30











