Fill DataGridView from SQLite DB (C#)

前端 未结 2 548
抹茶落季
抹茶落季 2021-01-12 18:28

I\'m trying to fill a datagridview from an SQLite database.

I\'ve found plenty of ways of doing this. However, I have pre-existing columns in my dgv (Item, Quantity)

2条回答
  •  长情又很酷
    2021-01-12 19:18

    If you dont want to disturb the columns you can read the rows one by one using SQLiteDataReader and put it into the datagridview..

    private void button1_Click_1(object sender, EventArgs e)
    {
        conn.Open();
        SQLiteCommand comm = new SQLiteCommand("Select * From Patients", conn);
        using (SQLiteDataReader read = comm.ExecuteReader())
        {
            while (read.Read())
            {
                dataGridView1.Rows.Add(new object[] { 
                read.GetValue(0),  // U can use column index
                read.GetValue(read.GetOrdinal("PatientName")),  // Or column name like this
                read.GetValue(read.GetOrdinal("PatientAge")),
                read.GetValue(read.GetOrdinal("PhoneNumber")) 
                });
            }
        }
    
    }
    

提交回复
热议问题