C# 学习之 DataGridView 的使用

1.去掉最前面的空行
这里写图片描述

不显示包含标题的列 RowHeadersVisible 设为 false ;

2.在单元格内输入信息

当满足下面的所有条件时,单元格将进入编辑模式:
能对基础数据源进行编辑。
DataGridView 控件已启用。
EditMode 属性值不为 EditProgrammatically。
单元格、行、列和控件的 ReadOnly 属性都设置为 false。

  1. 单元格文字居中显示

 指定 DataGridView 单元格的文本对齐方式
  将 DataGridViewCellStyle 的 Alignment 属性设置为 DataGridViewContentAlignment 枚举值之一。 下面的代码示例使用列的 DefaultCellStyle 属性设置特定列的对齐方式。
  Me.dataGridView1.Columns(“CustomerName”).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

  1. 改变单元格背景颜色

    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;


5.设置行高
dataGridView1.RowTemplate.Height = 100; //设置单元格高度
dataGridView1.ColumnHeadersHeight = 30; //设置标题列高度
dataGridView1.Rows[10].Height = 47; //设置某一行高度 6.新增新行的两种方法 (一,方便添加数据,二、方便添加控件)

方法一:
int index=this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = “1”;
this.dataGridView1.Rows[index].Cells[1].Value = “2”;
this.dataGridView1.Rows[index].Cells[2].Value = “监听”;

方法二:
DataGridViewRow row = new DataGridViewRow();
DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
textboxcell.Value = “aaa”;
row.Cells.Add(textboxcell);
DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();
row.Cells.Add(comboxcell);
dataGridView1.Rows.Add(row);

  1. 列排序

SortMode是DataColumn的属性值,通过DataGridView获取DataColumn,然后将DataColumn的SortMode属性设置也可以达到同样的目的。
DataGridView中的Columns属性里面可以设置。进入“Edit Columns”窗口后,在相应的列属性设置里面把SortMode属性选择为”NotSortable”就可以了。
NotSortable:不进行排序
Automatic: 自动排序
Programmatic:程序控制

8.DataGridView 控件添加单击事件

   private void WriteCoeff_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    //获取当前点击的行
    if (WriteCoeff.CurrentCell is DataGridViewButtonCell && e.RowIndex > -1)
    {
        //获取当前被点击的单元格
        DataGridViewButtonCell btnCell = WriteCoeff.CurrentCell as DataGridViewButtonCell;
        if (btnCell.Tag == null)
        {
            MessageBox.Show(e.ColumnIndex.ToString());
        }
    }
}

9、为单元格编辑添加按键控制

private void WriteCoeff_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    //检测是被表示的控件还是DataGridViewTextBoxEditingControl
    if (e.Control is DataGridViewTextBoxEditingControl)
    {
        DataGridView dgv = (DataGridView)sender;

        //取得被表示的控件
        DataGridViewTextBoxEditingControl tb = (DataGridViewTextBoxEditingControl)e.Control;

        //事件处理器删除
        tb.KeyPress -= new KeyPressEventHandler(KeyPress_IsDigit);
        //检测对应列
        //if (dgv.CurrentCell.OwningColumn.Name == "单价")
        if (dgv.CurrentCell.ColumnIndex > 1)
        {
            // KeyPress事件处理器追加
            tb.KeyPress += new KeyPressEventHandler(KeyPress_IsDigit);
        }
    }
}

10、禁止DataGridView 回车 换行


//重写按键处理程序,禁止DataGridView 的enter 输入
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Enter && WriteCoeff.Focused)
    {
        return true;
    }
    else
    {
        return false;
    }
}

10、 更改窗口尺寸后,被隐藏的空间,在窗口尺寸恢复后,还原

11、更改单元格选中时的背景和前景颜色

WriteCoeff.Rows[index].DefaultCellStyle.SelectionBackColor = Color.Green;
WriteCoeff.Rows[index].DefaultCellStyle.SelectionForeColor = Color.Black;
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值