Listview DataGridView 求助 急!!! 在线等

来源:百度知道 编辑:UC知道 时间:2024/07/04 10:32:39
废话不多说:看图实现效果

我的数据库处理代码:
/**
*
* 查询Power中的所有
*/
public DataSet getAll()
{
String powerAll = "select PowerName as 帐户名称 ,PowerKey as 密码,PowerRange as 权限设置 from Power";

try
{
this.getConn();
oddap = new OleDbDataAdapter(powerAll, odcon);

ds = new DataSet();
oddap.Fill(ds);

}
catch (Exception e)
{
throw new Exception("Power" + e.Message);
}
return ds;
}

public ListView getListView()
{
String powerAll = "select PowerName,PowerKey,PowerRange from Power";
ListView listViewResult = new ListView();

try
{

数据库为:Access数据库;
你上面的那个 public DataSet getAll()
方法里面:oddap.Fill(ds);括号里面是不是少了第二个参数??一般写成oddap.Fill(ds,"数据表名");

还有,你在下面那个方法里面的while 循环里面给listview 加载数据有问题;
listview加载数据你可以这样:(在while 循环下)

这一句是给第一列(用户名称)加载的,这也就是ListVIew不同于DataGridView的地方:
listviewItem lvi=new listviewItem(oddr["PowerName"].ToString()
);
然后这一句是给每一行的第一列的子项添加数据;
lvi.SubItems.AddRang(new string[]{oddr["PowerKey"].ToString(),oddr["PowerRange"].ToString()});

最后将每一行的实例加载到控件listview里面
listViewResult.Items.Add(lvi);

路过~希望可以帮助你~