C#操作dataGridView添加数据库中数据,删除dataGridView中的数据
来源:互联网 发布:淘宝上的店铺怎么注销 编辑:程序博客网 时间:2024/05/20 17:38
C#操作dataGridView添加数据库中数据,删除dataGridView中的数据
这是我的一篇总结文章,供大家学习参考,便于自己以后的学习。
http://blog.sina.com.cn/s/blog_149e9d2ec0102vur0.html
首先需要using System.Data.SqlClient;
添加数据前:
首先创建数据库(SQLserver2008):
create database library
on
(
name = 'library',--逻辑文件名
filename = 'E:\SQLServer2008\library.mdf',--物理文件名
size = 3MB,
maxsize = 10,
filegrowth=20%
)
log on
(
name = 'library_log',
filename='E:\SQLServer2008\library_log.ldf',
size = 2MB,
maxsize = 20MB,
filegrowth = 1MB
)
然后创建一个表,并插入数据:
create table student
(
id int not null primary key,
name varchar(10) not null,
sex varchar(2)default '男'
)
insert into student(id, name, sex)values(1,'腾飞','男'),(2,'天地','女'),(3,'顾倩云','女'),(4,'李刚','男')
注:如果数据表命令创建不成功,可以用界面点击操作完成。
C#代码:
private void btn_AddData_Click(object sender,EventArgs e)//数据添加
{
//方法一
//stringid = "4";
//stringname = "Jim";
//stringsex = "男";
//string[]row = { id, name, sex };
//dataGridView1.Rows.Add(row);
//方法二
//创建行,添加行
stringconStr = @"Data Source=.;initial Catalog=library;User ID =sa;Pwd=241475";
SqlConnection conn = new SqlConnection(conStr);//连接数据库
try
{
conn.Open();//打开数据库
string strSql = "select *from student";//SQL语句
SqlCommand comm = newSqlCommand(strSql, conn);//执行SQL命令
SqlDataAdapter sda = newSqlDataAdapter(comm);
DataSet ds = newDataSet();
sda.Fill(ds,"student");
dataGridView1.DataSource =ds;
dataGridView1.DataMember ="student";
dataGridView1.Columns[0].HeaderText = "编号";//更改列名称
//设置dataGridView1控件第一列的列宽
dataGridView1.Columns[0].Width = 85;
dataGridView1.Columns[1].HeaderText = "姓名";
//设置dataGridView1控件第二列的列宽
dataGridView1.Columns[1].Width = 110;
dataGridView1.Columns[2].HeaderText = "性别";
//设置dataGridView1控件第三列的列宽
dataGridView1.Columns[2].Width = 105;
}
catch(Exception ex)
{
MessageBox.Show(" " +ex);
}
finally
{
conn.Close();
}
}
完成后显示:
删除dataGridView中的数据:
C#代码:
private void btn_DelData_Click(object sender,EventArgs e)
{
//dataGridView1.AllowUserToAddRows = false;//删除最后一行的空白行
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);//删除当前光标所在行
//dataGridView1.Rows.Clear();//删除所有行
}
dataGridviewView中添加和删除功能就实现了,恭喜你已经学会了怎么用了。
可供:学习借鉴
阅读全文
0 0
- C#操作dataGridView添加数据库中数据,删除dataGridView中的数据
- C#对DataGridView中的数据进行添加、修改、删除操作
- C#对DataGridView中的数据进行添加、修改、删除操作c#操作datatable
- 对DataGridView进行添加、修改、删除数据操作----C#
- DataGridView进行添加、修改、删除数据操作-C#
- C#对DataGridView进行添加、修改、删除数据操作
- C#中datagridview显示数据库数据
- 关于C#中datagridview中的数据绑定
- C# DataGridView手动添加数据
- 向datagridview添加数据,删除数据
- C#打印dataGridView中的数据
- C#打印DataGridView 中的数据
- C#打印dataGridView中的数据
- C#清除datagridview中的数据
- c# 不用数据绑定,将DataTable中的数据添加到DataGridView中
- c#中dataGridView数据绑定后的数据操作
- 实现datagridview中数据的添加,更改,删除功能
- 在不涉及数据库的情况下,对DataGridView中的数据进行删除操作
- Android Studio MAT内存泄漏查找, 分析方法
- mybatis连接MySQL如何支持批量sql
- windows server 2003 如何添加svg图像显示
- 依赖注入与控制反转的理解
- 冒泡、鸡尾酒、选择、插入、快排、希尔
- C#操作dataGridView添加数据库中数据,删除dataGridView中的数据
- Emoji表情符号录入MySQL数据库报错的解决方案
- Codeforces 719C Efim and Strange Grade【贪心】
- iOS 系统弹窗 自动消失
- python性能优化(1)
- Oracle行级锁的特殊用法简析
- 利用tf.nn.conv2d_transpose实现图片分辨率按指定倍数扩展
- Mybatis的逆向工程
- mysql 合并函数 -- COALESCE