access数据库里面有一张表,通过listview控件显示出来
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
using System.Windows;
namespace WindowsApplication1
{
class Class1
{
public static OleDbConnection GreatConn()
{
string strConnection = "provider=Microsoft.jet.OLEDB.4.0;data Source=" + Application.StartupPath + "/db1.mdb;";
OleDbConnection objCon = new OleDbConnection(strConnection);
return objCon;
}
public static DataTable GetTabel(string strsql)
{
OleDbConnection Ocon = GreatConn();
Ocon.Open();
OleDbDataAdapter oda = new OleDbDataAdapter(strsql, Ocon);
DataTable dt = new DataTable("uinfo");
oda.Fill(dt);
return dt;
}
public static void BindListView(ListView lv, DataTable dt) //将DataTable写入ListView
{
lv.Clear();
lv.View = View.Details;
lv.FullRowSelect = true;
foreach (DataColumn dc in dt.Columns)
{
lv.Columns.Add(dc.ColumnName);
}
foreach (DataRow dr in dt.Rows)
{
ListViewItem lvi = new ListViewItem(dr.ToString());
lvi.SubItems.Add(dr.ToString());
lv.Items.Add(lvi);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataAll();
}
void DataAll()
{
listView1.Items.Clear();
DataTable dt =Class1.GetTabel("select UName, UCity from Uinfo");
Class1.BindListView(listView1, dt);
}
}
}
转自:http://bbs.csdn.net/topics/280039986
页:
[1]