0

我试图用 MySql 查询的结果填充 DataGridView,但是它们并没有全部进入。这是我的代码:

try
{
                conn.Open();
                player_search = new MySqlCommand("SELECT * FROM admin;", conn);
                reader = player_search.ExecuteReader();

                int counter = 0;
                while (reader.Read())
                {

                        player_list[0, counter].Value = reader.GetString(0);
                        player_list[1, counter].Value = reader.GetString(1);
                        player_list[2, counter].Value = reader.GetString(6);
                        player_list[3, counter].Value = reader.GetString(7);
                        player_list[4, counter].Value = reader.GetString(8);
                        player_list[5, counter].Value = reader.GetString(9);
                        player_list[6, counter].Value = "Remove";

                        counter = counter+1;

                }
            }

但是它并没有全部进去。它只插入查询的第一行?为什么要这样做?我没有错误?

4

2 回答 2

0

解决了!我不得不做一个冗长的方法,但它有效!

MySqlCommand mysqlcmd = new MySqlCommand("SELECT * FROM admin;", conn);
            MySqlDataAdapter mysqlad = new MySqlDataAdapter(mysqlcmd);
            DataSet ds = new DataSet();
            mysqlad.Fill(ds);
            DataTable dt = ds.Tables[0];
            player_list.DataSource = dt;
            int rowIndex = 0;
            foreach (DataRow row in dt.Rows)
            {
                int i = 0;

                foreach (var item in row.ItemArray)
                {
                    if (i == 0) {
                        player_list[0, rowIndex].Value = item.ToString();
                    }
                    if (i == 1) {
                        player_list[1, rowIndex].Value = item.ToString();
                    }
                    if (i == 4)
                    {
                        player_list[2, rowIndex].Value = item.ToString();
                    }
                    if (i == 7)
                    {
                        player_list[3, rowIndex].Value = item.ToString();
                    }
                    if (i == 8)
                    {
                        player_list[4, rowIndex].Value = item.ToString();
                    }
                    if (i == 9)
                    {
                        player_list[5, rowIndex].Value = item.ToString();
                    }
                    player_list[6, rowIndex].Value = "Remove";
                    ++i;
                }
                ++rowIndex;
                i = 0;
            }
于 2013-10-25T14:50:55.507 回答
0

我建议您尝试使用 DataSet 获取它:

public DataTable GetDBDataTable(MySqlConnection dbconnection, string table, string columns = "*", string clause = "")
    {
        MySqlCommand mysqlcmd = new MySqlCommand("SELECT " + columns + " FROM " + table + " " + clause +";", dbconnection);
        MySqlDataAdapter mysqlad = new MySqlDataAdapter(mysqlcmd);
        DataSet ds = new DataSet();
        mysqlad.Fill(ds);
        DataTable dt = ds.Tables[0];
        return dt;
    }

 UPDATE:     player_list.DataSource(GetDBDataTable(...));
             player_list.DataBind();
于 2013-10-25T08:50:49.130 回答