• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Cannot see rows in unbound DataGridView

I'm trying to pass an unbound DataGridView to a form, and cannot get the rows to appear, even though the Rows.Count value is correct. I feel like I'm missing something obvious.

// 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
}
0
SaintMartins
Asked:
SaintMartins
1 Solution
 
SaintMartinsAuthor Commented:
Thanks for your help! Using that information, I was able to get it to work by using this code in the load event of my form:

foreach (DataGridViewColumn column in _datagridtest.Columns)
{
    this.dataGridView1.Columns.Add(column.Name, column.HeaderText);
}
this.dataGridView1.RowCount = _datagridtest.RowCount;
for (int col = 0; col <= _datagridtest.Columns.Count - 1; col++)
{
    for (int row = 0; row <= _datagridtest.Rows.Count - 1; row++)
    {
        this.dataGridView1[col, row].Value = _datagridtest[col, row].Value;
    }
}
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now