请教 datagridview
数据库用的是access,一共四列,SEQ,string1,string2,string3
用四列listbox挺简单的
{string PathDb = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Directory.GetCurrentDirectory()+"\\"+strPath;
OleDbDataReader dr;
OleDbConnection conn = new OleDbConnection(PathDb);
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand( "select * from every",conn);
cmd.CommandType = CommandType.Text;
dr = cmd.ExecuteReader();
while (dr.Read())
{
{
listBox1.Items.Add(dr[0].ToString());
listBox2.Items.Add(dr[1].ToString());
listBox3.Items.Add(dr[2].ToString());
listBox4.Items.Add(dr[3].ToString());
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
问题是,如果换成datagridview,我就懵了....
前后都一样
while (dr.Read())
{
//datagridview填充数据
}
读取这块直接郁闷了
求教高手
------解决方案--------------------C# code
while (reader.Read())
{
DataGridViewRow row = dataGridView1.Rows.Add();
row.Cells[0].Value = xxx;
row.Cells[1].Value = xxx;
row.Cells[2].Value = xxx;
row.Cells[3].Value = xxx;
}
------解决方案--------------------
之前在 DataGridView 中创建好列,绑定代码
C# code
while (reader.Read())
{
int index = dataGridView1.Rows.Add();
for (int i = 0; i < 4; i++)
{
dataGridView1.Rows[index].Cells[i].Value = dr[i].ToString();
}
}