【问题标题】:How to tell if datagridview column is an image or checkbox column?如何判断 datagridview 列是图像列还是复选框列?
【发布时间】:2013-01-19 20:28:29
【问题描述】:

我正在尝试创建一个自定义函数,将 datagridview 的内容保存到文件中。我这样做而不是序列化,因为我的自定义函数旨在尽可能简单快速地实现以保存和加载 winform 的状态,并且每次我想序列化对象时创建自定义类并不容易和快速尽可能。

为此,我需要以编程方式检测 datagridview 中的哪些列是图像列或复选框列,以便我知道如何在加载时正确处理它们。我在网上看了看,似乎还没有其他人提到它(据我的谷歌搜索建议),但我绝对确定有某种方法可以判断某个单元格的类型/列/列是。有人知道吗?

【问题讨论】:

    标签: c# types datagridview detection


    【解决方案1】:

    你可以这样做 -

            foreach (var col in dataGridView1.Columns)
            {
                if (col is DataGridViewImageColumn)
                {
                    // Image Column
                }
    
                if (col is DataGridViewCheckBoxColumn)
                {
                    // CheckBox Column
                }
            }
    

    【讨论】:

      【解决方案2】:
      foreach (DataGridViewRow dr in dataGridView1.Rows) {
        foreach (DataGridViewCell dc in dr.Cells) {
          if (dc.GetType() == typeof(DataGridViewImageCell)) {
      
          }
        }
      }
      

      这应该可以工作...包括细胞!!!

      【讨论】:

        【解决方案3】:

        这是我正在使用的:

        Dim colType As Type = DataGridView1.Columns(0).GetType
        If colType.Name = "DataGridViewImageColumn" Then
        'code for image column type
        End If
        

        学分: Link to VB forums

        【讨论】:

          【解决方案4】:

          这应该可行。

           For each col in datagridview.columns   
             If col.CellType.Name = "DataGridViewImageColumn" Then
             'do something
             End If
           Next
          

          【讨论】:

            【解决方案5】:

            只比较类型:

                    DataGridView dgv = new DataGridView();
            
                    foreach (DataGridViewColumn col in dgv.Columns)
                    {
                        if (col.GetType() == typeof(DataGridViewCheckBoxColumn))
                        {
                            // do something
                        }
                        else if (col.GetType() == typeof(DataGridViewImageColumn))
                        {
                            // do something else
                        }
                    }
            

            【讨论】:

            • 你能验证吗?这不会在 C# 中编译
            【解决方案6】:

            你可能还想看看

            column.ValueType
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-08-30
              • 2021-11-27
              • 1970-01-01
              • 2020-01-23
              • 1970-01-01
              相关资源
              最近更新 更多