Click here to Skip to main content
Rate this: bad
good
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 1

After setting the DataSource property, you need to call GridView.DataBind()
  Permalink  
v2
Comments
Sandeep Mewara at 25-Sep-12 11:02am
   
Not in Winforms. Only in ASP.NET
Kåre Hvid Lind at 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 at 25-Sep-12 17:23pm
   
dtGridView.DataSource = dataTable; is enough. Where does the bndSource as object comes from ?
Kåre Hvid Lind at 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 ?
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  

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

  Print Answers RSS
0 Maciej Los 580
1 OriginalGriff 443
2 CPallini 189
3 CHill60 180
4 Peter Leow 175
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,958
2 Maciej Los 3,269
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2014
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