C# DataGridView 使用实例
using MySql.Data.MySqlClient;
namespace BookMIS
{
public partial class Adminbook : Form
{
public Adminbook()
{
InitializeComponent();
}
private void Adminbook_Load(object sender, EventArgs e)
{
Table();
}
方式一:
//private void Table()
//{
// dataGridView1.Rows.Clear();
// Dao dao = new Dao();
// string sqlStr = "select * from bookinfo";
// MySqlDataReader dc = dao.Reader(sqlStr);
// while (dc.Read())
// {
// dataGridView1.Rows.Add(dc[0].ToString(),dc[1].ToString(),dc[2].ToString(),dc[3].ToString(),dc[4].ToString());
// }
// dc.Close();
// dao.DaoClose();
//}
方式二:
private void Table()
{
dataGridView1.Rows.Clear();
Dao dao = new Dao();
string sqlStr = "select * from bookinfo";
MySqlDataReader dc = dao.Reader(sqlStr);
string a0, a1, a2, a3, a4;
while (dc.Read())
{
a0 = dc[0].ToString();
a1 = dc[1].ToString();
a2 = dc[2].ToString();
a3 = dc[3].ToString();
a4 = dc[4].ToString();
string[] table = { a0, a1, a2, a3, a4 };
dataGridView1.Rows.Add(table);
}
dc.Close();
dao.DaoClose();
}
}
}