使用sql查询结果填充datagridview

时间:2013-08-07 20:34:06

标签: c# winforms datagridview

我正在尝试呈现查询结果,但我一直在获取空白数据网格。 这就像数据本身不可见

这是我的代码:

 private void Employee_Report_Load(object sender, EventArgs e)
 {
     string select = "SELECT * FROM tblEmployee";
     Connection c = new Connection();
     SqlDataAdapter dataAdapter = new SqlDataAdapter(select, c.con); //c.con is the connection string
     SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

     DataTable table = new DataTable();
     table.Locale = System.Globalization.CultureInfo.InvariantCulture;
     dataAdapter.Fill(table);
     bindingSource1.DataSource = table;

     dataGridView1.ReadOnly = true;        
     dataGridView1.DataSource = bindingSource1;
}

这段代码出了什么问题?

10 个答案:

答案 0 :(得分:35)

这是你的代码修好了。接下来忘记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];

答案 1 :(得分:7)

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;

答案 2 :(得分:2)

您不需要bindingSource1

只需设置dataGridView1.DataSource = table;

即可

答案 3 :(得分:1)

尝试将DataGridView绑定到DefaultView的{​​{1}}:

DataTable

答案 4 :(得分:0)

如果将数据源设置为添加到表单但未使用的数据集,则可能会获得空白数据网格。如果您是基于上述代码以编程方式设置dataSource,请将此项设置为“无”。

答案 5 :(得分:0)

您可以尝试此示例,并始终检查连接字符串,您可以将此示例与或使用out bindingsource,您可以将数据加载到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.");
                }

            }

        }

 }

答案 6 :(得分:0)

您必须将属性表添加到DataGridView数据源

 dataGridView1.DataSource = table.Tables[0];

答案 7 :(得分:0)

如果您正在使用mysql,则可以使用此代码。

string con = "SERVER=localhost; user id=root; password=; database=databasename";
    private void loaddata()
{
MySqlConnection connect = new MySqlConnection(con);
connect.Open();
try
{
MySqlCommand cmd = connect.CreateCommand();
cmd.CommandText = "SELECT * FROM DATA1";
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
datagrid.DataSource = dt;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

答案 8 :(得分:0)

这应该是最安全且容易出错的查询:

    public void Load_Data()
        {
            using (SqlConnection connection = new SqlConnection(DatabaseServices.connectionString)) //use your connection string here
            {
                var bindingSource = new BindingSource();
                string fetachSlidesRecentSQL = "select top (50) * from dbo.slides order by created_date desc";
                using (SqlDataAdapter dataAdapter = new SqlDataAdapter(fetachSlidesRecentSQL, connection))
                {
                    try
                    {
                       SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

                        DataTable table = new DataTable();
                        dataAdapter.Fill(table);
                        bindingSource.DataSource = table;
                        recent_slides_grd_view.ReadOnly = true;
                        recent_slides_grd_view.DataSource = bindingSource;
                    }
                    catch (SqlException ex)
                    {
                       MessageBox.Show(ex.Message.ToString(), "ERROR Loading");
                    }
                    finally
                    {
                        connection.Close();
                    }
                }

            }
        }

答案 9 :(得分:0)

晚了很多年,但这是最简单的方法,以防万一。

7

使用 String connectionString = @"Data Source=LOCALHOST;Initial Catalog=DB;Integrated Security=true"; SqlConnection cnn = new SqlConnection(connectionString); SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tblEmployee;", cnn); DataTable data = new DataTable(); sda.Fill(data); DataGridView1.DataSource = data; 不是必需的,DataSet 应该足够好。 DataTable 也是不必要的。