本人也是在昨天2022年4月6日第一次接触DEV控件,我的师傅就给我拍下一个任务。内容如下:
1、新建.netframwork4.7桌面程序工程
2、在窗体中创建DEV的GridControl
3、设计类员工,拥有属性 员工ID 姓名 年龄 性别 所属部门 (员工ID为随机的Guid,其他属性类型自定)
4、界面添加按钮,增加。编写按钮逻辑,点击后在缓存中添加员工对象
5、将添加的对象缓存显示在GridControl中
6、实现在GridControl对缓存中员工属性的直接修改
7、界面添加按钮,删除。编写按钮逻辑,对GridControl中所选中的对象执行从缓存中删除的功能
额外:(参考已有代码编写)
1、在关闭界面时,将缓存中的员工对象保存至本地。
2、在打开界面时,读取本地的员工信息到缓存中,并在界面显示。
于是乎 我便做了如下的编写。先上截图:
功能很简单,但是我还是有一些没搞懂,纯属自己摸索的。
1. 首先得有DEV控件 ,是吧哈哈哈。。。。感觉自己是个智障。
2.附上代码,
using DevExpress.XtraEditors; using DevExpress.XtraEditors.Repository; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Grid; using System; using System.ComponentModel; using System.Data; using System.Runtime.CompilerServices; using System.Windows.Forms; namespace EmployTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); //gridView1标题 gridView1.GroupPanelText = "员工管理:"; //gridView1顶部显示添加行 gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.Top; //gridView1允许编辑 gridView1.OptionsBehavior.Editable = true; #region gridView1选中行后按快捷键 “Del” 删除 //gridControl1.ProcessGridKey += (s, e) => //{ // if (e.KeyCode == Keys.Delete ) // { // if (XtraMessageBox.Show("是否删除选中行?", "删除行对话框", MessageBoxButtons.YesNo) != // DialogResult.Yes) // return; // GridControl grid = s as GridControl; // GridView view = grid.FocusedView as GridView; // view.DeleteSelectedRows(); // } //}; #endregion } /// <summary> /// 字段属性设置并验证字段属性数据的正确性,属性变更后向客户端发出属性值已更改的通知。 /// </summary> public class Record : INotifyPropertyChanged { int id; [DisplayName("序号")] public int ID { get { return id; } set { if (id != value) { id = value; OnPropertyChanged(); } } } string text; [DisplayName("姓名")] public string Name { get { return text; } set { if (text != value) { if (string.IsNullOrEmpty(value)) throw new Exception(); text = value; OnPropertyChanged(); } } } string age; [DisplayName("年龄")] public string Age { get { return age; } set { if (age != value) { if (string.IsNullOrEmpty(value)) throw new Exception(); age = value; OnPropertyChanged(); } } } string dt; [DisplayName("性别")] public string Gender { get { return dt; } set { if (dt != value) { dt = value; OnPropertyChanged(); } } } string dt1; [DisplayName("部门")] public string Bumen { get { return dt1; } set { if (dt1 != value) { dt1 = value; OnPropertyChanged(); } } } /// <summary> /// 通知界面属性 /// </summary> public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } /// <summary> /// 生成随机字段数据 /// </summary> public class DataHelper { public static string[] names = new string[] { "小张", "小王", "小希", "小钱", "小马", "小磊", "小红" ,"小明"}; public static string[] gender = new string[] { "男", "女" }; public static string[] bumen = new string[] { "软件", "电气" }; public static BindingList<Record> GetData(int count) { BindingList<Record> records = new BindingList<Record>(); Random rnd = new Random(); for (int i = 0; i < count; i++) { int n = rnd.Next(); records.Add(new Record() { ID = i + 0, Name = names[i % names.Length], Gender = gender[i % gender.Length], Age = Convert.ToString(rnd.Next(1, 50)), Bumen = bumen[i % bumen.Length], }); }; return records; } } private void Form1_Load(object sender, EventArgs e) { //dataGrid自动在数据源中找到公共字段并创建列。 gridControl1.DataSource = DataHelper.GetData(20); //创建一个ComboBox编辑器,在“姓名”列中显示可用的姓名。 RepositoryItemComboBox riComboBox = new RepositoryItemComboBox(); riComboBox.Items.AddRange(DataHelper.names); gridControl1.RepositoryItems.Add(riComboBox); gridView1.Columns["Name"].ColumnEdit = riComboBox; //创建一个ComboBox编辑器,在“性别”列中显示可用的性别。 RepositoryItemComboBox riComboBox1 = new RepositoryItemComboBox(); riComboBox1.Items.AddRange(DataHelper.gender); gridControl1.RepositoryItems.Add(riComboBox1); gridView1.Columns["Gender"].ColumnEdit = riComboBox1; //创建一个ComboBox编辑器,在“部门”列中显示可用的部门。 RepositoryItemComboBox riComboBox2 = new RepositoryItemComboBox(); riComboBox2.Items.AddRange(DataHelper.bumen); gridControl1.RepositoryItems.Add(riComboBox2); gridView1.Columns["Bumen"].ColumnEdit = riComboBox2; } /// <summary> /// 导出excel /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Btn_Output_Click_1(object sender, EventArgs e) { SaveFileDialog fileDialog = new SaveFileDialog(); fileDialog.Title = "导出Excel"; fileDialog.Filter = "Excel文件(*.xls)|*.xls"; DialogResult dialogResult = fileDialog.ShowDialog(this); if (dialogResult == DialogResult.OK) { DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions(); gridControl1.ExportToXls(fileDialog.FileName); DevExpress.XtraEditors.XtraMessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } /// <summary> /// 增加行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddRow_Click(object sender, EventArgs e) { gridView1.AddNewRow(); } /// <summary> /// 删除行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DelRow_Click(object sender, EventArgs e) { if (XtraMessageBox.Show("请确定是否删除当前记录?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { GridControl grid = new GridControl(); GridView view = grid.FocusedView as GridView; gridView1.DeleteSelectedRows(); } } } }
注意点: 保姆级别的教程。
选中行设置。
3. 至此,所有的代码就在这里了,写了两种删除的方法。
代码下载地址:点我 提取码: 8ug9
还有很多不足,请各位大佬手下留情。