zoukankan      html  css  js  c++  java
  • dataGridView笔记

    最近用dataGridView比较多,先把代码备份在这里,有时间系统总结一下

    using FindId.DAL;
    using FindId.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace FindId
    {
        
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
                //bin = new delegateBin(DataBin);
            }
            public static bool IsClosed = false;
            private void Form2_Load(object sender, EventArgs e)
            {
                CreateView();            
            }
            public void Test()
            {
                //bin = new delegateBin(DataBin);
            }
            public static delegateBin bin;
            public void DataBin()
            {
                DAL_Goods dal = new DAL_Goods();
                string findGroupID = tbFindGroupID.Text.Trim();            
                List<Model_Goods> dlist = (List<Model_Goods>)dal.ListWhere(findGroupID);
                dataGridView1.Refresh();
                dataGridView1.DataSource = dlist;            
            }
    
            private void CreateView()
            {
                dataGridView1.AutoGenerateColumns = false;
                DataGridViewLinkColumn linkID = new DataGridViewLinkColumn();
                linkID.Name = "linkID";
                linkID.HeaderText = "id";
                linkID.Width = 1;
                linkID.DataPropertyName = "ID";
                dataGridView1.Columns.Add(linkID);
    
                //DataGridViewTextBoxColumn dgtbGroupID = new DataGridViewTextBoxColumn();
                //dgtbGroupID.DataPropertyName = "GroupID";
                //dgtbGroupID.Name = "tbGroupID";
                //dgtbGroupID.HeaderText = "产品ID";
                //dataGridView1.Columns.Add(dgtbGroupID);
    
    
                DataGridViewTextBoxColumn dgtbGoodsTitle = new DataGridViewTextBoxColumn();
                dgtbGoodsTitle.DataPropertyName = "GoodsTitle";
                dgtbGoodsTitle.Name = "dgtbGoodsTitle";
                dgtbGoodsTitle.HeaderText = "产品名称";
                dgtbGoodsTitle.Width = 200;
                dataGridView1.Columns.Add(dgtbGoodsTitle);
    
                DataGridViewTextBoxColumn dgtbAttributeTitle = new DataGridViewTextBoxColumn();
                dgtbAttributeTitle.DataPropertyName = "AttributeTitle";
                //dgtbAttributeTitle.CellTemplate
                dgtbAttributeTitle.Name = "dgtbAttributeTitle";
                dgtbAttributeTitle.HeaderText = "属性名称";
                dataGridView1.Columns.Add(dgtbAttributeTitle);
    
                DataGridViewTextBoxColumn dgtbAttribute = new DataGridViewTextBoxColumn();
                dgtbAttribute.DataPropertyName = "Attribute";
                dgtbAttribute.Name = "dgtbAttribute";
                dgtbAttribute.HeaderText = "内容";
                dataGridView1.Columns.Add(dgtbAttribute);
    
    
                DataGridViewLinkColumn linkDelete = new DataGridViewLinkColumn();
                linkDelete.Text = "删除";
                linkDelete.Name = "linkDelete";
                linkDelete.HeaderText = "删除";
                linkDelete.Width = 40;
                linkDelete.UseColumnTextForLinkValue = true;
                dataGridView1.Columns.Add(linkDelete);
            }
    
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                var Column = dataGridView1.Columns[e.ColumnIndex];
                if (Column.Name == "linkDelete")
                {
                    var cellId = dataGridView1.Rows[e.RowIndex].Cells[0];
                    string temp = cellId.EditedFormattedValue.ToString();
                    int id;
                    int.TryParse(temp, out id);
                    if (MessageBox.Show("确认删除?", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        new DAL_Goods().Delete(new Model_Goods() { ID = id });                   
                        DataBin();
                    }   
                }
                else
                {
                    //var cellSelected = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    var cellSelected = dataGridView1.Rows[e.RowIndex].Cells[3];
                    string txt = cellSelected.EditedFormattedValue.ToString();
                    MessageBox.Show(txt);
                }
            }
            private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                //更新
                var cellSelected = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                string txt = cellSelected.EditedFormattedValue.ToString();
    
                var cellId = dataGridView1.Rows[e.RowIndex].Cells[0];
                string temp = cellId.EditedFormattedValue.ToString();
                int id;
                int.TryParse(temp, out id);
    
                DAL_Goods dal = new DAL_Goods();
                Model_Goods model = new Model_Goods();
    
                var ColumnsSelected = dataGridView1.Columns[e.ColumnIndex];
                if (ColumnsSelected.Name == "dgtbGoodsTitle")
                {
                    model.GoodsTitle = txt;
                }
                else if (ColumnsSelected.Name == "dgtbAttributeTitle")
                {
                    model.AttributeTitle = txt;
                }
                else if (ColumnsSelected.Name == "dgtbAttribute")
                {
                    model.Attribute = txt;
                }
                model.ID = id;
                bool b = dal.UpdateAutoCreateSqlText(model);
                //MessageBox.Show(string.Format("vale = {0}  id = {1} is{2}",txt,id,b));
            }
    
            private void btnSave_Click(object sender, EventArgs e)
            {
                Model_Goods model = new Model_Goods();
                model.GroupID = tbGroupID.Text.Trim();
                model.GoodsTitle = tbGoodsTitle.Text.Trim();
                model.AttributeTitle = tbAttributeTitle.Text.Trim();
                model.Attribute = tbAttribute.Text.Trim();
                DAL_Goods dal = new DAL_Goods();
                dal.AddNew(model);
                DataBin();
    
            }
            private void btnFindGroupID_Click(object sender, EventArgs e)
            {
                DataBin();
            }
    
    
    
    
    
    
    
    
    
    
    
           //======================================以下是试验代码 上边的是整理好的可用代码==========================
    
            private void NewMethod1()
            {
                //DataGridView控件用法(二):为每行记录最后加“编辑”-“删除”按钮列 - lllljz的专栏 - CSDN博客  https://blog.csdn.net/lllljz/article/details/7718201
    
    
                ////为每行数据增加编辑列。
                dataGridView1.AutoGenerateColumns = false; // //设定列不能自动作成
                DataGridViewLinkColumn dlink = new DataGridViewLinkColumn();////创建一个DataGridViewLinkColumn列
                dlink.Text = "编辑";//添加的这列的显示文字,即每行最后一列显示的文字。
                dlink.Name = "linkEdit";
                dlink.HeaderText = "编辑";//列的标题
                dlink.DataPropertyName = "ID";
               
                //dlink.UseColumnTextForLinkValue = true;//上面设置的dlink.Text文字在列中显示
                dataGridView1.Columns.Add(dlink);//将创建的列添加到dataGridView1中
    
    
                ////同上方法为每条记录创建“删除”超链接
                DataGridViewLinkColumn dlink2 = new DataGridViewLinkColumn();
                dlink2.Text = "删除";
                dlink2.Name = "linkDelete";
                dlink2.HeaderText = "删除";
                dlink2.UseColumnTextForLinkValue = true; 
                dataGridView1.Columns.Add(dlink2);
    
    
                ////同上方法为每条记录创建“查看”超链接
                DataGridViewButtonColumn btn1 = new DataGridViewButtonColumn();
                btn1.Text = "查看";
                btn1.Name = "linkView";
                btn1.HeaderText = "查看";
                btn1.UseColumnTextForButtonValue = true;
                btn1.Width = 66;
                dataGridView1.Columns.Add(btn1);
    
                //DataGridViewComboBoxColumn dCombo = new DataGridViewComboBoxColumn();
    
                //dCombo.Items.Add("编号");
                //dCombo.Items.Add("产品ID");
                //dCombo.Items.Add("属性名");
                //dCombo.Items.Add("属性");
                //dCombo.Name = "dCombo";
                ////dcombo.DisplayIndex = 1;	//该列显示的位置
                ////dCombo.HeaderText = "角色";
                //dCombo.DataPropertyName = "GroupID";//绑定的列
                //dCombo.DataSource = dlist;
                //dCombo.DataPropertyName = "GroupID";//绑定的列
    
                DataGridViewTextBoxColumn tb1 = new DataGridViewTextBoxColumn();
                tb1.DataPropertyName = "GroupID";
                tb1.Name = "产品ID";
                dataGridView1.Columns.Add(tb1);
    
    
                DataGridViewTextBoxColumn tb2 = new DataGridViewTextBoxColumn();
                tb2.DataPropertyName = "GoodsTitle";
                tb2.Name = "产品名称";
                tb2.Width = 500;
    
                dataGridView1.Columns.Add(tb2);
    
                //绑定数据库的值时使用以下属性
                DAL_Goods dal = new DAL_Goods();
                List<Model_Goods> dlist = (List<Model_Goods>)dal.ListWhere("124454445744");
    
                //dataGridView1.
                dataGridView1.DataSource = dlist;
            }
    
            private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
            {
                //C# DataGridView添加右键菜单等技巧 --转 - 晔 - 博客园  https://www.cnblogs.com/liye/archive/2010/09/29/1838709.html
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex >= 0)
                    {
                        //若行已是选中状态就不再进行设置
                        if (dataGridView1.Rows[e.RowIndex].Selected == false)
                        {
                            dataGridView1.ClearSelection();
                            dataGridView1.Rows[e.RowIndex].Selected = true;
                        }
                        //只选中一行时设置活动单元格
                        if (dataGridView1.SelectedRows.Count == 1)
                        {
                            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        }
                        //弹出操作菜单
                        contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                    }
                    var cellId = dataGridView1.Rows[e.RowIndex].Cells[0];
                    string temp = cellId.EditedFormattedValue.ToString();
                   
                    int.TryParse(temp, out id);
                    //MessageBox.Show(id.ToString());
    
                }
            }
            int id;
            private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                //MessageBox.Show(id.ToString());
                FormEdit fedit = new FormEdit(id);
                fedit.ShowDialog();
                
            }
    
            private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("确认删除?", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    new DAL_Goods().Delete(new Model_Goods() { ID = id });
                    DataBin();
                }   
            }
    
            private void btnAddNew_Click(object sender, EventArgs e)
            {
                FormAddNew frAdd = new FormAddNew();
                frAdd.ShowDialog();
            }
    
    
    
       
    
    
    
    
    
        }
    }
    

      

  • 相关阅读:
    linux下解压命令大全
    位运算的技巧
    逆元
    asp.net core 教程(五)
    day7--面向对象进阶(内含反射和item系列)
    day6--面向对象初识
    day6--二分查找法
    day6--递归函数
    day5--装饰器函数的信息打印,迭代器,生成器,列表推导式,内置函数
    python零散补充与总结
  • 原文地址:https://www.cnblogs.com/lkf18/p/10503137.html
Copyright © 2011-2022 走看看