Click here to Skip to main content
12,356,455 members (67,669 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# SQL-Server
For the populateDataGridView()-method below I have an instance of DataGridView called dtGridView and an instance of a BindingSource called bndSource.

I can see that the data is written to bndSource, but the data does not get from the bndSource to the dtGridView.

My question is:

Why do the data from bndSource not display in dtGridView ?

Best regards,

KL
private void populateDataGridView()
        {
            string connectionString = "Data Source=MY-PC;Initial Catalog=TestDataBase;Integrated Security=SSPI";
            string sqlQuery = "SELECT * From dataTable";
            SqlConnection connection = new SqlConnection(connectionString);
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlQuery, connection);
            
            DataTable dataTable = new DataTable();
            
            connection.Open();
            dataAdapter.Fill(dataTable);
            connection.Close();
            bndSource.DataSource = dataTable;
              
            dtGridView.DataSource = bndSource;
        }
Posted 25-Sep-12 4:45am
Edited 25-Sep-12 4:51am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hi

Please try replace your code with this one below:

string connectionString = "Data Source=MY-PC;Initial Catalog=TestDataBase;Integrated Security=SSPI";
            string sqlQuery = "SELECT * From dataTable";
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand dbCmd = new SqlCommand(sqlQuery, connection);
            connection.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
 
            dbAdapter.SelectCommand = dbCmd;
            dataAdapter.Fill(ds,"dataTable");  
            dtGridView.DataSource = ds.Tables["dataTable"];
            connection.Close();
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

After setting the DataSource property, you need to call GridView.DataBind()
  Permalink  
v2
Comments
Sandeep Mewara 25-Sep-12 11:02am
   
Not in Winforms. Only in ASP.NET
Kåre Hvid Lind 25-Sep-12 16:08pm
   
There is no method called DataBind() for the DataGridView in Visual C# 2012.

Is there an alternative way ?

Best regards,


Kåre
digimanus 25-Sep-12 17:23pm
   
dtGridView.DataSource = dataTable; is enough. Where does the bndSource as object comes from ?
Kåre Hvid Lind 26-Sep-12 4:45am
   
Well that does not work either. bndSource is an instance of the BindingSource which manages the communication with the database.

The right number of rows are inserted into the database but the numbers are not showing.


Do you know why ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100