DataGridView实现设置默认值

【目标】系统功能主要为数据录入,大量的数据要尽量减少人工操作,故设想设置一些datagridview的列为默认值

【结果】实现 1.checkbox类型,textbox类型,comBox类型 2.可以启用系统原始默认值,也可以临时改变,将当前行设为默认值,按Enter键自动跳到下一行

【注意点】若是每一列都设置了默认值,并且当前行并没有经过编辑,那么按Enter键进入下一行时,下一行成为当前行,每一列已经填好默认值,但是上一行的默认值会全部清空,所以若没有经过编辑,则不进入下一行。总之,请编辑之后进入下一行。

【完整代码】

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace UI
11 {
12     public partial class DefaultValuesNeededTest : Form
13     {
14         int num = 0;
15         bool bl; //是否以上一行为默认值,默认为否
16         public DefaultValuesNeededTest()
17         {
18             InitializeComponent();
19             checkBox1.Checked = true;
20             bl = false;
21             textBox1.Text = "大库房";
22         }
23 
24         private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
25         {
26             int count=dataGridView1.Rows.Count;
27             if (!bl||count==1)
28             {
29                 e.Row.Cells["Column1"].Value = true;
30                 e.Row.Cells["Column2"].Value = num++;
31                 e.Row.Cells["Column3"].Value = textBox1.Text;
32                 e.Row.Cells["Column4"].Value = "小李";
33                 e.Row.Cells["Column5"].Value = "kg";
34             }
35             else
36             {
37                 e.Row.Cells["Column1"].Value = dataGridView1.Rows[count - 2].Cells["Column1"].Value;
38                 e.Row.Cells["Column2"].Value = dataGridView1.Rows[count - 2].Cells["Column2"].Value;
39                 e.Row.Cells["Column3"].Value = dataGridView1.Rows[count - 2].Cells["Column3"].Value;
40                 e.Row.Cells["Column4"].Value = dataGridView1.Rows[count - 2].Cells["Column4"].Value;
41                 e.Row.Cells["Column5"].Value = dataGridView1.Rows[count - 2].Cells["Column5"].Value;
42             }
43         }
44 
45         private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
46         {
47             // 没有经过编辑则当期行为新行
48             if (!dataGridView1.CurrentRow.IsNewRow && e.KeyValue == 13)
49             {
50                 dataGridView1.Rows.Add();
51             }
52 
53           
54             if (e.KeyCode == Keys.C)
55             {
56                 int index=dataGridView1.CurrentRow.Index;
57                 dataGridView1.Rows.AddCopy(index);
58             }
59         }
60 
61         private void checkBox1_CheckedChanged(object sender, EventArgs e)
62         {
63             if (checkBox1.Checked == true)
64             {
65                 checkBox2.Checked = false;
66                 bl = false;
67             }
68         }
69 
70         private void checkBox2_CheckedChanged(object sender, EventArgs e)
71         {
72             if (checkBox2.Checked == true)
73             {
74                 checkBox1.Checked = false;
75                 bl = true;
76             }
77         }
78 
79     }
80 }

【结果展示】

原文地址:https://www.cnblogs.com/denghuachengle/p/3481066.html