• DataGridView控件的使用


    创建dataGridView1控件

    private System.Windows.Forms.DataGridView dataGridView1;
    this.dataGridView1 = new System.Windows.Forms.DataGridView();
    
    • 1
    • 2

    1、创建列

    dataGridView1.ColumnCount = 2;
    dataGridView1.Columns[0].Name = "StartAddressColumn";
     dataGridView1.Columns[0].HeaderText = "开始地址";
    dataGridView1.Columns[1].Name = "EndAddressColumn";
     dataGridView1.Columns[1].HeaderText = "结束地址";
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、添加序号

    SolidBrush solidBrush;
    StringFormat stringFormat = new StringFormat();
    solidBrush = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
    stringFormat.Alignment = StringAlignment.Center;
    stringFormat.LineAlignment = StringAlignment.Center;
    stringFormat.FormatFlags = StringFormatFlags.NoWrap;
    private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
    {
        e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, solidBrush, new Rectangle(e.RowBounds.X, e.RowBounds.Y, dataGridView1.RowHeadersWidth, e.RowBounds.Height), stringFormat);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3、设置行头宽度

    SetRowHeadersWidth();
    private void SetRowHeadersWidth()
    {
       int line = dataGridView1.Rows.Count;
       Size size = TextRenderer.MeasureText(line.ToString(),     dataGridView1.RowHeadersDefaultCellStyle.Font);
       dataGridView1.RowHeadersWidth = size.Width + 50;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4、行数限制

    dataGridView1.AllowUserToAddRows = false;
    private void RowsCountChanged()
    {
        if (dataGridView1.Rows != null && dataGridView1.Rows.Count > 8)
        {
           dataGridView1.AllowUserToAddRows = false;
        }
        else if (!dataGridView1.AllowUserToAddRows)
        {
           dataGridView1.AllowUserToAddRows = true;
        }
    }
    
    private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
    {
       RowsCountChanged();
    }
    
    private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
    {
       RowsCountChanged();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    5、数字输入校验

    this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(Dgv_storageEditingControlShowing);
    
    public void Dgv_storageEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
         Control control = new TextBox();
         control = (TextBox)e.Control;
         if (control.Name == "")
           {
              control.KeyPress += new KeyPressEventHandler(dataGridView1_KeyPress);
           }
    }
    
    private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
    {
         if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'a' && e.KeyChar <= 'f') || (e.KeyChar >= 'A' && e.KeyChar <= 'F') || e.KeyChar == '\b')
        {
             if (e.KeyChar >= 'a' && e.KeyChar <= 'f')
            {
                e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper());
            }
            e.Handled = false;
        }
        else
        {
            e.Handled = true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    6、添加行

    private void btAddSelectedRow_Click(object sender, EventArgs e)
    {
         if (dataGridView1.Rows != null && dataGridView1.Rows.Count < 8)
             dataGridView1.Rows.Add();
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    7、删除选定行

    private void btDeleteSelectedRow_Click(object sender, EventArgs e)
    {
          int selectedRowsCount = dataGridView1.SelectedRows.Count;
          int[] ia = new int[selectedRowsCount];
          for(int i = 0; i < selectedRowsCount; i++)
           {
               int id = dataGridView1.SelectedRows[i].Index;
               ia[i] = id;   
           }
           for(int i = 0; i < ia.Length; i++)
          {
             dataGridView1.Rows.RemoveAt(ia[i]);
         }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    8、读取数据

    private void button2_Click(object sender, EventArgs e)
    {
       List<string> str = new List<string>();
       for(int i=0;i< dataGridView1.Rows.Count; i++)
       {
          string s = dataGridView1.Rows[i].Cells[0].Value.ToString() + " " + dataGridView1.Rows[i].Cells[1].Value.ToString();
          str.Add(s);
       }          
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    C++中为什么要创建堆区域数据
    带权有向图最短路径之Dijkstra和Floyd
    职业PDF标准 Python 下载器-CSDN
    JDK线程池的总结
    G. SlavicG‘s Favorite Problem(树的遍历DFS,BFS均可)
    js — 原生轮播图的制作
    上位机与下位机通讯方式(转载)
    2024-05-16 Proxmox VE三种控制台(共享剪切版,文件拖拽)
    本地浏览器打开远程服务器上的Jupyter Notebook
    使用Excel 表示汽车、摩托车10年免检时间、非常清晰。
  • 原文地址:https://blog.csdn.net/qq_27474555/article/details/126850961