Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Cannot get datagridview to show records

Cannot get datagridview to show records

Scheduled Pinned Locked Moved C#
csharpdatabasegraphicssysadmin
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    brainfuelmedia_
    wrote on last edited by
    #1

    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);

    K D 2 Replies Last reply
    0
    • B brainfuelmedia_

      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);

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      I am pretty sure you need a databind after you set the datasource. Ben

      1 Reply Last reply
      0
      • B brainfuelmedia_

        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);

        D Offline
        D Offline
        Drew McGhie
        wrote on last edited by
        #3

        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);

        B 1 Reply Last reply
        0
        • D Drew McGhie

          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);

          B Offline
          B Offline
          brainfuelmedia_
          wrote on last edited by
          #4

          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!

          D 1 Reply Last reply
          0
          • B brainfuelmedia_

            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!

            D Offline
            D Offline
            Drew McGhie
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups