如何使用Datagridview在我的项目中停止代码冗余 [英] How Do I Stop Code Redundancy In My Project With Datagridview
本文介绍了如何使用Datagridview在我的项目中停止代码冗余的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我在1表单上设计了一个包含37个DataGridViews的日历,具体取决于所选月份,datagridviews隐藏并显示指定日期的详细信息。
目前我正在为每个DataGridView双击事件触发相同的模块,我的问题是,而不是每个DataGridView双击事件有1行37次,我有没有办法检测哪个Datagridview被双击并激活我的详细信息代码?
谢谢
Antony
解决方案
我不会质疑您对解决方案的要求。之前的评论涵盖了您当前条件的优缺点,并充分要求。但我发现自己处于这样的情况:对现有代码的修改受到策略,管理决策的限制或仅仅因为......
可以将所有DataGridViews添加到集合中(List(of datagridview)列表没有必要,但如果你需要它可以提供帮助)。该列表可用于为MouseDoubleClick事件分配事件处理程序。最后,事件处理程序可以将Sender对象强制转换为双击的datagridview。下面是一个小代码示例,创建一个新的Form,添加6个Datagridview对象。将此代码放在类文件中。希望这会有所帮助。
公共类Form1
结构myString
私有_stringValue As String
公共属性StringValue ()As String
获取
返回_stringValue
结束获取
设置(ByVal值为字符串)
_stringValue = value
结束集
结束属性
Public Sub New(ByVal newValue As String)
_stringValue = newValue
End Sub
End Structure
Dim dgList As New List(Of DataGridView)
Dim list As New List(Of myString)
Public Sub New()
InitializeComponent()
makelists()
End Sub
Private Sub MakeLists()
'设置ssome数据使用
For i As Integer = 0到20
list.Add(New myString(String_data _& i.ToString))
Next
'创建一些datag ridviews
dgList.Add(DataGridView1)
dgList.Add(DataGridView2)
dgList.Add(DataGridView3)
dgList.Add(DataGridView4)
dgList.Add(DataGridView5) )
dgList.Add(DataGridView6)
'为datagridviews添加处理程序
For each dg As DataGridView in dgList
dg.DataSource = list
AddHandler dg .MouseDoubleClick,AddressOf dgs_MouseDoubleClick
Next
End Sub
'handle atagirdview doubleclick
Private Sub dgs_MouseDoubleClick(sender As Object,e As MouseEventArgs)
Dim dg As DataGridView = DirectCast (sender,DataGridView)
如果dg.CurrentRow IsNot Nothing AndAlso dg.CurrentRow.Index> -1然后
MessageBox.Show(dg.Name&& list(dg.CurrentRow.Index).StringValue)
End if
End Sub
End Class
Hi There,
I designed a calendar with 37 DataGridViews on 1 form, depending on the month selected, the datagridviews hide and show details for the specified days.
At the moment i'm firing the same module for each DataGridView double click event, my question is, instead of having 1 line 37 times for each DataGridView double click event, is there no way i detect which Datagridview is double clicked and fire my details code?
Thank you
Antony
解决方案
I won't question you requirement for a solution. The previous comments cover the pros/cons of you current conditions and request adequately. But I have found myself in situations where modifications to existing code are limited by policy, management decisions or just because...
One can add all the DataGridViews to a collection (List(of datagridview)the list is not necessary but it is there to help if you need to). The list can be used to assign an event handler for the MouseDoubleClick event. Finally the event handler can then cast the Sender object as the datagridview that was double clicked. Below is a small code sample, create a new Form, add 6 Datagridview object to it. Place this code in the Class file. Hope this helps.
Public Class Form1 Structure myString Private _stringValue As String Public Property StringValue() As String Get Return _stringValue End Get Set(ByVal value As String) _stringValue = value End Set End Property Public Sub New(ByVal newValue As String) _stringValue = newValue End Sub End Structure Dim dgList As New List(Of DataGridView) Dim list As New List(Of myString) Public Sub New() InitializeComponent() makelists() End Sub Private Sub MakeLists() 'Setup ssome data to use For i As Integer = 0 To 20 list.Add(New myString("String_data _ " & i.ToString)) Next 'Create a number of datagridviews dgList.Add(DataGridView1) dgList.Add(DataGridView2) dgList.Add(DataGridView3) dgList.Add(DataGridView4) dgList.Add(DataGridView5) dgList.Add(DataGridView6) 'Add handlers to datagridviews For Each dg As DataGridView In dgList dg.DataSource = list AddHandler dg.MouseDoubleClick, AddressOf dgs_MouseDoubleClick Next End Sub 'Handle atagirdview doubleclick Private Sub dgs_MouseDoubleClick(sender As Object, e As MouseEventArgs) Dim dg As DataGridView = DirectCast(sender, DataGridView) If dg.CurrentRow IsNot Nothing AndAlso dg.CurrentRow.Index > -1 Then MessageBox.Show(dg.Name & " " & list(dg.CurrentRow.Index).StringValue) End If End Sub End Class
这篇关于如何使用Datagridview在我的项目中停止代码冗余的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文