设置标签属性datagridview C#

时间:2017-12-01 18:11:38

标签: c# winforms

我正在WinForms中创建一个datagridview。 datagridview中的每个单元格都是textboxcell或datagridview图像单元格。我正在触发一个cellMouseDownEent(对象发送者,DataGridViewCellMouseEventArgs e)。如果所选择的单元格是图像单元格,则执行task1,如果是textboxcell,则执行task2。我不知道如何确定当前单元格是图像单元格还是文本框单元格。我尝试将图像单元格的tag属性设置为0,将textboxcell单元格设置为1以确定哪些被点击,但没有运气。任何建议都被认可。

谢谢,

I'm adding my code here:
Ignore if a column or row header is clicked
                if (e.RowIndex != -1 && e.ColumnIndex != -1)
                {
                    if (e.Button == MouseButtons.Right)
                    {
                        DataGridViewCell clickedCell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex];

                        // Here you can do whatever you want with the cell
                        this.dgvAddFilters.CurrentCell = clickedCell;  // Select the clicked cell, for instance


                        // Get mouse position relative to the vehicles grid
                        var relativeMousePosition = dgvAddFilters.PointToClient(Cursor.Position);
                        if (clickedCell.Tag.ToString()==null)
                        {
                            return;
                        }
                        else if (imageCell == null) return;
                        else if (e.ColumnIndex == 0 && e.RowIndex == 0)
                        {
                            if ((dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value == null))
                            // (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value == null))
                            {
                                dgvAddFilters.ContextMenuStrip = contMenuOr;
                                this.contMenuOr.Show(dgvAddFilters, relativeMousePosition);
                            }
                            else return;
                        }
                        else if ((e.ColumnIndex == 0)
                           && (e.RowIndex > 0)
                           && (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value == null)
                           && (dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value == null)
                           && (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
                        {
                            dgvAddFilters.ContextMenuStrip = contMenuFilterMenu;
                            this.contMenuFilterMenu.Show(dgvAddFilters, relativeMousePosition);
                        }
                        else if ((e.ColumnIndex == 0)
                            && (e.RowIndex > 0)
                            && (dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value == null)
                            && (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value != null)
                            && (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
                        {
                            dgvAddFilters.ContextMenuStrip = contMenuOrEditDelete;
                            this.contMenuOrEditDelete.Show(dgvAddFilters, relativeMousePosition);
                        }
                        else if ((e.ColumnIndex == 0)
                           && (e.RowIndex > 0)
                           && (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value == null)
                           && (dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value != null)
                           && (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
                        {
                            dgvAddFilters.ContextMenuStrip = contMenuAndDeleteEditMenu;
                            this.contMenuAndDeleteEditMenu.Show(dgvAddFilters, relativeMousePosition);
                        }
                        else if ((dgvAddFilters[e.ColumnIndex, (e.RowIndex + 2)] != null)
                            && (dgvAddFilters[(e.ColumnIndex + 2), e.RowIndex].Value != null)
                             && (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
                        {
                            dgvAddFilters.ContextMenuStrip = contmenuDeletEdit;
                            this.contmenuDeletEdit.Show(dgvAddFilters, relativeMousePosition);
                        }
                        else if ((dgvAddFilters[e.ColumnIndex, (e.RowIndex + 2)] != null)
                            && (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
                        {
                            dgvAddFilters.ContextMenuStrip = contMenuAndDeleteEditMenu;
                            this.contMenuAndDeleteEditMenu.Show(dgvAddFilters, relativeMousePosition);
                        }
                        else
                        {
                            return;
                    }

2 个答案:

答案 0 :(得分:1)

要知道点击的单元格类型,您可以尝试以下方式...查看它是否有用。

获取单击的单元格并检查其类型。

下面是检查复选框类型单元格的示例。

 private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
 {
      Type type = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType();

      if (type.Name == "DataGridViewCheckBoxCell")
      {
        string value = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
      }
 }

答案 1 :(得分:0)

这对你有帮助吗?

using System.ComponentModel;
using System.Windows.Forms;

namespace DGVCellTypes_47599159
{
    public partial class Form1 : Form
    {

        DataGridView dgv = new DataGridView();
        BindingList<dgventry> dgventries = new BindingList<dgventry>();
        public Form1()
        {
            InitializeComponent();
            InitOurStuff();
        }

        private void InitOurStuff()
        {
            this.Controls.Add(dgv);
            dgv.Dock = DockStyle.Top;
            dgv.DataSource = dgventries;
            dgv.CellMouseDown += Dgv_CellMouseDown;
            for (int i = 0; i < 10; i++)
            {
                dgventries.Add(new dgventry { col1 = $"col1_{i}", col2 = $"col2_{i}", col3 = (i % 2) > 0 });
            }
        }

        private void Dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {

            if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewCheckBoxCell)
            {
                //do something
            }
            else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewTextBoxCell)
            {
                //do something
            }
            else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewImageCell)
            {
                //do something
            }
            else
            {
                //do something
            }
        }
    }


    public class dgventry
    {
        public string col1 { get; set; }
        public string col2 { get; set; }
        public bool col3 { get; set; }

    }
}
  

我没有得到如何确定当前单元格是图像单元格还是文本框单元格

        private void Dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {

            if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewCheckBoxCell)
            {
                //do something
            }
            else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewTextBoxCell)
            {
                //do something
            }
            else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewImageCell)
            {
                //do something
            }
            else
            {
                //do something
            }
        }