c# use datatable in other form datagridview

怎甘沉沦 提交于 2019-12-11 10:38:20

问题


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

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

}

Lets say I would click on button in Form1, then Form2 would appear with another 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);
        }

}

回答1:


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();

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
}

Usage in Form1 might look something like this:

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


来源:https://stackoverflow.com/questions/29562557/c-sharp-use-datatable-in-other-form-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!