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

  

dataGridView笔记的更多相关文章

  1. C#控件DataGridView笔记

    1.列排序问题,点击列标题行时列会自动排序导致表格混乱.解决办法: 2.改变行高 改变列头行高 ColumnHeadersHeaderSize属性设为 EnableResizing ColumnHea ...

  2. 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性

    基于.net的分布式系统限流组件   在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...

  3. 研二下学期做的第一个项目(主要关于datagridview的一些笔记)

    首先是行标题列rowheader dataGridView1.TopLeftHeaderCell.Value = "details"; ______________________ ...

  4. 学习笔记 DataGridView数据导出为Excel

    DataGridView数据导出为Excel   怎样把WinForm下的“DGV”里的绑定数据库后的数据导出到Excel中. 比如:在窗体里有个一“DGV”,DataGridView1,绑定了数据源 ...

  5. DataGridView控件-学习笔记总结

    1.GridColor属性用来获取或设置网格线的颜色 dataGridView1.GridColor=Color.Blue; 2.设置宽度 .高度 dataGridView1.Columns[].Wi ...

  6. easyUi 学习笔记 (二 ) 使用tabs 里datagridview 发送ajax请求 不访问后台的问题

    这个BUG 我花了一个半小时, 还是看不出哪里的问题,  于是就百度到这么一段话,我需要记住 <================================================= ...

  7. WinForm笔记1:TextBox编辑时和DataGridView 单元格编辑时 的事件及其顺序

    TextBox 编辑框 When you change the focus by using the mouse or by calling the Focus method, focus event ...

  8. [开发笔记]-DataGridView控件中自定义控件的使用

    最近工作之余在做一个百度歌曲搜索播放的小程序,需要显示歌曲列表的功能.在winform中采用DataGirdView来实现. 很久不写winform程序了,有些控件的用法也有些显得生疏了,特记录一下. ...

  9. 让DataGridView显示行号

          http://www.cnblogs.com/JuneZhang/archive/2011/11/21/2257630.html 为了表示行号,我们可以在DataGridView的RowP ...

随机推荐

  1. goroutine的意义与实现

    goroutine的意义与实现 goroutine存在的意义 goroutine是用于实现GO的并发的,而不是并行.此处的并发指的是一套管理.调度.执行goroutine的过程. 并行的性能更高,可以 ...

  2. showdoc app接口文档编写利器

    通过朋友介绍,才知道有这么好的一个在线接口编写文档开源项目,非常感谢原作者的贡献 ShowDoc介绍 关于ShowDoc的介绍,请访问:http://blog.star7th.com/2015/11/ ...

  3. iOS开发,这样写简历才能让大厂面试官看重你!

    前言: 对于职场来说,简历就如同门面.若是没想好,出了差错,耽误些时日倒不打紧,便是这简历入不了HR的眼,费力伤神还不能觅得好去处,这数年来勤学苦练的大好光阴,岂不辜负? 简历,简而有力.是对一个人工 ...

  4. Java如何计算一个程序的运行时间

    话不多说 直接看代码 package com.mowcode; /** * * @ClassName: Code_01_ProjectTime * @Description: 拿到程序运行时间 * @ ...

  5. Springboot 项目启动后执行某些自定义代码

    Springboot 项目启动后执行某些自定义代码 Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRun ...

  6. Oracle 11g 服务启动/关闭 及 DB dump 导入

    本地启动Oracle 服务脚本 由于本地机子安装了Oracle后,会自动启动一些默认的Oracle服务,这样子会导致机子比较慢.所以需要改成手动启动/关闭服务. 即用即开,不用就关. 开启的脚本: O ...

  7. django数据库迁移报错

    当输入C:\Users\太阳之泪\b2p\my_pro>python manage.py makemigrations shop 出现一下情况 然后输入C:\Users\太阳之泪\b2p\my_ ...

  8. 将Chrome插件Momentum背景图片设为桌面壁纸

    Momentum简介 Momentum插件是一款自动更换壁纸,自带时钟,任务日历和工作清单的chrome浏览器插件.官方的解释就是:替换你 Chrome 浏览器默认的“标签页”.里面的图片全部来自50 ...

  9. The base and high address of the custom IP are not correctly reflected in xparameters.h in SDK

    This issue has been observed in 2015.3, 2015.4, and 2015.4.1 builds of Vivado. When you create and a ...

  10. SimpleDateFormat 常用用法

    1.SimpleDateFormat函数语法:                   G 年代标志符          y 年          M 月          d 日          h ...