Populate a datagridview with sql query results

前端 未结 9 1075
旧巷少年郎
旧巷少年郎 2020-12-02 23:02

I\'m trying to present query results, but I keep getting a blank data grid. It\'s like the data itself is not visible

Here is my code:

 private void         


        
相关标签:
9条回答
  • 2020-12-02 23:32
    String strConnection = Properties.Settings.Default.BooksConnectionString;
    SqlConnection con = new SqlConnection(strConnection);
    
    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.Connection = con;
    sqlCmd.CommandType = CommandType.Text;
    sqlCmd.CommandText = "Select * from titles";
    SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
    
    DataTable dtRecord = new DataTable();
    sqlDataAdap.Fill(dtRecord);
    dataGridView1.DataSource = dtRecord;
    
    0 讨论(0)
  • 2020-12-02 23:37

    You may get a blank data grid if you set the data Source to a Dataset that you added to the form but is not being used. Set this to None if you are programatically setting your dataSource based on the above codes.

    0 讨论(0)
  • 2020-12-02 23:38

    Here's your code fixed up. Next forget bindingsource

     var select = "SELECT * FROM tblEmployee";
     var c = new SqlConnection(yourConnectionString); // Your Connection String here
     var dataAdapter = new SqlDataAdapter(select, c); 
    
     var commandBuilder = new SqlCommandBuilder(dataAdapter);
     var ds = new DataSet();
     dataAdapter.Fill(ds);
     dataGridView1.ReadOnly = true; 
     dataGridView1.DataSource = ds.Tables[0];
    
    0 讨论(0)
  • 2020-12-02 23:39

    you have to add the property Tables to the DataGridView Data Source

     dataGridView1.DataSource = table.Tables[0];
    
    0 讨论(0)
  • 2020-12-02 23:40

    You may try this sample, and always check your Connection String, you can use this example with or with out bindingsource you can load the data to datagridview.

    private void Employee_Report_Load(object sender, EventArgs e)
    {
            var table = new DataTable();
    
            var connection = "ConnectionString";
    
            using (var con = new SqlConnection { ConnectionString = connection })
            {
                using (var command = new SqlCommand { Connection = con })
                {
    
                    if (con.State == ConnectionState.Open)
                    {
                        con.Close();
                    }
    
                    con.Open();
    
                    try
                    {
                        command.CommandText = @"SELECT * FROM tblEmployee";
                        table.Load(command.ExecuteReader());
    
                        bindingSource1.DataSource = table;
    
                        dataGridView1.ReadOnly = true;
                        dataGridView1.DataSource = bindingSource1;
    
                    }
                    catch(SqlException ex)
                    {
                        MessageBox.Show(ex.Message + " sql query error.");
                    }
    
                }
    
            }
    
     }
    
    0 讨论(0)
  • 2020-12-02 23:47

    Try binding your DataGridView to the DefaultView of the DataTable:

    dataGridView1.DataSource = table.DefaultView;
    
    0 讨论(0)
提交回复
热议问题