Cannot get datagridview to show records
-
I'm a noob to C#, so bear with me. I have the following class and form:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Text; using System.Windows.Forms; namespace tracker { public partial class frmMain : Form { SqlConnection objDBConnection; SqlDataAdapter objDBAdapter; DataSet objDBDataSet; DataGridView objDBGridView; public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { this.objDBConnection = new SqlConnection("Server=BigHouse;Database=tracker;Trusted_Connection=yes"); try { this.objDBConnection.Open(); this.toolStripStatusLabel1.Text = "Connected to Tracker database"; } catch (Exception connerr) { this.toolStripStatusLabel1.Text = "Cannot connect to Tracker database"; } this.getProjectGridData(); /* test data */ DataRow r = this.objDBDataSet.Tables["tbl_projects_main"].Rows[0]; Console.WriteLine("num records = " + this.objDBDataSet.Tables["tbl_projects_main"].Rows.Count + " and " + r["prj_name"].ToString()); /* END test data */ this.objDBGridView = new DataGridView(); this.objDBGridView.DataSource = this.objDBDataSet.Tables["tbl_projects_main"]; this.objDBGridView.AutoGenerateColumns = true; this.objDBGridView.Location = new Point(0, 0); this.objDBGridView.Size = new Size(700, 490); this.objDBGridView.MinimumSize = new Size(700, 490); this.objDBGridView.BackgroundColor = System.Drawing.Color.AliceBlue; this.objDBGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); this.objDBGridView.Visible = true; } private void getProjectGridData() { string strSql; strSql = "SELECT * FROM dbo.tbl_projects_main"; this.objDBDataSet = new DataSet(); this.objDBAdapter = new SqlDataAdapter(); this.objDBAdapter.SelectCommand = new SqlCommand(strSql, this.objDBConnection);
-
I'm a noob to C#, so bear with me. I have the following class and form:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Text; using System.Windows.Forms; namespace tracker { public partial class frmMain : Form { SqlConnection objDBConnection; SqlDataAdapter objDBAdapter; DataSet objDBDataSet; DataGridView objDBGridView; public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { this.objDBConnection = new SqlConnection("Server=BigHouse;Database=tracker;Trusted_Connection=yes"); try { this.objDBConnection.Open(); this.toolStripStatusLabel1.Text = "Connected to Tracker database"; } catch (Exception connerr) { this.toolStripStatusLabel1.Text = "Cannot connect to Tracker database"; } this.getProjectGridData(); /* test data */ DataRow r = this.objDBDataSet.Tables["tbl_projects_main"].Rows[0]; Console.WriteLine("num records = " + this.objDBDataSet.Tables["tbl_projects_main"].Rows.Count + " and " + r["prj_name"].ToString()); /* END test data */ this.objDBGridView = new DataGridView(); this.objDBGridView.DataSource = this.objDBDataSet.Tables["tbl_projects_main"]; this.objDBGridView.AutoGenerateColumns = true; this.objDBGridView.Location = new Point(0, 0); this.objDBGridView.Size = new Size(700, 490); this.objDBGridView.MinimumSize = new Size(700, 490); this.objDBGridView.BackgroundColor = System.Drawing.Color.AliceBlue; this.objDBGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); this.objDBGridView.Visible = true; } private void getProjectGridData() { string strSql; strSql = "SELECT * FROM dbo.tbl_projects_main"; this.objDBDataSet = new DataSet(); this.objDBAdapter = new SqlDataAdapter(); this.objDBAdapter.SelectCommand = new SqlCommand(strSql, this.objDBConnection);
-
I'm a noob to C#, so bear with me. I have the following class and form:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Text; using System.Windows.Forms; namespace tracker { public partial class frmMain : Form { SqlConnection objDBConnection; SqlDataAdapter objDBAdapter; DataSet objDBDataSet; DataGridView objDBGridView; public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { this.objDBConnection = new SqlConnection("Server=BigHouse;Database=tracker;Trusted_Connection=yes"); try { this.objDBConnection.Open(); this.toolStripStatusLabel1.Text = "Connected to Tracker database"; } catch (Exception connerr) { this.toolStripStatusLabel1.Text = "Cannot connect to Tracker database"; } this.getProjectGridData(); /* test data */ DataRow r = this.objDBDataSet.Tables["tbl_projects_main"].Rows[0]; Console.WriteLine("num records = " + this.objDBDataSet.Tables["tbl_projects_main"].Rows.Count + " and " + r["prj_name"].ToString()); /* END test data */ this.objDBGridView = new DataGridView(); this.objDBGridView.DataSource = this.objDBDataSet.Tables["tbl_projects_main"]; this.objDBGridView.AutoGenerateColumns = true; this.objDBGridView.Location = new Point(0, 0); this.objDBGridView.Size = new Size(700, 490); this.objDBGridView.MinimumSize = new Size(700, 490); this.objDBGridView.BackgroundColor = System.Drawing.Color.AliceBlue; this.objDBGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); this.objDBGridView.Visible = true; } private void getProjectGridData() { string strSql; strSql = "SELECT * FROM dbo.tbl_projects_main"; this.objDBDataSet = new DataSet(); this.objDBAdapter = new SqlDataAdapter(); this.objDBAdapter.SelectCommand = new SqlCommand(strSql, this.objDBConnection);
As far as I can see, you do all the data stuff fine, but you never actually add the datagridview to the form itself. Try this.Controls.Add(objDBGridView);
-
As far as I can see, you do all the data stuff fine, but you never actually add the datagridview to the form itself. Try this.Controls.Add(objDBGridView);
Thanks to both of you. The real kicker was this.Controls.Add(objDBGridView). I had looked at that before and thought, "Nah. Couldn't be." Well, I was wrong. :D Thanks!
-
Thanks to both of you. The real kicker was this.Controls.Add(objDBGridView). I had looked at that before and thought, "Nah. Couldn't be." Well, I was wrong. :D Thanks!
The fact that you're using the DataGridView means you're using VS.Net 2005. The designer is really helpful in setting up datagridview columns and formatting if you need anything beyond basic formatting and display. Check that out if you get a chance.