DataGridView添加右键菜单等技巧

时间:2022-03-19 08:08:00


1). 添加一个快捷菜单contextMenuStrip1;
2). 给dataGridView1的CellMouseDown事件添加处理程序:

 程序代码

private void DataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        if (e.RowIndex >= 0)
        {
            //若行已是选中状态就不再进行设置
            if (dataGridView1.Rows[e.RowIndex].Selected == false)
            {
                dataGridView1.ClearSelection();
                dataGridView1.Rows[e.RowIndex].Selected = true;
            }
            //只选中一行时设置活动单元格
            if (dataGridView1.SelectedRows.Count == 1)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            }
            //弹出操作菜单
            contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
        }
    }
}


效果图:

DataGridView添加右键菜单等技巧



2).复制选中单元格的内容到剪贴板

 程序代码

Clipboard.SetDataObject(dataGridView1.GetClipboardContent());


3).只显示自定义列

 程序代码

dataGridView1.AutoGenerateColumns = false;//必须在代码中设置


4).显示图片

通常,我们将图片路径保存在数据库中,但在dataGridView1中要显示图片,,可以进行如下操作:

①.添加一个DataGridViewTextBoxColumn类型的列,Name=Path,DataPropertyName=Pic,Visible=False;
②.添加一个DataGridViewImageColumn类型的列,Name=Pic;
③.dataGridView1控件DataBindingComplete事件处理程序如下:

 程序代码

private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    Image image1 = null;
    Image image2 = null;
    string path = string.Empty;

    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        path = @"F:\" + dataGridView1.Rows[i].Cells["Path"].Value;

        if (File.Exists(path))
        {
            image1 = Image.FromFile(path);
            image2 = new Bitmap(image1, 120, 120);//重设大小

            dataGridView1.Rows[i].Cells["Pic"].Value = image2;
            //((DataGridViewImageCell)dataGridView1.Rows[i].Cells["Pic"]).Value = image2;
        }
    }
}


5).当网格未填充满控件时,画线来填充空白区域

 程序代码

/// <summary>
/// 绘制网格填充空白区域
/// </summary>
/// <param></param>
/// <param></param>
public void CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    DataGridView myDataGridView = (DataGridView)sender;

    if (myDataGridView.Rows.Count > 0)
    {
        int i = myDataGridView.ColumnHeadersHeight;//标题行高
        int j = myDataGridView.Rows.GetRowsHeight(DataGridViewElementStates.Visible); //所有可见行总高
        int k = myDataGridView.Height; //控件高度
        int l = myDataGridView.Rows.GetLastRow(DataGridViewElementStates.Visible);//最后一行索引
        int count = myDataGridView.Columns.Count;//列总数
        int width = 0;

        //当网格未充满控件时才画线
        if (i + j < k)
        {
            using (Brush gridBrush = new SolidBrush(myDataGridView.GridColor))
            {
                using (Pen gridLinePen = new Pen(gridBrush))
                {
                    //处理标题列
                    if (myDataGridView.RowHeadersVisible)
                    {
                        width = myDataGridView.RowHeadersWidth;
                        e.Graphics.DrawLine(gridLinePen, width, i + j, width, k);
                    }
                    else
                    {
                        width = 1;
                    }

                    //处理正常列
                    for (int index = 0; index < count; index++)
                    {
                        if (myDataGridView.Columns[index].Visible)
                        {
                            width += myDataGridView.Columns[index].Width;

                            e.Graphics.DrawLine(gridLinePen, width, i + j, width, k);
                        }
                    }
                }
            }
        }
    }
}