在c#中从数据库检索数据到dataGridView时清空行

时间:2016-06-20 07:00:04

标签: c# mysql datagridview row data-retrieval

当我从MySQL数据库检索行到dataGridView时,为什么会出现空白行?我最终获得了行数,但它是空的(它没有文本)。

到目前为止,这是我的代码:

        private void button2_Click(object sender, EventArgs e)
    {
        string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand("Select * FROM TopShineDB.Table1 ;", conDataBase);
        using (MySqlConnection conn = new MySqlConnection(constring))
        {
            try
            {
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = cmdDataBase;
                DataTable dt = new DataTable();
                sda.Fill(dt);

                BindingSource bs = new BindingSource();
                bs.DataSource = dt;
                dataGridView1.DataSource = bs;
                sda.Update(dt);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

  

我尝试了下面的代码示例来从数据库中获取数据并绑定到datagridview。

var ConnectionString = "your ConnectionString";
MySqlConnection connect = new MySqlConnection(ConnectionString);
MySqlCommand cmd = new MySqlCommand("your query");
connect.Open();
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet(); 
adp.Fill(ds); 
DataTable dt = new DataTable();
dt = DtSet.Tables[0];
dataGridView1.DataSource = DtSet.Tables[0];
connect.Close();

答案 1 :(得分:0)

试试这个:

private void BindGrid()
{
    string conString = @"Data Source=localhost;port=3306;Initial Catalog=TopShineDB;User Id=root;password=0159";
    using (MySqlConnection con = new MySqlConnection(conString))
    {
        using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM Table1", con))
        {
            cmd.CommandType = CommandType.Text;
            using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
        }
    }
}

答案 2 :(得分:0)

*只需清除您在 DataGridView 中定义的列 *

      private void button2_Click(object sender, EventArgs e)
{
    string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
    MySqlConnection conDataBase = new MySqlConnection(constring);
    MySqlCommand cmdDataBase = new MySqlCommand("Select * FROM TopShineDB.Table1 ;", conDataBase);
    using (MySqlConnection conn = new MySqlConnection(constring))
    {
        try
        {
            MySqlDataAdapter sda = new MySqlDataAdapter();
            sda.SelectCommand = cmdDataBase;
            DataTable dt = new DataTable();
            sda.Fill(dt);

       
                 dataGridView1.Columns.Clear();
            dataGridView1.DataSource = bs;
  
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

c#