1
我想從我的DateTimePicker,TextBoxes和ComboBox(這在form2上)添加一行到DGV(form1),現在我收到消息:'Overzicht.File.dt'由於其保護級別而無法訪問。我該如何解決這個問題?DataTable dt由於其保護級別而無法訪問
這是Form1中
using System;
using System.Data;
using System.Windows.Forms;
namespace Javell_Administratie_Software
{
public partial class Overzicht : Form
{
public Overzicht()
{
InitializeComponent();
Overzicht1.AutoGenerateColumns = false;
}
public DataTable table
{
set
{
File myFile = new File();
table = myFile.dt;
}
//above at myFile.dt its giving me the error
get
{
return table;
}
}
public class File
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
public void RekeningenOverzicht()
{
Overzicht oz = new Overzicht();
foreach (DataGridViewColumn col in oz.Overzicht1.Columns)
{
dt.Columns.Add(col.Name);
col.DataPropertyName = col.Name;
}
ds.Tables.Add(dt);
oz.Overzicht1.DataSource = dt;
}
}
public void AddDataTableRow()
{
Toevoegen tv = new Toevoegen();
Object row = new Object[]
{
tv.dateTimePicker1.Value, tv.textBox1.Text,
tv.textBox2.Text, tv.textBox3.Text, tv.textBox4.Text,
tv.textBox5.Text, tv.comboBox1.Text
};
table.Rows.Add(row);
Overzicht1.DataSource = table;
Overzicht1.Update();
tv.Close();
}
public void Toevoegen1_Click(object sender, EventArgs e)
{
Toevoegen tv = new Toevoegen();
tv.Show();
}
}
}
如果你想從你的類文件的DataTable DT是可見的,你需要將它們設置爲這樣
Public DataTable dt = new DataTable();
Public DataSet ds = new DataSet();
這是窗口2
using System;
using System.Windows.Forms;
namespace Javell_Administratie_Software
{
public partial class Toevoegen : Form
{
public Toevoegen()
{
InitializeComponent();
}
public void Toevoegen2_Click(object sender, EventArgs e)
{
Overzicht oz = new Overzicht();
oz.AddDataTableRow();
}
}
}
'File'類中的'dt'是私有的。你的屬性也會導致一個stackoverflow異常。 –
偏離主題,但似乎您需要查看[命名指南](https://msdn.microsoft.com/zh-cn/library/ms229002(v = vs.110).aspx)。 'table'不適合使用屬性名稱,同樣'File'不是合適的類名。 'Javell_Administratie_Software'也不適合名字空間。 –
你有沒有解決方案的getter? – Javell