谁有winform对datagridview进行增删改的实例啊?

kissapple500 2013-06-28 04:30:53
我就是想直接在datagridview上输入数据,然后就插入数据库了或者修改了。

现在的问题是:如图,我有一列--表ID是隐藏的,那个是当你添加一行数据的时候,ID自动生成,不需要用户输入,问题就是:当我输入好了最后一个列的值时候,鼠标离开这个列就报错,提示我那个ID列是不允许为空的。

请问我什么时候对这个隐藏的ID列赋值呢?写在哪个事件里面呢?请大哥们指教。

再说修改:这个我可以直接双击某一列对里面的值进行修改吧。

那么删除呢?我怎么做呀?是不是要加个删除按钮呢?

谁有实际例子麻烦给个我把。。多谢了啊。。。。
...全文
355 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kissapple500 2013-07-01
  • 打赏
  • 举报
回复
我现在有一个新问题是这样的。我绑定到gridview上的数据不能满足我的需求,我给gridview加了一个列,这个列是我手动赋值给他的。但是现在赋值了,却不能显示出来啊。奇怪了。代码如下:this.datagridview.Rows[i].Cells[2].Value = stationName; 我试着在后面又加了个refresh(),还是显示不出来这一列的数据,仍然为空的。请高手指教啊。。。。。。。。。。。。。。。。。。。。
独立级IT民工 2013-06-29
  • 打赏
  • 举报
回复
删除加一个删除按钮,然后获取选中行的那列ID值,当作条件传入删除SQL语句中,操作语句进行删除。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DataGridViewTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private DataSet ds = new DataSet();
        private SqlConnection conn = null;
        private SqlDataAdapter da = null;
        private const string DRIVER = "server=.;database=northwind;uid=sa;pwd=sa";
        private const string sql_select = "select * from region";

        /**//**
         * 此方法为将数据库northwind中的region表的数据查询出来并放入DataSet中 
        **/ 
        private void Form1_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection(DRIVER);
            da = new SqlDataAdapter(sql_select,conn);
            da.Fill(ds,"table");
            this.dataGridView1.DataSource = ds.Tables["table"].DefaultView;
        }

        private bool BtnInsert() //此方法作用于添加
        {
            da.InsertCommand = conn.CreateCommand();
            da.InsertCommand.CommandText = "insert into region values(@id,@ption)";
            da.InsertCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
            da.InsertCommand.Parameters.Add("@ption", SqlDbType.VarChar, 10, "regiondescription");
            int count = da.Update(ds);
            bool result = count > 0 ? true : false;
            return result;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.BtnInsert())//调用此方法
            {
                MessageBox.Show("添加成功!");
            }
            else 
            {
                MessageBox.Show("添加失败!");
            }
        }


        private bool BtnDelect() //此方法作用于删除
        {
            SqlParameter sp = new SqlParameter();
            da.DeleteCommand = conn.CreateCommand();
            da.DeleteCommand.CommandText = "delete region where regionid=@id";
            sp = da.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
            sp.SourceVersion = DataRowVersion.Original;
            ds.Tables["table"].Rows[this.dataGridView1.CurrentRow.Index].Delete();
            int count = da.Update(ds);
            bool result = count > 0 ? true : false;
            return result;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.BtnDelect())//调用删除方法
            {
                MessageBox.Show("删除成功!");
            }
            else
            {
                MessageBox.Show("删除失败!");
            }
        }


        private bool BtnUpdate() //此方法作用于修改
        {
            SqlParameter sp = new SqlParameter();
            da.UpdateCommand = conn.CreateCommand();
            da.UpdateCommand.CommandText = "update region set regionid=@id,regiondescription=@ption where regionid=@oldid";

            da.UpdateCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
            da.UpdateCommand.Parameters.Add("@ption", SqlDbType.VarChar, 10, "regiondescription");

            sp = da.UpdateCommand.Parameters.Add("@oldid", SqlDbType.Int, 4, "regionid");
            sp.SourceVersion = DataRowVersion.Original;
            
            int count = da.Update(ds);
            bool result = count > 0 ? true : false;
            return result;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (this.BtnUpdate())//调用修改方法
            {
                MessageBox.Show("修改成功!");
            }
            else
            {
                MessageBox.Show("修改失败!");
            }
        }


    }
}

Regan-lin 2013-06-28
  • 打赏
  • 举报
回复
主键应该一开始就赋值了!
u011018746 2013-06-28
  • 打赏
  • 举报
回复
想要增删改查不难吧,我知道有个框架叫OR-mapping,使得操作数据库像操作对象意义直观方便,国内用这个框架用的不错的平台很多,有金蝶的bos,极致的JBF,可以下载去体验体验这种框架一般界面的增删改查是不需要编写代码的!

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧