• DataGridView控件1——手动添加数据,遍历数据


    DataGridView控件

    用于网格形式显示数据,行和列由用户自定义。

    应用,显示数据,管理数据,操作数据

     

    DataGridView的组成:行,列,单元格

    行:DataGridViewRow,DataGridViewRowCollection,Rows

    列:DataGridViewColumn,DataGridViewColumnCollection,Columns

    FillWeight,ColumnType,Name,DataPropertyName,HeaderText,ReadOnly,Visible

    单元格:DataGridViewCell,Value,Selected,RowIndex,ColumnIndex,FormattedValue

     

    DataGridView常用属性:

    AllowUserToAddRows

    AllowUserToDeleteRows

    AutoSizeColumnMode

    ColumnHeadersHight

    ColumnHeadersVisible

    Columns

    DataSource 数据源——DataTable或List集合

    EnableHeaderVisualStyle

    MultiSelect

    RowHeadersVisible

    SelectionMode


     

     

    知识点1:

     手动添加空行,手动添加一行数据,遍历行数据(无数据源)

      代码如下:

    using System;
    using System.Windows.Forms;
    
    namespace ControlsTest
    {
        public partial class FormDataGridView : Form
        {
            public FormDataGridView()
            {
                InitializeComponent();
    
            }
    
            private void btnAdd_Click(object sender, EventArgs e)
            {
                //添加一个空行
                dgv1.Rows.Add();//方法1
                dgv1.AllowUserToAddRows = true;//方法2
            }
    
            private void btnAddData_Click(object sender, EventArgs e)
            {
                //代码添加一行数据
                //方法1
                dgv1.Rows.Add(new object[] { 1000, "黎明", true, "12345678" });
                //方法2
                DataGridViewRow row = new DataGridViewRow();
                DataGridViewCell cell0 = new DataGridViewTextBoxCell();
                cell0.Value = 1001;
                DataGridViewCell cell1 = new DataGridViewTextBoxCell();
                cell1.Value = "王强";
                DataGridViewCell cell2 = new DataGridViewCheckBoxCell();
                cell2.Value = false;
                DataGridViewCell cell3 = new DataGridViewTextBoxCell();
                cell3.Value = "87654321";
                row.Cells.AddRange(cell0, cell1, cell2, cell3);
                dgv1.Rows.Add(row);
    
            }
    
            private void btnGet_Click(object sender, EventArgs e)
            {
    
                //遍历行,获取行中单元格的数据
                foreach (DataGridViewRow row in dgv1.Rows)
                {
                    int id = int.Parse(row.Cells[0].Value.ToString());
                    string name = row.Cells[1].Value.ToString();
                    bool state = Convert.ToBoolean(row.Cells[2].Value);
                    string phone = row.Cells[3].Value.ToString();
                    MessageBox.Show(id.ToString() + name + state.ToString() + phone);
                }
            }
        }
    }

     

  • 相关阅读:
    想更改Github仓库中的某个文件结构
    记git一些基本用法
    剑指Offer-Python(16-20)
    剑指Offer-Python(11-15)
    初次使用flask
    Python的Cmd模块的简易运用学习
    SQL-全称量词查询
    线段树模板1
    OJ输入输出超时(C++)
    二叉查找树(BST)定义
  • 原文地址:https://www.cnblogs.com/hanzq/p/16813084.html
Copyright © 2020-2023  润新知