Click here to Skip to main content
15,886,065 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
C#
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
Updated 25-Sep-12 4:51am
v2

Hi

Please try replace your code with this one below:

C#
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();
 
Share this answer
 
After setting the DataSource property, you need to call GridView.DataBind()
 
Share this answer
 
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
Herman<T>.Instance 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900