Winform DataGridView控件常见用法

1.显示行号

 通过RowPostPaint事件更新每行HeaderCell.Value

private void dgGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
  for (int i = 0; i < dgv.Rows.Count; i++)
  {   
    //HeaderCell设置行号,行号从1开始(下标+1)      
    dgv.Rows[i].HeaderCell.Value = (i+1).ToString();  //HeaderCell获取或设置行的标头单元格            
  }
}

2.删除选中行

   通过行号Remove

GridFiles.Rows.Remove(GridFiles.SelectedRows[0]);

3.首列合并单元格

/// <summary>
/// 列合并
/// </summary>
private void dataGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    try
    {
        // 对第1列相同单元格进行合并
        if (e.ColumnIndex == 0 && e.RowIndex != -1)
        {
            using (Brush gridBrush = new SolidBrush(this.dataGrid.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
            {
                using (Pen gridLinePen = new Pen(gridBrush))
                {
                    // 清除单元格
                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                    // 画 Grid 边线(仅画单元格的底边线和右边线)
                    //   如果下一行和当前行的数据不同,则在当前的单元格画一条底边线
                    if (e.RowIndex < dataGrid.Rows.Count - 1 &&
                    dataGrid.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString())
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                        e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                        e.CellBounds.Bottom - 1);
                    // 画右边线
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                        e.CellBounds.Top, e.CellBounds.Right - 1,
                        e.CellBounds.Bottom);

                    // 画(填写)单元格内容,相同的内容的单元格只填写第一个
                    if (e.Value != null)
                    {
                        if (e.RowIndex > 0 && dataGrid.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString())
                        { }
                        else
                        {
                            e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                Brushes.Black, e.CellBounds.X + 2,
                                e.CellBounds.Y + 5, StringFormat.GenericDefault);
                        }
                    }
                    e.Handled = true;
                }
            }
        }
    }
    catch (Exception)
    {
        throw;
    }
}

4.单元格绘制底色

  通过RowPrePaint事件

/// <summary>
/// 单元格底色绘制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGrid_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    try
    {
        if (dataGrid.Rows.Count > 0 && dataGrid.Columns.Count > 0)
        {
            for (int indexRow = 0; indexRow < this.dataGrid.Rows.Count; indexRow++)
            {
                for (int indexColum = 2; indexColum < this.dataGrid.Columns.Count; indexColum++)
                {
                    if (this.dataGrid.Rows[indexRow].Cells[indexColum].Value.ToString() == "NG" || this.dataGrid.Rows[indexRow].Cells[indexColum].Value.ToString() == "无效")
                        this.dataGrid.Rows[indexRow].Cells[indexColum].Style.BackColor = Color.Red;
                    else
                        this.dataGrid.Rows[indexRow].Cells[indexColum].Style.BackColor = Color.White;
                }
            }
        }
    }
    catch (Exception ex)
    {
    }
}

5.DataError弹窗报错

     代码操作包含首列单元格合并、单元格底色绘制,报下图错误,并且catch不到,

 使用DataGridView控件的DataError事件可以捕获异常,并处理

 private void dataGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
 //日志记录
}

6.列填充整个界面

7.单元格中添加按钮

元格中添加按钮
 this.dgdParamsSend.Controls.Clear();
 //生成按钮列
for (int i = 0; i < dgdParamsSend.Rows.Count; i++)
{
    string cellVaule = dgdParamsSend.Rows[i].Cells[2].Value.ToString();
    if (string.IsNullOrEmpty(cellVaule)) continue;
    foreach (Control item in this.dgdParamsSend.Controls)
    {
        if (item.Text == cellVaule)
            return;
    }
    Button btn = new Button();
    btn.Text = cellVaule;
    btn.Font = new Font("宋体", 10);
    btn.Visible = true;
    btn.Width = this.dgdParamsSend.GetCellDisplayRectangle(2, i, true).Width;
    btn.Height = this.dgdParamsSend.GetCellDisplayRectangle(2, i, true).Height;
    this.dgdParamsSend.Controls.Add(btn);
    btn.Location = new Point(((this.dgdParamsSend.GetCellDisplayRectangle(2, i, true).Right) -
           (btn.Width)), this.dgdParamsSend.GetCellDisplayRectangle(2, i, true).Y);//设置btn显示位置 
}

8.单元格双击处理

    通过单元格双击事件获取到当前单元格行、列下标与内容,根据内容执行业务执行

 private void dgdParamsSend_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (btnStart.Enabled == true)
            {
                MessageBox.Show("请先点击开始!");
                return;
            }
            int indexRow, indexCol;
            string type, buttonName;
            indexRow = e.RowIndex;
            indexCol = e.ColumnIndex;
            type = this.dgdParamsSend.Rows[indexRow].Cells[1].Value.ToString();
            buttonName = this.dgdParamsSend.Rows[indexRow].Cells[2].Value.ToString();
            if (indexCol != 2) return;
            if (type != "XXX" || string.IsNullOrEmpty(buttonName)) return;
            AuthenticationTest.ButtonSendData(indexRow, buttonName);
        }

9.取消点击列标题(表头)排序

for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
{
  this.dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值