• winfrom DataGridView 总结


    1. 想让上面的每一列都自动调整并塞满整个控件

      

    2. 增加序列号

      在RowPostPaint事件中,添加如下代码

      

         SolidBrush b = new SolidBrush(this.dgvleft.RowHeadersDefaultCellStyle.ForeColor);
                e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dgvleft.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
         

    3. 将DataGridView的数据转成DataTable函数

      

    public DataTable GetDgvToTable(DataGridView dgv)
            {
                DataTable dt = new DataTable();
    
                // 列强制转换
                for (int count = 0; count < dgv.Columns.Count; count++)
                {
                    DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
                    dt.Columns.Add(dc);
                }
    
                // 循环行
                for (int count = 0; count < dgv.Rows.Count; count++)
                {
                    DataRow dr = dt.NewRow();
                    for (int countsub = 0; countsub < dgv.Columns.Count; countsub++)
                    {
                        dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value);
                    }
                    dt.Rows.Add(dr);
                }
                return dt;
            }

    4. 鼠标上事件

      

        private void Ups()
            {
                try
                {
                    dgvright.Rows.Clear();
                    int nowrow = dgvleft.CurrentRow.Index;
                    int prvrow;
                    if (nowrow > 0)
                    {
                        prvrow = nowrow - 1;
                        dgvleft.CurrentCell = dgvleft.Rows[prvrow].Cells[0];
    
                        string value = dgvleft.Rows[prvrow].Cells["put_Order"].Value.ToString();
                        AddLeftData(value);
                    }
                    else
                    {
                        MessageBox.Show("最顶端");
                    }
                }
                catch { }
    
            }
    
            private void Downs()
            {
                try
                {
                    dgvright.Rows.Clear();
                    int nowrow = dgvleft.CurrentRow.Index;
                    int prvrow;
                    if (nowrow < dgvleft.Rows.Count - 1)
                    {
                        prvrow = nowrow + 1;
                        dgvleft.CurrentCell = dgvleft.Rows[prvrow].Cells[0];
    
                        object value = dgvleft.Rows[prvrow].Cells["put_Order"].Value;
                        if (value != null)
                        {
                            if (value.ToString() != "")
                            {
                                AddLeftData(value.ToString());
                            }
                        }
    
                    }
                    else
                    {
                        MessageBox.Show("最后一行");
                    }
                }
                catch { }
            }
    喜欢的朋友请帮忙点个赞!!!
  • 相关阅读:
    [0] 需求管理工具CaliberRM
    [0] 自动化测试工具QTP
    [0] UML建模工具(StarUML)
    [0] C# 扩展方法(Extension Method)
    [0] 统一软件过程(RUP)
    [0] XP敏捷开发
    关于ES6的let,const与var之间的三生三世
    lumen手记:自定义Validate表单验证
    window下python安装pip,切换为国内阿里云镜像
    Lumen开发:简单实现auth用户认证
  • 原文地址:https://www.cnblogs.com/mchuang/p/5467754.html
Copyright © 2020-2023  润新知