c# 在其他形式的 datagridview 中使用数据表
c# use datatable in other form datagridview
您好,我搜索了很多主题,也在 Whosebug 中,但是 none 解决了我的问题
这是我的主窗体,我在其中操作我的数据库并在 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
在这个表单中,正如我所说,我想要另一个 dataGridView 让我们称它为 dataGridV,它将显示与 Form1 中的 dataGridView 相同的项目,我应该怎么做?
这是我的代码,但它只显示空 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
应该将这个实例保存在私有字段中:
private Form2 form2 = new Form2();
然后,在 Form2
中创建一个 public 方法,允许 Form1
设置 table 字段(而不是 Form2
尝试拉取它来自 Form1
,如您的代码示例中所示)。
public void SetTable(DataTable table)
{
table2 = table;
// Your code to fill DGV source with table2
}
Form1
中的用法可能如下所示:
form2.SetTable(this.table);
form2.ShowDialog();
您好,我搜索了很多主题,也在 Whosebug 中,但是 none 解决了我的问题
这是我的主窗体,我在其中操作我的数据库并在 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
在这个表单中,正如我所说,我想要另一个 dataGridView 让我们称它为 dataGridV,它将显示与 Form1 中的 dataGridView 相同的项目,我应该怎么做?
这是我的代码,但它只显示空 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
应该将这个实例保存在私有字段中:
private Form2 form2 = new Form2();
然后,在 Form2
中创建一个 public 方法,允许 Form1
设置 table 字段(而不是 Form2
尝试拉取它来自 Form1
,如您的代码示例中所示)。
public void SetTable(DataTable table)
{
table2 = table;
// Your code to fill DGV source with table2
}
Form1
中的用法可能如下所示:
form2.SetTable(this.table);
form2.ShowDialog();