使用Mysql表填充C#ListView

时间:2015-05-11 12:33:26

标签: c# mysql listview

我不明白为什么listview是空的。

enter image description here

enter image description here

刷新按钮= Button2,这是我的代码:

       private void button2_Click(object sender, EventArgs e)
    {

        MySqlDataAdapter ada = new MySqlDataAdapter("select * from Teams", _dbConnect.getConnection());
        DataTable dt = new DataTable();
        ada.Fill(dt);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            ListViewItem listitem = new ListViewItem(dr["team_id"].ToString());
            listitem.SubItems.Add(dr["team_name"].ToString());
            listView1.Items.Add(listitem);
        }
    }

3 个答案:

答案 0 :(得分:2)

您应该向ListView添加至少一列。添加另一列以显示子项目。

enter image description here

答案 1 :(得分:0)

在这种情况下,我不相信你需要子项目,直接添加项目;

listView1.Items.Add(dr["team_name"].ToString());

答案 2 :(得分:0)

尝试此代码:下载并添加对Mysql.Data dll文件的引用

使用MySql.Data.MySqlClient;

public class Square {
public int size;

public Square(int size) {
    this.size = size;
}

public void goSquare(){            
    for (int line=1; line<= size; line++){
        for (int n=1; n<= (line-1); n++) {
            System.out.print ("# ");
        }
        for (int n=1; n<= ((size+1)-line); n++){
            System.out.print ("$ ");
        }
        System.out.println ("");
    }
}

public static void main (String[]args){
    Square square = new Square(6);
    square.goSquare();
}
}