【问题标题】:Get the Value of UserControl in datagridview获取datagridview中UserControl的值
【发布时间】:2021-07-08 18:27:03
【问题描述】:

我试图在 datagridview 中添加自定义用户控件

我创建了我的用户控件 ==> 只有两个标签

然后我写了这段代码来在datagridview中添加这个控件并生成数据

Private Function CreateData() As DataTable
    Dim T_Table1 As DataTable = New DataTable()
    T_Table1.Columns.AddRange(New DataColumn() {New DataColumn("UserControl1"), New DataColumn("GenderID"), New DataColumn("GenderName")})
    T_Table1.Rows.Add(New Object() {"5", "1", "Man"})
    T_Table1.Rows.Add(New Object() {"5", "2", "Female"})
    T_Table1.Rows.Add(New Object() {"5", "3", "Other"})
    Return T_Table1
End Function

Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.DataGridView1.DataSource = CreateData()
    Me.DataGridView1.Columns(0).Width = 124
    Me.DataGridView1.Columns(2).Width = 125
    For i As Integer = 0 To DataGridView1.RowCount - 1
        Dim userControl1 As UserControl1 = New UserControl1()
        userControl1.Label1.Text = DataGridView1.Rows(i).Cells("GenderName").Value
        userControl1.Label2.Text = DataGridView1.Rows(i).Cells("GenderID").Value
        Me.DataGridView1.Controls.Add(userControl1)
        userControl1.Location = Me.DataGridView1.GetCellDisplayRectangle(0, i, True).Location
        userControl1.Size = Me.DataGridView1.GetCellDisplayRectangle(0, i, True).Size

    Next
End Sub

结果看起来不错

现在当我点击第一个单元格时,DataGridView1_CellClick 事件没有被激活。

那么当我点击相应的单元格时,如何在 myusercontrol 中获取 Id/Name 的值?例如,根据 Id 值更改名称的颜色。

【问题讨论】:

    标签: .net datagridview user-controls


    【解决方案1】:

    您好决定改变概念,我将使用 flowlayout 控件 谢谢你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多