如何使用自定义类数据源在datagridview单元格中显示结构属性 [英] How to show structure property in a datagridview cell with custom class datasource

查看:46
本文介绍了如何使用自定义类数据源在datagridview单元格中显示结构属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我这个代码里面有

- 一个列表(类)(它里面有一个结构的类)

- a datagridview它绑定到类



当我运行代码时,我可以看到第一列和第二列单元格正确填充。

但是在第三列单元格填充'Form1.schoolSubjects',但我想看看属性'平均'。

当我用鼠标移动到第三列的单元格时,我想要看到被保留的'toString'值。



我看一看互联网,我发现帮助可以来自

System.ComponentModel.DisplayName

或者来自

System.ComponentModel.ICustomTypeDescriptor



但我没有'理解如何。



PS我知道有一种方法可以让它在运行时更改单元格值(即修改cellPaint事件的输出),但我想直接绑定类或结构代码的正确输出。



以下是代码

 公共  Form1 
Dim dgv As DataGridView
Dim classe 作为列表( 学生)
私有 Sub Form1_Load(发件人 As 对象,e As EventArgs)句柄 MyBase .Load
Me .Size = 大小( 400 200
dgv = DataGridView
dgv.Size = 大小( .Size.Width - 10 .Size.Height - 20
dgv.Location = 点( 0 0

.Controls.Add(dgv)
classe = 列表( 学生)来自{学生( John Brown schoolSubjects( 4 10 8 5 )),学生( James Morrison schoolSubjects( 3 4 10 2 ))}
dgv.DataSource = classe
End Sub
结束 Class

公共 班级学生
Sub ByVal StudentFirstName 作为 字符串 ByVal StudentSecondName As 字符串 ByVal StudentSubjects As schoolSubjects)
firstName = StudentFirstName
secondName = StudentSecondName
Subjects = New schoolSubjects(StudentSubjects.Mathematics,StudentSubjects.English,StudentSubjects。 ModernLanguage,StudentSubjects.Science)
结束 Sub
公开 属性 firstName 作为 字符串
公共 属性 secondName 正如 字符串
公共 属性主题作为 schoolSubjects
结束

公共 结构 schoolSubjects
Sub ByVal MathematicsVote As 整数 ByVal EnglishVote As 整数 ByVal LanguageVote 作为 整数 ByVal ScienceVote 作为 整数
数学=数学投票
英语=英语投票
ModernLanguage =语言投票
科学=科学投资
平均=(数学+英语+现代语言+科学)/ 4
tostring = 字符串 .Concat( 数学:,Mathematics.ToString,vbCrLf,_
English: ,English.ToString,vbCrLf,_
ModernLanguage:, ModernLanguage.ToString,vbCrLf,_
科学:,Science.ToString)
结束 Sub
公共 属性数学作为 整数
公共 属性英语 As 整数
公共 属性 ModernLanguage 作为 整数
公共 属性科学作为 整数
公共 属性平均作为 整数
公共 阴影 Prope rty tostring 作为 字符串
结束 结构





任何解决方案?



我尝试了什么:



我试过了

 <   System.ComponentModel.DisplayName(Average) >  
公共财产主题作为schoolSubjects



但我必须明白,如果这是正确的方法,如何设置它。

解决方案

我找到了解决方案。



< System.ComponentModel.Browsable( False )> 
公开 属性主题作为 schoolSubjects
公共 ReadOnly 物业平均值作为 整数
获取
返回 Subjects.Average
结束 获取
结束 物业





不确定它是最好的,因为我已经:

- 添加调用已存在的'主题的价值的'平均'属性.Average'属性

- '隐藏''主题'属性到DataGridView。



希望有人可以得到一个bett解决方案(只需绑定'Subjects.Average'属性的值)。


好的,我找到了我正在寻找的解决方案

 公共 覆盖 功能 tostring()作为 字符串 
返回 Average.ToString
结束 功能





对于toolTip,只需在dataGridViewCellMouseEnter中引发一个事件。

 私有  Sub  dgv_CellEnter(发件人作为 对象,e  As  DataGridViewCellEventArgs)
如果 e.RowIndex = -1 < span class =code-keyword>然后 退出 Sub
选择 案例 dgv.Columns(e.ColumnIndex).Name
案例 主题

dgv.Rows( e.RowIndex)。细胞( 2 )。ToolTipText = classe(e.RowIndex).Subjects.Details
结束 选择
结束 Sub





您可以存放物业(即细节)并称之为提升此事件的价值。

这是完整的结构。

 公开 结构 schoolSubjects 
Sub ByVal MathematicsVote 作为 整数 ByVal EnglishVote 作为 整数 ByVal LanguageVote 作为 整数 ByVal ScienceVote 作为 整数
数学=数学投票
英语=英语投票
ModernLanguage = LanguageVote
科学=科学投资
平均= Math.Round((数学+英语+ ModernLanguage + Science)/ 4 1
Details = String .Concat( 数学: ,Mathematics.ToString,vbCrLf,_
英语:,英语.ToString,vbCrLf,_
ModernLanguage:,ModernLanguage.ToString,vbCrLf,_
科学:,Science.ToString,vbCrLf,vbCrLf,_
平均值:,Average.ToString)
结束 Sub
公共 属性数学 As 整数
公共 属性英语作为 整数
公共 属性 ModernLanguage 作为 整数
公共 属性科学作为 整数
公共 属性平均 As Double
公开 PROPERT y 详细信息作为 字符串
公开 覆盖 功能 tostring() As 字符串
返回 Average.ToString
结束 功能

结束 结构


Hi I've this code where there is
- a list (of class) (the class it's with a structure inside)
- a datagridview that it's binded to the class

When i run the code i can see the first and second columns cell correctly filled.
But in the third column the cell are filled with 'Form1.schoolSubjects', but i want to see the property 'Average'.
And when i go with the mouse over the cells of the third column, i want to see the custmized 'toString' value.

I've take a look over internet and i've found that an help can come from
System.ComponentModel.DisplayName
or from
System.ComponentModel.ICustomTypeDescriptor

But i haven't understand how.

P.S. I know there is a way to get it changing the cells value at run time (i.e. modifing the output of the cellPaint Event), but i want to bind directly the right output from the class or structure codes.

Here is the code

Public Class Form1
    Dim dgv As DataGridView
    Dim classe As List(Of students)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Size = New Size(400, 200)
        dgv = New DataGridView
        dgv.Size = New Size(Me.Size.Width - 10, Me.Size.Height - 20)
        dgv.Location = New Point(0, 0)

        Me.Controls.Add(dgv)
        classe = New List(Of students) From {New students("John", "Brown", New schoolSubjects(4, 10, 8, 5)), New students("James", "Morrison", New schoolSubjects(3, 4, 10, 2))}
        dgv.DataSource = classe
    End Sub
End Class

Public Class students
    Sub New(ByVal StudentFirstName As String, ByVal StudentSecondName As String, ByVal StudentSubjects As schoolSubjects)
        firstName = StudentFirstName
        secondName = StudentSecondName
        Subjects = New schoolSubjects(StudentSubjects.Mathematics, StudentSubjects.English, StudentSubjects.ModernLanguage, StudentSubjects.Science)
    End Sub
    Public Property firstName As String
    Public Property secondName As String
    Public Property Subjects As schoolSubjects
End Class

Public Structure schoolSubjects
    Sub New(ByVal MathematicsVote As Integer, ByVal EnglishVote As Integer, ByVal LanguageVote As Integer, ByVal ScienceVote As Integer)
        Mathematics = MathematicsVote
        English = EnglishVote
        ModernLanguage = LanguageVote
        Science = ScienceVote
        Average = (Mathematics + English + ModernLanguage + Science) / 4
        tostring = String.Concat("Mathematics: ", Mathematics.ToString, vbCrLf, _
                                 "English: ", English.ToString, vbCrLf, _
                                 "ModernLanguage: ", ModernLanguage.ToString, vbCrLf, _
                                 "Science: ", Science.ToString)
    End Sub
    Public Property Mathematics As Integer
    Public Property English As Integer
    Public Property ModernLanguage As Integer
    Public Property Science As Integer
    Public Property Average As Integer
    Public Shadows Property tostring As String
End Structure



Any solution?

What I have tried:

I've tried

<System.ComponentModel.DisplayName("Average")>
    Public Property Subjects As schoolSubjects


but i must understand if is the correct way and how to set it.

解决方案

I've found a solution.

<System.ComponentModel.Browsable(False)>
    Public Property Subjects As schoolSubjects
    Public ReadOnly Property Average As Integer
        Get
            Return Subjects.Average
        End Get
    End Property



Not sure it's the best, because i've:
- add the property 'Average' that call to the value of the already existent 'Subjects.Average' property
- 'hide' the 'Subjects' property to the DataGridView.

Hope someone can get a better solution (just bind the value of the 'Subjects.Average' property).


Ok, i've found the solution i was looking for

Public Overrides Function tostring() As String
        Return Average.ToString
    End Function



And for the toolTip, just raise an event in the dataGridViewCellMouseEnter.

Private Sub dgv_CellEnter(sender As Object, e As DataGridViewCellEventArgs)
        If e.RowIndex = -1 Then Exit Sub
        Select Case dgv.Columns(e.ColumnIndex).Name
            Case "Subjects"

                dgv.Rows(e.RowIndex).Cells(2).ToolTipText = classe(e.RowIndex).Subjects.Details
        End Select
    End Sub



You can store a property (i.e. Details) and call the value raising this event.
This is the full structure.

Public Structure schoolSubjects
    Sub New(ByVal MathematicsVote As Integer, ByVal EnglishVote As Integer, ByVal LanguageVote As Integer, ByVal ScienceVote As Integer)
        Mathematics = MathematicsVote
        English = EnglishVote
        ModernLanguage = LanguageVote
        Science = ScienceVote
        Average = Math.Round((Mathematics + English + ModernLanguage + Science) / 4, 1)
        Details = String.Concat("Mathematics: ", Mathematics.ToString, vbCrLf, _
                                "English: ", English.ToString, vbCrLf, _
                                 "ModernLanguage: ", ModernLanguage.ToString, vbCrLf, _
                                 "Science: ", Science.ToString, vbCrLf, vbCrLf, _
                                 "Average: ", Average.ToString)
    End Sub
    Public Property Mathematics As Integer
    Public Property English As Integer
    Public Property ModernLanguage As Integer
    Public Property Science As Integer
    Public Property Average As Double
    Public Property Details As String
    Public Overrides Function tostring() As String
        Return Average.ToString
    End Function

End Structure


这篇关于如何使用自定义类数据源在datagridview单元格中显示结构属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆