1.不显示第一个空白列
RowHeaderVisible属性设置为false

2.点击cell选取整行
SelectinModel属性FullRowSelect
RowSelectinModel属性设置
或用CellClick事件也可以         //整行选取
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //e.RowIndex > -1否则点击header也是叫一列
            if (dataGridView1.Rows.Count > 0 && e.RowIndex > -1)
            {
                //MessageBox.Show(e.RowIndex.ToString());
                dataGridView1.Rows[e.RowIndex].Selected = true;
            }
        }
3.添加按钮图片按钮列事件
按钮上要显示文字要设置Text属性。默认是没有值的。但还没搞清楚,为什么DataPropertyName绑定字段,不显示。所以想获得如ID这些识别记录的值只有放到其他列了。
        //按钮事件
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
            }
        }
4.添加外部按钮取得选取列
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                MessageBox.Show(dataGridView1.SelectedRows[0].Cells[1].Value.ToString());
            }
        }
5.其他设置的。
是否允许多行选取:MultiSelect 。
不自动绑定数据表里的字段。
让最后一列宽度延伸满适应上级的宽度:设置改列AutoSizeModel为Fill

6 //dataGridView 删除某一列的操作
 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == -1)
            {
                return;
            }
            string action = dataGridView1.Columns[e.ColumnIndex].Name;//操作类型
            if (action == "DelOrder")
            {
                if (MessageBox.Show("确定删除吗?", "提示", MessageBoxButtons.OKCancel,MessageBoxIcon.Warning) == DialogResult.OK)
                {                 
                    string ID = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                    SellBiClass.DelRules(ID);

}
            }
            NewLoad();
        }
   
        void NewLoad()
        {           
            dataGridView1.DataSource = SellBiClass.GetRules();
        }
7 //dataGridView 格式化每一列
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 7) //哪一列
            {
                if (e.Value.ToString()=="1")
                {
                    e.Value = "特定比例";              
                }
                else
                {
                    e.Value = "默认比例";
                    e.CellStyle.ForeColor = Color.Red;
                }
            }

}
8  获得dataGridView 某一条记录的主键id  this.dataGridView1[第几列, this.dataGridView1.CurrentCell.RowIndex].Value.ToString()
9 不显示出dataGridView1的最后一行空白 dataGridView1.AllowUserToAddRows = false; 设置上这条语句即可黑色头发

10 contextMenuStrip 属性:当用记右击该控件时显示的快捷方式.

11导出excel 

private void pictureBox4_Click(object sender, EventArgs e)
        {
            #region 导出
            if (dataGridView1.Rows.Count > 0)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.DefaultExt = "xls";
                saveFileDialog.Filter = "EXCEL文件(*.XLS)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.FileName = "客户信息";
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.CreatePrompt = true;
                saveFileDialog.Title = "导出到EXCEL";
                saveFileDialog.ShowDialog();
                if (saveFileDialog.FileName == "")
                    return;
                Stream myStream;
                myStream = saveFileDialog.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string str = "";
                try
                {
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        if (i > 0)
                        {
                            str += "\t";
                        }
                        str += dataGridView1.Columns[i].HeaderText;
                    }
                    sw.WriteLine(str);
                    for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {
                        string tempStr = "";
                        for (int k = 0; k < dataGridView1.Columns.Count; k++)
                        {
                            if (k > 0)
                            {
                                tempStr += "\t";
                            }
                            tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
                        }
                        sw.WriteLine(tempStr);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
            #endregion
        }

12.计算总记录数 (dataGridView1.Rows.Count>0) dataGridView1.Rows.Count>-1包括标题列

13使用button控件进行修改dataGridView中的某一行数据或删除某一行记录
 private void pictureBox2_Click(object sender, EventArgs e)
        {
            #region 赋值
            if (dataGridView1.Rows.Count > 0)
            {
                EmpInfo empInfo = new EmpInfo();
                int index = dataGridView1.CurrentRow.Index;

//记录复制到文本框
                empInfo.EmpId = dataGridView1.Rows[index].Cells[0].Value.ToString();
                empInfo.EmpName = dataGridView1.Rows[index].Cells[1].Value.ToString();
                empInfo.EmpSex = dataGridView1.Rows[index].Cells[2].Value.ToString();
                empInfo.EmpPhone = dataGridView1.Rows[index].Cells[3].Value.ToString();
                empInfo.EmpDate = Convert.ToDateTime(dataGridView1.Rows[index].Cells[4].Value);
                empInfo.EmpPhone = dataGridView1.Rows[index].Cells[5].Value.ToString();
                empInfo.EmpEdu = dataGridView1.Rows[index].Cells[6].Value.ToString();
                empInfo.EmpMarriage = dataGridView1.Rows[index].Cells[7].Value.ToString();
                empInfo.EmpBirth = Convert.ToDateTime(dataGridView1.Rows[index].Cells[8].Value.ToString());
                empInfo.EmpCard = dataGridView1.Rows[index].Cells[9].Value.ToString();
                empInfo.EmpAddress = dataGridView1.Rows[index].Cells[10].Value.ToString();
                empInfo.EmpRemark = dataGridView1.Rows[index].Cells[11].Value.ToString();
                EmpNews empNews = new EmpNews(empInfo);
                empNews.ShowDialog();
                dataGridView1.DataSource = HXX.SeleEmp();
            }
            #endregion
        }

private void pictureBox3_Click(object sender, EventArgs e)
        {
            #region 删除
            if (dataGridView1.Rows.Count > 0)
            {
                int index = dataGridView1.CurrentRow.Index;
                string EmpId = dataGridView1.Rows[index].Cells[0].Value.ToString();
                if (MessageBox.Show("删除后将不能恢复!", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    HXX.deleEmp(EmpId);
                    dataGridView1.DataSource = HXX.SeleEmp();
                }
            }
            #endregion
        }

C# WinForm dataGridView 技巧小结的更多相关文章

  1. 关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案

    上周在开发Winform 项目中,我曾遇到一个看似简单,但一直都没有解决的问题,那就是:设置winform DataGridView控件的行DefaultCellStyle,但却没有任何变化,我也曾求 ...

  2. C#实现WinForm DataGridView控件支持叠加数据绑定

    我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支 ...

  3. PowerDesigner实用技巧小结(3)

    PowerDesigner实用技巧小结(3) PowerDesigner 技巧小结 sqlserver数据库databasevbscriptsqldomain 1.PowerDesigner 使用 M ...

  4. PowerDesigner实用技巧小结(2)

    PowerDesigner实用技巧小结 1.ORACLE数据库建模时,由于ORACLE的表名.字段名如果是小写会有一定的麻烦,需要将小写转化为大写? (1)在打开pdm的情况下,进入Tools-Mod ...

  5. .Net 2.0实例学习:WebBrowser页面与WinForm交互技巧

    原文:.Net 2.0实例学习:WebBrowser页面与WinForm交互技巧 最近看到博客园入门教学文章比较流行,自己最近又偷懒比较多,没啥心得,不妨写一篇没啥深度的入门文章吧. 话说有了WebB ...

  6. WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

  7. C# winform DataGridView 常见属性

    C# winform DataGridView 属性说明① 取得或者修改当前单元格的内容 ② 设定单元格只读 ③ 不显示最下面的新行 ④ 判断新增行 ⑤ 行的用户删除操作的自定义 ⑥ 行.列的隐藏和删 ...

  8. CentOS系统中的passwd命令实用技巧小结

    这篇文章主要介绍了Linux系统中的passwd命令实用技巧小结,是Linux入门学习中的基础知识,需要的朋友可以参考下   先来回顾一下passwd命令的基本用法: Linux passwd命令用来 ...

  9. Winform Datagridview 单元格html格式化支持富文本

    Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...

随机推荐

  1. 字典实体类:DictionaryEntry类

    DictionaryEntry类是一个字典集合,主要包括的内容是键/值对.这样的组合方式能够方便地定位数据,当中的"键"具备唯一性,类似于数据库中的"id",一 ...

  2. HDSF主要节点解说(二)工作原理

    HDFS(Hadoop Distributed File System )Hadoop分布式文件系统. 是依据google发表的论文翻版的.论文为GFS(Google File System)Goog ...

  3. hdu1828(线段树——矩形周长并)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 分析:与面积不同的地方是还要记录竖的边有几个(num记录),并且当边界重合的时候需要合并(用lb ...

  4. MVAPI第一个版本架构图

    MVAPI采用矢量与栅格结合的方式进行移动地图的显示. 进过几个月,目前终于可以完成基本的地图显示及操作功能.还有待实现的是各种性能及效果优化.3D地物等. 发一个1.0的架构图留存一下.(虽然目前还 ...

  5. Linux下OpenCV的环境搭建(转)

    OpenCV is the most popular and advanced code library for Computer Vision related applications today, ...

  6. 动态加载资源文件(ResourceDictionary)

    原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...

  7. Redis安装及简单測试

    摘要: Redis是眼下业界很受到欢迎的一个内存数据库,一般用作系统的中间缓存系统,用以提升总体商业系统的吞吐量和响应速度.本文将简要介绍安装的主要过程以及给出一个简要的測试代码. 1.  系统环境和 ...

  8. nyoj 228 士兵杀死(五岁以下儿童)【树状数组】

    分析:这个问题问的是,因为它是一个单独的更新.因此,让我们更新,然后在c[i]表现为1~i之间,还原之后看起来像一个. #include <cstdio> #include <cst ...

  9. JS脚本加载与执行对性能的影响

    高性能JavaScript-JS脚本加载与执行对性能的影响 在web产品优化准则中,很重要的一条是针对js脚本的加载和执行方式的优化.本篇文章简单描述一下其中的优化准则. 1. 脚本加载优化 1.1 ...

  10. C++使用函数模板

    函数模板: 函数模板是蓝图或处方功能,编译器使用其发电功能系列中的新成员. 第一次使用时,新的功能是创建.从功能模板生成的函数的实例称为模板或模板的实例.函数模板的开始是keywordtemplate ...