2
我試圖將未綁定的DataGridView傳遞給窗體,並且無法讓行顯示,即使Rows.Count值是正確的。我覺得我失去了一些明顯的東西。無法看到行中未綁定DataGridView
// method code
// Create an DataGridView
DataGridView dgv = new DataGridView();
dgv.Columns.Add("Name","Name");
dgv.Columns.Add("Comments", "Comments");
// Add rows to datagridview
int n = dgv.Rows.Add();
dgv.Rows[n].Cells["Name"].Value = "a test name";
dgv.Rows[n].Cells["Comments"].Value = "some comment";
// Pass DataGridView to form
test_PassingDataGridView f = new test_PassingDataGridView();
f.DataGridTest = dgv;
f.ShowDialog();
// test_PassingDataGridView form code
DataGridView _datagridtest;
public DataGridView DataGridTest
{
get { return _datagridtest; }
set { _datagridtest = value; }
}
public test_PassingDataGridView()
{
InitializeComponent();
}
private void test_PassingDataGridView_Load(object sender, EventArgs e)
{
this.dataGridView1 = _datagridtest; // _datagridtest.Rows.Count is correct at this point
}
Samuel Neff的回答幾乎肯定是正確的 - 但它看起來似乎已經在表單上有一個datagridview - 您的this.dataGridView1。您可以簡單地將您當前在dvg上工作的所有代碼應用到datagridview1 - 在InitializeComponent()之後,這是新網格由設計器生成的代碼實例化的地方 –