如何在单击事件按钮上使用数据库记录填充列表视图 [英] How to populate list view with database records on click event button

查看:71
本文介绍了如何在单击事件按钮上使用数据库记录填充列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在列表视图上显示一组数据库记录,但是由于某种原因,我会收到此错误对象引用未设置为对象的实例.
基本上在我的数据库表上,我有服务器记录,并且分类如下:

我的数据库表称为Food,该表的属性为FoodName&. FoodType.
FoodType字段上的记录分为以下类别:

-入门-这是10条记录
-主要-这是12条记录
-Dessert-这是10条记录

现在我想要的是,当我单击启动程序"按钮时,我想在我的列表视图中显示FoodType启动程序"的10条记录的FoodName.
到目前为止,我已经设法在启动程序"单击事件按钮下执行了此操作(请参见下面的代码),但是由于某种原因,我遇到了错误.

I''m trying to display a set of database records on my listview, but for some reason I get this error Object reference not set to an instance of an object.
Basically on my database table I have serveral records and thay are categorised for instance:

My database table is called Food and the attributes of this table are FoodName & FoodType.
The records on the FoodType field are categorised such as:

-Starter - here are 10 records
-Main - here are 12 records
-Dessert - here are 10 records

Now what I want is that when I click the Starter Button I want to display on my listview the FoodName of 10 records of the FoodType ''Starter''.
So far I have managed to do this under my Starter click event button (see the code below) but for some reason i get an error.

private void cmdStarters_Click(object sender, EventArgs e)
{
   OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
   connBuilder.DataSource = @"C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposMenu.accdb";
   connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
   connBuilder.Add("Jet OLEDB:Engine Type", "5");
  
   string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
   using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
   {
      try
      {
         conn.Open();
        
         OleDbCommand foodsCommand = new OleDbCommand(foodTypeSql, conn);
         OleDbParameter foodType = foodsCommand.Parameters.Add("@foodType", OleDbType.VarChar, 15);
         OleDbDataAdapter foodsDa = new OleDbDataAdapter(foodsCommand);
                   
         foodType.Value = "Starter";
         foodsDa.Fill(DtposMenuDS, "Starter");

         starterTable = DtposMenuDS.Tables["Food"];

         ListViewItem foodItem = new ListViewItem(((DataRowView)DtposMenuBS.Current)["FoodName"].ToString());
         this.listViewItemsInStock.Items.Add(foodItem);
      }
      catch (Exception ex)
      {
         MessageBox.Show("Error: " + ex);
      }
   }
}



谁能告诉我如何解决此问题


在此先感谢


roni



could anyone tell how to fix this problem please


thanks in advance


roni

推荐答案

//将数据从数据集中加载到ListView
私有void LoadList()
{
//从数据集中获取表
DataTable dtable = _DataSet.Tables ["Titles"];
//清除ListView控件
listView1.Items.Clear();
//在ListView控件中显示项目
for(int i = 0; i< dtable.Rows.Count; i ++)
{
DataRow drow = dtable.Rows [i];
//只有未删除的行
如果(drow.RowState!= DataRowState.Deleted)
{
//定义列表项
ListViewItem lvi =新的ListViewItem(drow ["title"].ToString());
lvi.SubItems.Add(drow ["title_id"].ToString());
lvi.SubItems.Add(drow ["price"].ToString());
lvi.SubItems.Add(drow ["pubdate"].ToString());
//将列表项添加到ListView
listView1.Items.Add(lvi);
}
}
}


请投票给我,如果它正在工作... plz :)

使用这种类型的代码....循环遍历数据集的数据表,并将每个项目添加到列表中.
提供反馈意见.
// Load Data from the DataSet into the ListView
private void LoadList()
{
// Get the table from the data set
DataTable dtable = _DataSet.Tables["Titles"];
// Clear the ListView control
listView1.Items.Clear();
// Display items in the ListView control
for (int i = 0; i < dtable.Rows.Count; i++)
{
DataRow drow = dtable.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
ListViewItem lvi = new ListViewItem(drow["title"].ToString());
lvi.SubItems.Add (drow["title_id"].ToString());
lvi.SubItems.Add (drow["price"].ToString());
lvi.SubItems.Add (drow["pubdate"].ToString());
// Add the list items to the ListView
listView1.Items.Add(lvi);
}
}
}


Please vote me if it''s working...plz :)

use this type of code ....loop through the datatable of dataset and add each items into list..

give feedback if working..


这篇关于如何在单击事件按钮上使用数据库记录填充列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆