Cannot see rows in unbound DataGridView

Posted on 2012-09-19
Last Modified: 2012-09-20
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("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;

// test_PassingDataGridView form code
DataGridView _datagridtest;
public DataGridView DataGridTest
    get { return _datagridtest; }
    set { _datagridtest = value; }

public test_PassingDataGridView()

private void test_PassingDataGridView_Load(object sender, EventArgs e)
    this.dataGridView1 = _datagridtest; // _datagridtest.Rows.Count is correct at this point
Question by:SaintMartins
    LVL 28

    Accepted Solution


    Author Closing Comment

    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;

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now