vb.net 如何通过模块添加行datagridview数据绑定控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15854725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:09:12  来源:igfitidea点击:

How to add a row datagridview data-bound control by a module

vb.netdatagridviewmodulerowadd

提问by Haytham Mtair

In my project I had to create a module, lets say it's name is "intjobs.vb", and I want to call a sub in it which is called dorow.

在我的项目中,我必须创建一个模块,假设它的名称是“intjobs.vb”,我想在其中调用一个名为dorow.

Here is the module:

这是模块:

 Module intjobs

    Public Sub dorow(fldtype, fldid, flddlvl)


      Dim fdt As New dataTDataSet.fieldsDataTable
      Dim dr As DataRow = fdt.NewRow
      dr.Item(0) = "del"
      dr.Item(1) = "up"
      dr.Item(2) = "down"
      dr.Item(3) = fldtype
      dr.Item(4) = False
      dr.Item(5) = fldid.ToString
      dr.Item(6) = flddlvl.ToString
      fdt.Rows.Add(dr)
      'Dim log As String = "Sub name: dorow {somevalue}"
      'logger(2, log)
  End Sub
End Module

So when I put this sub in the same windows form with the datagridview, it works perfectly, but it doesn't add any row when I use this module.

因此,当我将这个 sub 与 datagridview 放在同一个窗口表单中时,它可以完美运行,但是当我使用这个模块时它不会添加任何行。

Please help.

请帮忙。

采纳答案by IWIH

You can achieve that by using the right Modifier:

您可以通过使用正确的Modifier来实现:

  1. Set the Modifierof the grid view as Public.
  2. In the Moduleuse a code similar to this:

    Public Sub Modify_Cross_Module(ByRef DataGrid As DataGridView, _
                               ByVal firstColumnVal As String, _
                               ByVal secondColumnVal As String)
    
    DataGrid.Rows.Add(firstColumnVal, secondColumnVal)
    End Sub
    
  1. 将网格视图的修改器设置为Public
  2. 模块中使用类似于此的代码:

    Public Sub Modify_Cross_Module(ByRef DataGrid As DataGridView, _
                               ByVal firstColumnVal As String, _
                               ByVal secondColumnVal As String)
    
    DataGrid.Rows.Add(firstColumnVal, secondColumnVal)
    End Sub
    

You have to make sure that the DataGridvariable as ByRef.

您必须确保DataGrid变量为ByRef

Whenyou call that sub, use the following code:

您调用该 sub 时,请使用以下代码:

Modify_Cross_Module(DataGridView1, "FirstVal", "SecondVal")

Where DataGridView1is the DataGridView you want to add row to it.

其中DataGridView1是您要向其添加行的 DataGridView。