Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Populate win form datagridview from method in another class

$
0
0

Hello,

I have a very simple winform with a data grid view on it.  The data grid view is called dataGridView1.

In my other class I have the following code:

  public class  GetConnection : Form1
    {
        public void RunSql(string tsql)
        {

            SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter(SqlText(), c);

            var commandBuilder = new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.ReadOnly = true;
            dataGridView1.DataSource = dt;
        }

    }

No errors are thrown but the data grid remains blank.  When I copy the code into form1.cs it works fine.  In the definition for dataGridView1 I have made this change:

internal System.Windows.Forms.DataGridView dataGridView1;

I also tried creating a new instance of form1 in my class but I get the same behavoir...no error just a blank data grid.

 Form1 f = new Form1();
            f.dataGridView1.DataSource = dt;

What else am I missing?

Thanks




Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>