• C#的DataGridView数据控件(直接访问SQL vs 通过EF实体模型访问SQL)


    目录

    一、在DataGridView控件中显示数据

    1.直接编程访问SQL

    (1)源码

    (2)生成效果

    2.通过EF实体模型访问SQL

    (1)源码

    (2)生成效果

    二、获取DataGridView控件中的当前单元格

    1.直接编程访问SQL 

    (1)源码

    (2)生成效果​​​​​​​

    2.通过EF实体模型访问SQL

    (1)源码

    (2)生成效果

    三、在DataGridView控件中修改数据

     1.直接编程访问SQL

    (1)源码

    (2)生成效果 

     2.通过EF实体模型访问SQL

    (1)源码

    (2)生成效果


           使用DataGridView控件可以快速地将数据库中的数据显示给用户,并且可以通过DataGridView控件直接对数据进行操作,大大增强了操作数据库的效率。DataGridView控件提供一种强大而灵活的以表格形式显示数据的方式。

            既可以通过编程直接访问SQL ,也可以通过建立EF实体模型间接访问SQL,哪个方法更方便呢?请读者给出答案吧?

             如何建立数据库的实体模型,在作者的其他文章里有所涉及,本文中不再论述。

    一、DataGridView控件中显示数据

    1.直接编程访问SQL

            通过DataGridView控件显示数据表中的数据,首先需要使用DataAdapter对象查询指定的数据,然后通过该对象的Fill()方法填充DataSet,最后设置DataGridView控件的DataSource属性为DataSet的表格数据。DataSource属性用于获取或设置DataGridView控件所显示数据的数据源。

    1. 语法如下:public Object DataSource{get;set;}
    2. 属性值:包含DataGridView控件要显示的数据的对象。

    (1)源码

    1. //Form1.cs
    2. //在DataGridView控件中显示数据
    3. using System;
    4. using System.Collections.Generic;
    5. using System.ComponentModel;
    6. using System.Data;
    7. using System.Data.SqlClient;
    8. using System.Drawing;
    9. using System.Linq;
    10. using System.Text;
    11. using System.Threading.Tasks;
    12. using System.Windows.Forms;
    13. namespace _01
    14. {
    15. public partial class Form1 : Form
    16. {
    17. public Form1()
    18. {
    19. InitializeComponent();
    20. }
    21. ///
    22. /// 实例化SqlConnection变量conn,连接数据库db_CSharp
    23. /// 创建一个SqlDataAdapter对象
    24. /// 创建一个DataSet对象
    25. /// 使用SqlDataAdapter对象的Fil()方法填充DataSet
    26. /// 设置dataGridView1控件数据源
    27. ///
    28. private void Form1_Load(object sender, EventArgs e)
    29. {
    30. dataGridView1.Dock = DockStyle.Fill;
    31. dataGridView1.AllowUserToAddRows = false;
    32. dataGridView1.AllowUserToDeleteRows = false;
    33. dataGridView1.AllowUserToResizeColumns = false;
    34. dataGridView1.AllowUserToResizeRows = false;
    35. dataGridView1.ReadOnly = true;
    36. dataGridView1.RowHeadersVisible = false;
    37. SqlConnection conn = new SqlConnection("Server=DESKTOP-QFENBNJ\\SQL_WEN;integrated security=SSPI;Initial Catalog=db_CSharp");
    38. //conn = new SqlConnection("Server=DESKTOP-GFMO83R;integrated security=SSPI;Initial Catalog=db_CSharp");
    39. SqlDataAdapter sda = new SqlDataAdapter("select*from tb_emp", conn);
    40. DataSet ds = new DataSet();
    41. sda.Fill(ds, "emp");
    42. dataGridView1.DataSource = ds.Tables[0];
    43. }
    44. }
    45. }

    (2)生成效果

     

    2.通过EF实体模型访问SQL

    (1)源码

    1. //Form1.cs
    2. //在DataGridView控件中显示数据
    3. using System;
    4. using System.Collections.Generic;
    5. using System.ComponentModel;
    6. using System.Data;
    7. using System.Drawing;
    8. using System.Linq;
    9. using System.Text;
    10. using System.Threading.Tasks;
    11. using System.Windows.Forms;
    12. namespace _01_1
    13. {
    14. public partial class Form1 : Form
    15. {
    16. public Form1()
    17. {
    18. InitializeComponent();
    19. }
    20. private void Form1_Load(object sender, EventArgs e)
    21. {
    22. dataGridView1.Dock = DockStyle.Fill;
    23. dataGridView1.AllowUserToAddRows = false;
    24. dataGridView1.AllowUserToDeleteRows = false;
    25. dataGridView1.AllowUserToResizeColumns = false;
    26. dataGridView1.AllowUserToResizeRows = false;
    27. dataGridView1.ReadOnly = true;
    28. dataGridView1.RowHeadersVisible = false;
    29. using (db_CSharpEntities db_ = new db_CSharpEntities())
    30. {
    31. dataGridView1.DataSource = db_.tb_emp.ToList();
    32. };
    33. }
    34. }
    35. }

    (2)生成效果

             生成效果与上述方法相同。但通过EF实体模型访问数据库更简单。

    二、获取DataGridView控件中的当前单元格

    1.直接编程访问SQL 

            若要与DataGridView进行交互,通常要求通过编程方式发现哪个单元格处于活动状态。如果需要更改当前单元格,可通过DataGridView控件的CurrentCell属性来获取当前单元格信息。CurrentCell属性用于获取当前处于活动状态的单元格。语法如下:

    1. public DataGridViewCell CurrentCell {get;set;}
    2. 属性值:表示当前单元格的DataGridViewCell,如果没有当前单元格,则为空引用。默认值是第一列中的第一个单元格,如果控件中没有单元格,则为空引用。

    (1)源码

    1. //Form1.cs
    2. //获取控件单元格信息
    3. using System;
    4. using System.Collections.Generic;
    5. using System.ComponentModel;
    6. using System.Data;
    7. using System.Data.SqlClient;
    8. using System.Drawing;
    9. using System.Linq;
    10. using System.Reflection.Emit;
    11. using System.Text;
    12. using System.Threading.Tasks;
    13. using System.Windows.Forms;
    14. namespace _02
    15. {
    16. public partial class Form1 : Form
    17. {
    18. public Form1()
    19. {
    20. InitializeComponent();
    21. }
    22. ///
    23. /// 初始化Form1
    24. /// 实例化SqlConnection变量conn,连接数据库db_CSharp
    25. /// 创建一个SqlDataAdapter对象
    26. /// 创建一个DataSet对象
    27. /// 使用SqlDataAdapter对象的Fil()方法填充DataSet
    28. /// 设置dataGridView1控件数据源
    29. ///
    30. private void Form1_Load(object sender, EventArgs e)
    31. {
    32. button1.Text = "获取单元格信息:";
    33. button1.Size = new Size(110,23);
    34. textBox1.Size = new Size(200,21);
    35. dataGridView1.AllowUserToAddRows = false;
    36. dataGridView1.AllowUserToDeleteRows = false;
    37. dataGridView1.AllowUserToResizeColumns = false;
    38. dataGridView1.AllowUserToResizeRows = false;
    39. dataGridView1.ReadOnly = true;
    40. dataGridView1.RowHeadersVisible = false;
    41. //SqlConnection conn = new SqlConnection("Server=DESKTOP-QFENBNJ\\SQL_WEN;integrated security=SSPI;Initial Catalog=db_CSharp");
    42. SqlConnection conn_ = new SqlConnection("Server=DESKTOP-GFMO83R;integrated security=SSPI;Initial Catalog=db_EMS");
    43. SqlDataAdapter sda_ = new SqlDataAdapter("select*from tb_CNStudent", conn_);
    44. DataSet ds_ = new DataSet();
    45. sda_.Fill(ds_, "emp");
    46. dataGridView1.DataSource = ds_.Tables[0];
    47. }
    48. ///
    49. /// 获取控件中单元格的信息
    50. /// 鼠标选中想要获取的单元格,点击获取信息按钮,文本框就会显示单元格信息
    51. ///
    52. private void Button1_Click(object sender, EventArgs e)
    53. {
    54. string msg_ = String.Format("第{0}行,第{1}列", dataGridView1.CurrentCell.RowIndex,dataGridView1.CurrentCell.ColumnIndex);
    55. textBox1.Text = "选择的单元格为:" + msg_;
    56. }
    57. }
    58. }

    (2)生成效果

    2.通过EF实体模型访问SQL

    (1)源码

    1. //Form1.cs
    2. //EF获取控件单元格信息
    3. using _02_1;
    4. using System;
    5. using System.Collections.Generic;
    6. using System.ComponentModel;
    7. using System.Data;
    8. using System.Drawing;
    9. using System.Linq;
    10. using System.Text;
    11. using System.Threading.Tasks;
    12. using System.Windows.Forms;
    13. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
    14. namespace _02_1
    15. {
    16. public partial class Form1 : Form
    17. {
    18. public Form1()
    19. {
    20. InitializeComponent();
    21. }
    22. ///
    23. /// 初始化Form1
    24. ///
    25. private void Form1_Load(object sender, EventArgs e)
    26. {
    27. button1.Text = "获取单元格信息:";
    28. button1.Size = new Size(110, 23);
    29. textBox1.Size = new Size(200, 21);
    30. dataGridView1.AllowUserToAddRows = false;
    31. dataGridView1.AllowUserToDeleteRows = false;
    32. dataGridView1.AllowUserToResizeColumns = false;
    33. dataGridView1.AllowUserToResizeRows = false;
    34. dataGridView1.ReadOnly = true;
    35. dataGridView1.RowHeadersVisible = false;
    36. using (db_EMSEntities db_ = new db_EMSEntities())
    37. {
    38. dataGridView1.DataSource = db_.tb_CNStudent.ToList();
    39. };
    40. }
    41. ///
    42. /// 获取单元格信息
    43. /// Format{}中“”是格式字符串,逗号前=当前行,逗号后=当前列
    44. ///
    45. private void Button1_Click(object sender, EventArgs e)
    46. {
    47. string msg_ = String.Format("第{0}行,第{1}列", dataGridView1.CurrentCell.RowIndex, dataGridView1.CurrentCell.ColumnIndex);
    48. textBox1.Text = "选择的单元格为:" + msg_;
    49. }
    50. }
    51. }

    (2)生成效果

             生成效果与上述方法相同。但通过EF实体模型访问数据库更简单。

    三、DataGridView控件中修改数据

     1.直接编程访问SQL

            在DataGridView控件中修改数据,主要用到DataTable的ImportRow()方法和DataAdapter对象的Update()方法。实现的过程是通过DataTable的ImportRow()方法将更改后的数据复制到一个DataTable中,然后通过DataAdapter对象的Update()方法,将DataTable中的数据更新到数据库中。ImportRow()方法用于将DataRow复制到DataTable中,保留任何属性设置以及初始值和当前值

    1. 语法如下:
    2. public void ImportRow(DataRow row)
    3. row:要导入的DataRow。 
    4. void DataTable.ImportRow(DataRow row);
    5. 将DataRow复制到DataTable中,保留任何属性设置、初始值和当前值。

    (1)源码

    1. //Form1.cs
    2. //在DataGridView控件中修改数据,然后进行批量更新
    3. using System;
    4. using System.Data;
    5. using System.Data.SqlClient;
    6. using System.Drawing;
    7. using System.Windows.Forms;
    8. namespace _03
    9. {
    10. public partial class Form1 : Form
    11. {
    12. public Form1()
    13. {
    14. InitializeComponent();
    15. }
    16. SqlConnection _Conn;
    17. SqlDataAdapter _Adapter;
    18. int _CurIndex;
    19. ///
    20. /// 初始化Form1并加载SQL数据表
    21. ///
    22. private void Form1_Load(object sender, EventArgs e)
    23. {
    24. button1.Text = "提交修改";
    25. button1.Size = new Size(70, 23);
    26. dataGridView1.AllowUserToAddRows = true;
    27. dataGridView1.AllowUserToDeleteRows = true;
    28. dataGridView1.AllowUserToResizeColumns = false;
    29. dataGridView1.AllowUserToResizeRows = false;
    30. dataGridView1.ReadOnly = false;
    31. dataGridView1.RowHeadersVisible = false;
    32. dataGridView1.ColumnHeadersVisible = true;
    33. dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //全行选中
    34. dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red; //选中行红色背景
    35. //_Conn = new SqlConnection("Server=DESKTOP-GFMO83R;integrated security=SSPI;Initial Catalog=db_EMS");
    36. _Conn = new SqlConnection("Server=DESKTOP-QFENBNJ\\SQL_WEN;integrated security=SSPI;Initial Catalog=db_EMS");
    37. SqlDataAdapter sda = new SqlDataAdapter("select*from tb_CNStudent", _Conn);
    38. DataSet ds = new DataSet();
    39. sda.Fill(ds);
    40. dataGridView1.DataSource = ds.Tables[0];
    41. }
    42. ///
    43. /// 按下按钮,调用dbUpdate(),为真则消息提示=成功
    44. ///
    45. private void Button1_Click(object sender, EventArgs e)
    46. {
    47. if (DbUpdate())
    48. {
    49. MessageBox.Show("修改成功!");
    50. }
    51. }
    52. ///
    53. /// 功能:自定义更新数据库函数DbUpdate(),更新成功返回true否则返回false
    54. /// 功能:只能对同一行内的单元格进行修改。多行修改,只有最后一行修改的内容会复制到SQL
    55. /// 功能:其前面的行的修改都是虚假修改,不会复制到SQL。退出重启后修改不见。
    56. /// 虽然VS提示CommandBuiler没有用,但实际不可或缺
    57. /// 提交自上次调用DataTable.AcceptChanges()以来对该表进行的所有更改,更改到数据表
    58. ///
    59. ///
    60. /// 调用Dbconn()的结果=dtSelect,是Fill()方法填充的数据表DataTable
    61. ///
    62. private Boolean DbUpdate()
    63. {
    64. string strSQL = "select * from tb_CNStudent";
    65. DataTable dtUpdate = Dbconn(strSQL); //创建数据表dtUpdate,其内容为dtSelect
    66. DataTable dtShow = (DataTable)dataGridView1.DataSource; //获取DataGridView所显示数据复制为数据表dtShow
    67. dtUpdate.ImportRow(dtShow.Rows[_CurIndex]); //把dtShow当前行属性、值复制到dtUpdate中
    68. SqlCommandBuilder CommandBuiler = new SqlCommandBuilder(_Adapter);//适配器沟通SQL的变量
    69. _Adapter.Update(dtUpdate); //对dtUpdate的已修改,Update到数据库
    70. dtUpdate.AcceptChanges(); //所有修改更新到数据表dtUpdate
    71. return true;
    72. }
    73. ///
    74. /// 功能:返回一个已填充的数据表对象DataTable dtSelect
    75. /// 建立DataTable的方法 Dbconn():
    76. /// 打开SQL连接
    77. /// 带入实参新建适配器对象dtSelect
    78. /// Fill()方法填充dtSelect
    79. /// 关闭SQL连接
    80. ///
    81. ///
    82. /// Dbconn()形参由DbUpdate()函数的实参带入
    83. ///
    84. ///
    85. /// 返回dtSelect,是Fill()方法填充的数据表DataTable
    86. ///
    87. private DataTable Dbconn(string strSQL)
    88. {
    89. _Conn.Open ();
    90. _Adapter = new SqlDataAdapter(strSQL, _Conn);
    91. DataTable dtSelect = new DataTable();
    92. _Adapter.Fill(dtSelect);
    93. _Conn.Close();
    94. return dtSelect;
    95. }
    96. ///
    97. /// 鼠标的cellclick事件
    98. /// 鼠标点击cell单元时,触发当前行的索引号_CurIndex
    99. /// 没有行索引号_CurIndex,提交虚假的修改成功,SSMS更新后发现实际没有修改成功
    100. ///
    101. private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    102. {
    103. _CurIndex = e.RowIndex;
    104. }
    105. }
    106. }

    (2)生成效果 

            以上数据表中单元格内容为null的都应该修改为空或者添加文字,否则生成后当点击含有null的行时就会报警,导致修改失败。取消行中所有的null后,警告消失。

     2.通过EF实体模型访问SQL

    (1)源码

            以上面的工程为例,通过EF实体模型访问数据库的,修改数据表中选中行的单元格内容。为了让读者更好地理解EF实体模型访问数据库与SQL编程访问数据库在应用方面的差别,在窗体设计上增加了一些控件,程序段里也增加了测试语句(均有注释)。

    1. //Form1.cs测试用
    2. //通过EF实体模型访问数据库的,修改数据表
    3. using System;
    4. using System.Collections.Generic;
    5. using System.ComponentModel;
    6. using System.Data;
    7. using System.Data.Entity;
    8. using System.Drawing;
    9. using System.Linq;
    10. using System.Net.NetworkInformation;
    11. using System.Runtime.Serialization.Json;
    12. using System.Security.Principal;
    13. using System.Text;
    14. using System.Threading.Tasks;
    15. using System.Windows.Forms;
    16. namespace _03_1
    17. {
    18. public partial class Form1 : Form
    19. {
    20. public Form1()
    21. {
    22. InitializeComponent();
    23. }
    24. public int _ID;
    25. public int _RowIndex;
    26. public string[,] _CellContext;
    27. ///
    28. /// 初始化Form1
    29. ///
    30. private void Form1_Load(object sender, EventArgs e)
    31. {
    32. button1.Text = "提交修改";
    33. button1.Size = new Size(70, 23);
    34. dataGridView1.AllowUserToAddRows = true;
    35. dataGridView1.AllowUserToDeleteRows = true;
    36. dataGridView1.AllowUserToResizeColumns = false;
    37. dataGridView1.AllowUserToResizeRows = false;
    38. dataGridView1.ReadOnly = false;
    39. dataGridView1.RowHeadersVisible = false;
    40. dataGridView1.ColumnHeadersVisible = true;
    41. dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //全行选中
    42. dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red; //选中行红色背景
    43. using (db_EMSEntities db_ = new db_EMSEntities())
    44. {
    45. dataGridView1.DataSource = db_.tb_CNStudent.ToList();
    46. dataGridView1.Columns[3].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss"; //显示秒数格式
    47. };
    48. }
    49. ///
    50. /// 按下按键,执行修改
    51. /// 如果实例表不为空,则对选中行的cell执行修改
    52. /// 主键cell不能修改
    53. /// 如果实例是空表,其修改就用Add()方法增加到数据库
    54. ///
    55. private void Button1_Click(object sender, EventArgs e)
    56. {
    57. using (db_EMSEntities db_ = new db_EMSEntities())
    58. {
    59. tb_CNStudent _CNStudent =db_.tb_CNStudent.Where(W => W.学生编号 == _ID).FirstOrDefault(); //获取指定编号学生信息
    60. if (_CNStudent!=null)
    61. {
    62. db_.tb_CNStudent.Attach(_CNStudent);
    63. textBox1.Text = dataGridView1.Rows[_RowIndex].Cells[1].Value.ToString(); //测试
    64. _CNStudent.学生编号 = Convert.ToInt32(dataGridView1.Rows[_RowIndex].Cells[0].Value.ToString());
    65. _CNStudent.学生姓名 = dataGridView1.Rows[_RowIndex].Cells[1].Value.ToString();
    66. _CNStudent.性别 = dataGridView1.Rows[_RowIndex].Cells[2].Value.ToString();
    67. string _Str = dataGridView1.Rows[_RowIndex].Cells[3].Value.ToString();
    68. string _DateStr = Convert.ToDateTime(_Str).ToString("yyyy-MM-dd HH:mm:ss");
    69. textBox13.Text = _DateStr; //测试时间格式
    70. _CNStudent.出生年月 = DateTime.ParseExact(_DateStr, "yyyy-MM-dd HH:mm:ss", null);
    71. _CNStudent.年龄 = (int)dataGridView1.Rows[_RowIndex].Cells[4].Value; //等效语句
    72. //_CNStudent.年龄 = Convert.ToInt32(dataGridView1.Rows[_RowIndex].Cells[4].Value.ToString());
    73. _CNStudent.所在学院 = dataGridView1.Rows[_RowIndex].Cells[5].Value.ToString();
    74. _CNStudent.所学专业 = dataGridView1.Rows[_RowIndex].Cells[6].Value.ToString();
    75. _CNStudent.家庭住址 = dataGridView1.Rows[_RowIndex].Cells[7].Value.ToString();
    76. _CNStudent.统招否 = (bool)dataGridView1.Rows[_RowIndex].Cells[8].Value;//等效语句
    77. //_CNStudent.统招否 = Convert.ToBoolean(dataGridView1.Rows[_RowIndex].Cells[8].Value.ToString());
    78. _CNStudent.备注信息 = dataGridView1.Rows[_RowIndex].Cells[9].Value.ToString();
    79. db_.Entry(_CNStudent).Property("学生编号").IsModified = true;
    80. db_.Entry(_CNStudent).Property(_ => _.学生姓名).IsModified = true; //等效语句
    81. db_.Entry(_CNStudent).Property(_ => _.性别).IsModified = true; //等效语句
    82. db_.Entry(_CNStudent).Property("出生年月").IsModified = true; //依此类推
    83. db_.Entry(_CNStudent).Property("年龄").IsModified = true;
    84. db_.Entry(_CNStudent).Property("所在学院").IsModified = true;
    85. db_.Entry(_CNStudent).Property("所学专业").IsModified = true;
    86. db_.Entry(_CNStudent).Property("家庭住址").IsModified = true;
    87. db_.Entry(_CNStudent).Property("统招否").IsModified = true;
    88. db_.Entry(_CNStudent).Property("备注信息").IsModified = true;
    89. db_.SaveChanges();
    90. dataGridView1.DataSource = db_.tb_CNStudent.ToList();
    91. }
    92. else
    93. {
    94. db_.tb_CNStudent.Add(_CNStudent);
    95. db_.SaveChanges();
    96. dataGridView1.DataSource = db_.tb_CNStudent.ToList();
    97. }
    98. }
    99. }
    100. ///
    101. /// 点击单元格事件
    102. /// 定义单元格二维数组,全局变量,并由选中行的EF实体模型单元格的内容给赋值
    103. ///
    104. private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    105. {
    106. _CellContext = new string[dataGridView1.Rows.Count, dataGridView1.Columns.Count];
    107. _ID = Convert.ToInt32(dataGridView1[0, e.RowIndex].Value); //获取选中行的编号
    108. _RowIndex = dataGridView1.CurrentRow.Index;
    109. textBox12.Text = _RowIndex.ToString(); //测试行号
    110. using (db_EMSEntities db_ = new db_EMSEntities())
    111. {
    112. tb_CNStudent _CNStudent = db_.tb_CNStudent.Where(W => W.学生编号 == _ID).FirstOrDefault(); //获取指定编号学生信息
    113. //_CellContext[_RowIndex, 0] = dataGridView1.Rows[_RowIndex].Cells[0].Value.ToString(); //等效语句
    114. _CellContext[_RowIndex, 0] = _ID.ToString(); //等效语句
    115. //_CellContext[_RowIndex, 0] = _CNStudent.学生编号.ToString(); //等效语句
    116. _CellContext[_RowIndex, 1] = _CNStudent.学生姓名.ToString();
    117. _CellContext[_RowIndex, 2] = _CNStudent.性别.ToString();
    118. //_CellContext[_RowIndex, 3] = _CNStudent.出生年月.ToString(); //格式不标准1991/2/12 0:00:00
    119. _CellContext[_RowIndex, 3] = Convert.ToDateTime(_CNStudent.出生年月).ToString("yyyy-MM-dd HH:mm:ss"); //格式标准
    120. //string _DateStr = dataGridView1.Rows[_RowIndex].Cells[3].Value.ToString(); //标准格式等价语句
    121. //Convert.ToDateTime(_DateStr).ToString("yyyy-MM-dd HH:mm:ss");
    122. _CellContext[_RowIndex, 4] = _CNStudent.年龄.ToString();
    123. _CellContext[_RowIndex, 5] = _CNStudent.所在学院.ToString();
    124. _CellContext[_RowIndex, 6] = _CNStudent.所学专业.ToString();
    125. _CellContext[_RowIndex, 7] = _CNStudent.家庭住址.ToString();
    126. _CellContext[_RowIndex, 8] = _CNStudent.统招否.ToString();
    127. _CellContext[_RowIndex, 9] = _CNStudent.备注信息.ToString();
    128. textBox2.Text = _CellContext[_RowIndex, 0];
    129. textBox3.Text = _CellContext[_RowIndex, 1];
    130. textBox4.Text = _CellContext[_RowIndex, 2];
    131. textBox5.Text = _CellContext[_RowIndex, 3];
    132. textBox6.Text = _CellContext[_RowIndex, 4];
    133. textBox7.Text = _CellContext[_RowIndex, 5];
    134. textBox8.Text = _CellContext[_RowIndex, 6];
    135. textBox9.Text = _CellContext[_RowIndex, 7];
    136. textBox10.Text = _CellContext[_RowIndex, 8];
    137. textBox11.Text = _CellContext[_RowIndex, 9];
    138. string _Str = dataGridView1.Rows[_RowIndex].Cells[3].Value.ToString(); //测试
    139. string _DateStr = Convert.ToDateTime(_Str).ToString("yyyy-MM-dd HH:mm:ss");
    140. textBox13.Text = _DateStr; //测试时间格式
    141. }
    142. }
    143. }
    144. }

             源码中,第4列,出生年月,编程时和修改时要注意格式一致,有一点不一致也不会放过你的,会报警。

            按钮按下后的[行,列]值是修改后的内容,读取数据表的是修改前的内容,编程时一定要注意区别,否则也会出现警告,不让你修改SQL。

            正常设计时,不必设计DataGridView1_CellClick()。所有全局变量定义放到Button1_Click()中,取消所有测试用语句和等效语句,EF实体模型访问SQL的程序会更简便易懂。

    (2)生成效果

            Form1.cs[设计] 

            DEBUG生成 ,选中行3,修改各cell:张*飞哥哥,男子汉,2023-12-25 00:00:00,35,机电工程学院,车辆工程,吉林省长春市(不变),true,全日制。

             各个cell,想修改那个就修改哪个,不必全都修改。都不修改就不更新SQL。

             对应更新SQL后的效果。 

  • 相关阅读:
    【LeetCode-简单】121. 买卖股票的最佳时机(详解)
    c语言:深度刨析函数栈帧
    SpringBoot 中使用自定义参数解析器修改请求对象
    计算机视觉与深度学习(线性分类器、全连接神经网络)
    Camtasia Studio2023专业的电脑屏幕录像视频编辑软件
    SSH远程登录时常见问题解决
    【Hello Go】Go语言面向对象
    支付宝小程序集成mqtt兼容IOS和安卓
    Ant Design Modal模态框添加类名后样式不生效
    ②【Hash】Redis常用数据类型:Hash [使用手册]
  • 原文地址:https://blog.csdn.net/wenchm/article/details/133957576