Click here to Skip to main content
Sign Up to vote bad
good
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:45
Edited 25 Sep '12 - 4:51


2 solutions

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  
After setting the DataSource property, you need to call GridView.DataBind()
  Permalink  
Comments
Sandeep Mewara - 25 Sep '12 - 11:02
Not in Winforms. Only in ASP.NET
Kåre Hvid Lind - 25 Sep '12 - 16:08
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:23
dtGridView.DataSource = dataTable; is enough. Where does the bndSource as object comes from ?
Kåre Hvid Lind - 26 Sep '12 - 4:45
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 449
1 Mahesh Bailwal 373
2 Maciej Los 255
3 Rohan Leuva 175
4 CPallini 175
0 Sergey Alexandrovich Kryukov 9,402
1 OriginalGriff 7,204
2 CPallini 3,933
3 Rohan Leuva 3,211
4 Maciej Los 2,743


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid