A problem with datagridview

I can't get datain my datagridview from DB

Page 1 of 1

8 Replies - 1857 Views - Last Post: 16 December 2009 - 04:09 AM Rate Topic: -----

#1 omart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-December 09

A problem with datagridview

Posted 14 December 2009 - 01:15 PM

Hi there,
I want to display a table from my database in the datagridview, but the problem is that the datagridview is always empty,there not an error message.
My code is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;

namespace gestock
{
	public partial class Form2 : Form
	{

		 SqlConnection c=new SqlConnection();
		public Form2()
		{
			InitializeComponent();
			FillData();
		}

		/*public Form2(SqlConnection cn)
		{
			InitializeComponent();
			c = cn;
		}*/

		private void Form2_Load(object sender, EventArgs e)
		{
			//dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			  
		}
		 
		 void FillData()
		{
			string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\Administrateur\\Mes documents\\stock.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
			c.ConnectionString = connectionString;
			SqlDataAdapter d = new SqlDataAdapter("select * from fournir", c);
			MessageBox.Show("La connexion avec la base de donnée est faite!!!!!");
			DataTable dt = new DataTable();
			d.Fill(dt);
			//d.Fill(dt);
			dataGridView1.DataSource = dt;
			   
		}
	  
	}
}




Thanks everybody

Is This A Good Question/Topic? 0
  • +

Replies To: A problem with datagridview

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: A problem with datagridview

Posted 14 December 2009 - 01:28 PM

It's empty because you never call FillData (thus telling it to populate your grid). Try calling it in the Load event

private void Form2_Load(object sender, EventArgs e)
{
	   FillData();			 
}




EDIT: Did see you were calling it from the constructor (my bad) Try chaning this line
 dataGridView1.DataSource = dt;



To this
 dataGridView1.DataSource = dt.DefaultView;


Was This Post Helpful? 0
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: A problem with datagridview

Posted 14 December 2009 - 01:31 PM

From my experience your data grid view should get populated. Do you have your table in database populated? :-)

View PostPsychoCoder, on 14 Dec, 2009 - 12:28 PM, said:

It's empty because you never call FillData (thus telling it to populate your grid). Try calling it in the Load event

private void Form2_Load(object sender, EventArgs e)
{
	   FillData();			 
}




This method is called in form's constructor and I see no reason why this wouldn't work...
Was This Post Helpful? 0
  • +
  • -

#4 omart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-December 09

Re: A problem with datagridview

Posted 15 December 2009 - 02:18 AM

Hi there
thank you guys for your help
I changed the location of the function call in load event, the result is that the pc crashes for a few seconds, then displays an error message "An unhandled exception of type 'System.StackOverflowException" occurred in mscorlib.dll ".What's wrong? ,and what can I do to solve this problem?
What's that mean a database populated?

THANK YOU EVERYTBODY

Was This Post Helpful? 0
  • +
  • -

#5 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: A problem with datagridview

Posted 15 December 2009 - 02:37 AM

http://msdn.microsof...28VS.71%29.aspx

How many records does your fournir table have?
Was This Post Helpful? 0
  • +
  • -

#6 omart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-December 09

Re: A problem with datagridview

Posted 15 December 2009 - 03:33 AM

View PostFlashM, on 15 Dec, 2009 - 01:37 AM, said:

http://msdn.microsof...28VS.71%29.aspx

How many records does your fournir table have?



This table contains 4 records.
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: A problem with datagridview

Posted 15 December 2009 - 08:56 AM

Just for the fun of it try this modified code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;

namespace gestock
{
	public partial class Form2 : Form
	{

		 SqlConnection c = null;

		public Form2()
		{
			InitializeComponent();
		}

		private void Form2_Load(object sender, EventArgs e)
		{
					  FillData();
		}
		 
		 void FillData()
		{
			c = new SqlConnection(@"Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrateur\Mes documents\stock.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
			SqlDataAdapter d = new SqlDataAdapter("select * from fournir", c);
			MessageBox.Show("La connexion avec la base de donnée est faite!!!!!");
			DataTable dt = new DataTable();
			d.Fill(dt);
			//d.Fill(dt);
			dataGridView1.DataSource = dt.DefaultView;
			   
		}
	 
	}
}



Was This Post Helpful? 0
  • +
  • -

#8 omart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-December 09

Re: A problem with datagridview

Posted 16 December 2009 - 03:18 AM

Hi,
I tested your code, but it does not, an error message appears: "Instance failure" at the call of the function d.Fill (dt)
Was This Post Helpful? 0
  • +
  • -

#9 omart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-December 09

Re: A problem with datagridview

Posted 16 December 2009 - 04:09 AM

Hi guys,
Finally I could see the table in the datagridview, the problem is that I have misconfigured my database, now it's OK
The code works very well.
The code is as follows:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace gestock1
{

	public partial class Form2 : Form
	{

		SqlConnection c = new SqlConnection();
		public Form2()
		{
			InitializeComponent();
			FillData();
		}

		private void Form2_Load(object sender, EventArgs e)
		{
			//dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

			//FillData();
		}

		void FillData()
		{
			string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\Administrateur\\Mes documents\\stockage.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
			c.ConnectionString = connectionString;
			SqlDataAdapter d = new SqlDataAdapter("select * from fournir", c);
			//MessageBox.Show("La connexion avec la base de donnée est faite!!!!!");
			DataTable dt = new DataTable();
			d.Fill(dt);
			//d.Fill(dt);
			dataGridView1.DataSource = dt.DefaultView;

		}

	}
}




;) ;)
Thank you all
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1