【问题标题】:Pulling datagridview information from one form and using it in another从一个表单中提取 datagridview 信息并在另一个表单中使用它
【发布时间】:2026-01-30 21:40:01
【问题描述】:

例如,假设我在一个表单上有一个主屏幕,它有一个带有 PC 组件列表的 datagridview,以及用于从该 dgv 添加、删除或修改组件的按钮。我希望用户能够从 datagridview 中选择一行,然后单击修改按钮,该按钮将打开一个 ModifyComponent 表单,所选组件的详细信息将填充到各种文本框中。因此,我希望将所选行中的数据提取到 ModifyComponent 表单中。到目前为止,这就是我尝试过的方式。我在 MainScreen 表单中创建了这个方法(行索引与​​组件绑定列表中的 ComponentID 属性相同):

 public int getData()
    {
       int component = ComponentsGridView.SelectedRows[0].Index);
       return component;
    } 

然后在 ModifyComponent 表单中,我使用 Inventory 类中的 lookupComponent 方法,该方法获取组件 ID 并从组件列表中提取正确的组件:

 Inventory.lookupComponent(MainScreen.getData());

然后我将编写代码将每个属性放入正确的文本框中,但我收到错误消息“非静态字段、方法或属性 MainScreen.getData 需要对象引用”。解决这个问题的好方向或解决这个问题的不同方法是什么?

【问题讨论】:

  • 在两个表单之间共享一个数据源很容易。您需要停止认为数据“在”控件或行中 - 这就是您在 GRID 中向用户显示数据视图的方式
  • @WelcomeOverflow 我知道 dgv 只是显示列表中的数据。我实际上是在尝试传输有关当该人单击修改按钮时选择了哪一行的信息。尝试在修改表单中使用“ComponentsGridView.SelectedRows[0].Index”代码时也出现错误
  • 如果所选行的索引是一个不好的方法。如果你有 List<T> 提取项目并传递它。

标签: c# winforms datagridview


【解决方案1】:

这是一种可能的方法(C# 翻译)。

几件事:

  • 如果您不需要访问整个数据表,您可以只在表单之间共享一个数据行。不过,这只是一个参考,反正这样也不会花费你太多资源。
  • 如果您使用 SelectedIndex(0),您应该有 DatagridView.Multiselect = False。
  • 这样您还可以在父表单中使用函数,即在修改记录后调用 ParentF.ReloadData(),如果您有这样的函数来重新加载数据集为 Public

C#

// Declare In Parent form (outside functions, in the head of class)
public DataTable dt = new DataTable();  // create *public* datatable

// Get data any way you like into datatable
string cmdTextTC = "SELECT TOP 1000 ID, Name, Componet FROM TestTable; ";   
DataSet ds;
ds = DataAccessLayer.GetQueryResultsTC(cmdText);
If ds.Tables.Count > 0 Then
    dt = ds.Tables(0)
End If

// Use it for datagrid
this.DataGridViewComponents.datasource = dt;

// Declare In Child form (outside functions, in the head of class)
public MyParentForm ParentFrm;   // link to parent
public Int32 ComponentID;        // selected component

// When declaring child form in the parent, remember to set some relations
MyChildForm ChildForm; // child form
ChildForm.ParentFrm = this;     // create reference to parent
ChildForm.ComponentID = ComponentsGridView.SelectedRows(O).Index;  // Set Selected ID

// Use data in ChildForm
DataRow dr = ParentFrm.dt.rows(ComponentID);
this.NumericUpDownID.Value = System.Convert.ToInt32(dr("ID"));
this.TextBoxName.Text = System.Convert.ToString(dr("Name"));

VB 代码

' Declare In Parent form (outside functions, in the head of class)
Public dt As New DataTable  ' create *public* datatable 

    ' Fill it any way you like (here I use a declared function)
    Dim cmdTextTC As String = "SELECT TOP 1000 ID, Name, Componet FROM TestTable; "
    Dim ds As DataSet
    ds = DataAccessLayer.GetQueryResultsTC(cmdText)
    If ds.Tables.Count > 0 Then
        dt = ds.Tables(0)
    End If

    ' Use it for datagrid
    Me.DataGridViewComponents.datasource = dt

' Declare In Child form (outside functions, in the head of class)
Public ParentFrm As MyParentForm   ' link to parent
Public ComponentID As Int32        ' selected component

' When declaring child form in the parent, remember to set some relations
Dim ChildForm As MyChildForm ' child form
ChildForm.ParentFrm = Me     ' create reference to parent
ChildForm.ComponentID = ComponentsGridView.SelectedRows(O).Index  ' Set Selected ID

' Use data in ChildForm
Dim dr As DataRow = ParentFrm.dt.rows(ComponentID)
Me.NumericUpDownID.Value = CInt(dr("ID"))
Me.TextBoxName.Text = CStr(dr("Name"))

【讨论】:

    最近更新 更多