DataGridView 直接修改数据操作

DataGridView单元格进入编辑模式的方法:

双击或者f2

EditCellOnEnter 属性设为True,对DataGridView进行配置,当用户移动到该单元格后,该单元格可立即切换到编辑模式

 

ESC取消编辑,如果将EditCellEnter属性设置为True,则单元格仍将处于编辑模式,但是所有更改都将被放弃,要提交更改,用户只移动到新的单元格,或者叫焦点(focus切换到其他控件。

 

 

为了防止单元格编辑,用户可以设置

DataGridViewCellDataGridViewColumn·1DataGridViewRowDataGridViewReadOnly属性取决于用户要限定更改的内容

 

要在DataGridView控件中修改数据,可通过CellValueChanged事件来完成

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace DataGridView直接修改数据
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

     

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = outvalue();


            

        }
        private DataTable outvalue() //用Fill(Datatable) 方法的返回值不能是Dataset了
        {
            string constr = "server=192.168.100.222;user=sa;pwd=p@ssw1rd;database=pwd1";
            SqlConnection mycon = new SqlConnection(constr);
            DataTable mytable = new DataTable(); //定义一个datatable 数据表
            try
            {
                mycon.Open();
                SqlDataAdapter mydpt = new SqlDataAdapter("select * from book",mycon);
               
                mydpt.Fill(mytable); //DataAdapter 有多种重载方式,也可以直接写datatable
                dataGridView1.EditMode = DataGridViewEditMode.EditOnF2;   //点击编辑单元格



            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                mycon.Close();
            
            }
            return mytable;
        
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) //为DataGridView创建一个单元格值改变事件
        {
            string constr = "server=192.168.100.222;user=sa;pwd=p@ssw1rd;database=pwd1"; 
            SqlConnection mycon = new SqlConnection(constr);
            
            try
            {
                mycon.Open();
                //定义一个字符串str1=表格控件.列【事件获取列的索引】.表头的文本+‘=’+单引号+表格控件.当前单元格.值.转换string类型+单引号
                //【e.columnIndex】为什么要用这个?因为我们要编辑那个行的时候,需要用到(比如数字监控他在bname上)要获取bname的列名字
                string str1=dataGridView1.Columns[e.ColumnIndex].HeaderText+"="+"'"+dataGridView1.CurrentCell.Value.ToString()+"'";


                //定义一个字符串str2=表格控件.行【事件获取行的索引】.单元格【0】.值.转换string类型
                //where bid=100005 这个1005怎么获取?【e.RowIndex】获取行的索引.因为100005在单元格中第1行,(索引器从0开始的0就是1),在获取这个上面的值.转换成string类型
                string str2=dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();

                //关联str1和str2 执行update 的时候就相当于把条件获取string变量上
                string sqlupdate = "update book set "+str1+" where bid="+str2;

                //为什么用sqlcommand,不用dataadapter呢?因为SqlCommand下面有个方法ExecuteNonQuery,它ExecuteNonQuery()不返回任何值,一把应用于 insert update delete语句中
                SqlCommand mycom = new SqlCommand(sqlupdate, mycon);

                mycom.ExecuteNonQuery();  //用到只能更新的方法来交互数据库更新
               


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {

                mycon.Close();
            }
        }
    }
}
原文地址:https://www.cnblogs.com/xiaowie/p/8651644.html