【问题标题】:Fill Datagridview with MySQL data用 MySQL 数据填充 Datagridview
【发布时间】:2012-10-05 23:19:23
【问题描述】:

我尝试了几次让它工作,但它只是没有用 mysql 数据填充 datagridview,这是我的代码:

string connectionString = "SERVER=localhost;DATABASE=shootsource;UID=root;PASSWORD=;";
        string sql = "SELECT * FROM characters";
        MySqlConnection connection = new MySqlConnection(connectionString);
        connection.Open();
        sCommand = new MySqlCommand(sql, connection);
        sAdapter = new MySqlDataAdapter(sCommand);
        sBuilder = new MySqlCommandBuilder(sAdapter);
        sDs = new DataSet();
        sAdapter.Fill(sDs, "characters");
        sTable = sDs.Tables["characters"];
        connection.Close();
        dataGridView1.DataSource = sDs.Tables["characters"];
        dataGridView1.ReadOnly = true;
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

它只显示 mysql 表“字符”中的列:id、名称、时间等...我如何从数据库中填充它?

这是问题的图片:

【问题讨论】:

  • 它只显示列headers?你是这个意思吗?你确定表里还有数据吗?
  • 是的有..是的..这是一张照片:picz.ro/index.php?id=1dc2d2d989590d263a979c7a097124a0
  • 我看不出有什么问题
  • 你有任何工作代码要测试吗?
  • 没关系,但现在它给出了错误,关于无法将日期/时间转换为字符串...??

标签: c# mysql datagridview


【解决方案1】:

设置数据源后,您需要执行 DataBind

dataGridView1.DataBind();

【讨论】:

    【解决方案2】:
           private void MySQL_ToDatagridview()
        {
            //VarribleKeeper.MySQLConnectionString = your connection string
           //info being your table name
            MySqlConnection mysqlCon = new  
    
            MySqlConnection(VarribleKeeper.MySQLConnectionString);
            mysqlCon.Open();
    
            MySqlDataAdapter MyDA = new MySqlDataAdapter();
            string sqlSelectAll = "SELECT * from info";
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);
    
            DataTable table = new DataTable();
            MyDA.Fill(table);
    
            BindingSource bSource = new BindingSource();
            bSource.DataSource = table;
    
    
            dataGridView1.DataSource = bSource;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多