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();
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM