zoukankan      html  css  js  c++  java
  • 【WinForm】DataGridView使用小结

    一、数据


    1、填充数据
    https://blog.csdn.net/whuarui2010/article/details/9141349

                //以下采用两种方法:
                //第一种采用DataSource的方式赋值,显示到dataGridView1
                //第二种方式采用foreach循环遍历逐行赋值,显示到dataGridView2上
                //优缺点:
                //第一种,显示速度快,只能显示数据源中的数据,不够灵活
                //第二种,显示的数据灵活,数据量大时,填充数据慢
     
                //1.获取数据
                DataTable dt = new DataTable();
                dt = GetTargetDatas();
    
                //2.赋值,第一种方法
                FillDataGridViewWithDataSource(dataGridView1, dt);
     
                //2.赋值,第二种方法
                FillDataGridViewWithForeach(dataGridView2, dt);
      private void FillDataGridViewWithDataSource(DataGridView dataGridView,DataTable dTable)
            {
                //1.清空旧数据
                dataGridView.Rows.Clear();
                //2.填充新数据
                if (dTable != null && dTable.Rows.Count > 0)
                {
                    //设置DataGridView列数据
                    dataGridView.Columns["ITEM_NO"].DataPropertyName = "ITEM_NO";
                    dataGridView.Columns["ITEM_NAME"].DataPropertyName = "ITEM_NAME";
                    dataGridView.Columns["INPUT_CODE"].DataPropertyName = "INPUT_CODE";
     
                    //设置数据源,部分显示数据
                    dataGridView.DataSource = dTable;
                    dataGridView.AutoGenerateColumns = false;
                }
            }
    
    
     private void FillDataGridViewWithForeach(DataGridView dataGridView, DataTable dTable)
            {
                //1.清空旧数据
                dataGridView.Rows.Clear();
                //2.赋值新数据
                foreach (DataRow row in dTable.Rows)
                {
                    int index = dataGridView.Rows.Add();
                    dataGridView.Rows[index].Cells["ITEM_NO2"].Value = row["ITEM_NO"];
                    dataGridView.Rows[index].Cells["ITEM_NAME2"].Value = row["ITEM_NAME"];
                    dataGridView.Rows[index].Cells["INPUT_CODE2"].Value = row["INPUT_CODE"];
                }
            }


    2、DataGridView限制单元格输入数字、限制输入长度
    https://blog.csdn.net/haoduo123456789001/article/details/54944757

     

    //编写DataGridView的EditingControlShowing事件:
    
    TextBox control;    //定义输入框控件对象
    
    private  void  dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        //只对TextBox类型的单元格进行验证
        if (e.Control.GetType().BaseType.Name == "TextBox") 
        {
            control = new TextBox();
            control = (TextBox)e.Control;
            if (control.Text == "0")    //需要限制输入数字的单元格
            {
                control.KeyPress += new KeyPressEventHandler(control_KeyPress);
            }
            else
            {
                //非数字类型单元格
                control.Leave += new EventHandler(control_Leave);
            }
        }
    }
    
     
    void control_KeyPress(object sender, KeyPressEventArgs e)
    {
        //限制只能输入-9的数字,退格键,小数点和回车
    
        if (((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57) || e.KeyChar == 13 || e.KeyChar == 8 || e.KeyChar == 46)
        {
            e.Handled = false;
        }
        else
        {
            e.Handled = true;
            MessageBox.Show("只能输入数字!");
        }
    }
    
    void control_Leave(object sender, EventArgs e)
    {
        //如果需要限制字符串输入长度
        if (control.Text.Length != 11)
        {
            MessageBox.Show("只能为位!");
            control.Focus();
        }
    }

    3、判断DataGridView中的CheckBox列是否被选中

      for (int i = 0; i < dataGridView1.Rows.Count; i++)
      {
          if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue==true)
            {
                //TODO
            }
      }

    5、DataGridView嵌入ComboBox以及DataGridViewComboBoxColumn数据绑定

    (1)DataGridView加入ComboBox下拉框的实现
    https://blog.csdn.net/hws1058648831a/article/details/8970188

    (2)嵌入ComboBox以及DataGridViewComboBoxColumn数据绑定
    https://blog.csdn.net/u013992365/article/details/54292338

    6、提取选中DataGridView中某单元格的值为空时出错的问题

    不要用 dataGridView1.Rows[e.RowIndex].Cells[ ].Value.ToString();

    使用Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[ ].Value进行转换

    二、外观

    1、设置表头颜色

    必须先设置EnableHeadersVisualStyles为false

    再设置ColumnHeadersDefaultCellStyle的BackColor

       dataGridView1.EnableHeadersVisualStyles = false;
    
       dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Red;

    2、让列填充满控件

    AutoSizeColumnsMode设为Fill

    3、删除多余的空白行

    将AllowUserToAddRows属性设置为False

    /*******相与枕藉乎舟中,不知东方之既白*******/
  • 相关阅读:
    Visual Studio 2005 Starter Kits
    怎样去做才是朋友?
    C#读写日志文本文件
    [文摘20080707]马云追加投资20亿 淘宝首定10年超沃尔玛目标
    [转]在WinForm应用程序中实现自动升级
    [转]官方Flash CS3简体中文帮助文档下载,AS3.0简体中文帮助文档下载
    [引]MySQL INNODB类型表的外键关联设置
    [转]winfrom让弹出的MessageBox在指定时间内自动销毁
    生活开心一笑 之 "我家半"与"QQ病毒"
    [English20080721]疯狂英语365句
  • 原文地址:https://www.cnblogs.com/Mars-0603/p/14923003.html
Copyright © 2011-2022 走看看