c#以datagridview的其他形式使用datatable [英] c# use datatable in other form datagridview

查看:180
本文介绍了c#以datagridview的其他形式使用datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多主题,也在这里stackoverflow,但是没有解决我的问题

Hi Ive searched many topics, also here in stackoverflow, however none solved my problem

这是我的主要形式,我使用我的数据库和显示项目在datagridview

This is my main form where I operate with my database and display items in datagridview

public partial class Form1 : Form
{

     DatabaseConnection objConnect;
     string conString;
     private static DataTable table;
     DataSet dataSet;

     public Form1()
     {
          InitializeComponent();
          CreateConnection();
          MakeDataTable();
     }

     public DataTable table
     {
          get
          {
              return table;
          }
     }

     private void MakeDataTable()
     {
          table = new DataTable();

          DataColumn column;

          column = new DataColumn();
          column.DataType = Type.GetType("System.String");
          column.ColumnName = "Name of Item";
          table.Columns.Add(column);

          etc...
     }

     //this connects to my local db through DatabaseConnection.cs
     //where I got table ItemsInWorld, 
     //from where I take items and add it via InputBox to my datatable table
     //which works fine and shows all added items
     private void CreateConnection()
     {
          objConnect = new DatabaseConnection();
          conString = Properties.Settings.Default.ItemsConnectionString;

          objConnect.connection_string = conString;
          objConnect.Sql = Properties.Settings.Default.SQL;

          dataSet = objConnect.GetConnection;
     }
     //I also have here code that show content of this DataTable table in 
     //DataGridView Form1.dataGridView

}

让我说我会点击Form1中的按钮,然后Form2会出现另一个dataGridView

Lets say I would click on button in Form1, then Form2 would appear with another dataGridView

在这种形式中,正如我所说,我想要另一个dataGridView可以调用它只是dataGridV,它将显示与Form1中的dataGridView相同的项目,我该怎么办?

In this form, as I said, I would like to have another dataGridView lets call it just dataGridV that would show same items as the dataGridView in Form1, what should I do?

这是我的代码,但它只显示空表

This is my code yet, but it only shows empty table

Form2 : form
{
        DataTable table2;
        DatabaseConnection objConnect;
        string conString;
        DataSet dataSet;
        public DataGridV()
        {
            InitializeComponent();
            CreateConnection();
            CreateDataView();
        }


        private void CreateConnection()
        {
            objConnect = new DatabaseConnection();
            conString = Properties.Settings.Default.ItemsConnectionString;

            objConnect.connection_string = conString;
            objConnect.Sql = Properties.Settings.Default.SQL;

            dataSet = objConnect.GetConnection;
        }
        public void CreateDataView()
        {
            Form1 f = new Form1();
            table2 = f.TableBackpack;
            dataGridViewMix.DataSource = new DataView(tableBackpack);
        }

}


推荐答案

如果 Form1 负责打开 Form2 的实例,则 Form1 应该将此实例保留在私有字段中:

If Form1 is responsible for opening an instance of Form2, then Form1 should keep this instance in a private field:

private Form2 form2 = new Form2();

然后,在 Form2 中创建一个公共方法这将允许 Form1 设置表字段(而不是 Form2 试图将其从 Form1 ,如你的代码示例)

Then, create a public method in Form2 that will allow Form1 to set the table field (instead of Form2 trying to pull it from Form1 as in your code example).

public void SetTable(DataTable table)
{
  table2 = table;
  // Your code to fill DGV source with table2
}

Form1 可能看起来像这样:

form2.SetTable(this.table);
form2.ShowDialog();

这篇关于c#以datagridview的其他形式使用datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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