[英]C# Pass ListView Items from a Function to a ListView on a form?
i am trying to do the following: 我正在尝试执行以下操作:
I have a function.. which Queries SQL and gets some data from a database.. now i want to display those values in a listview which is on a form. 我有一个函数..它查询SQL并从数据库中获取一些数据..现在,我想在表单上的列表视图中显示这些值。 The function is in a SQL Class.
该函数在SQL类中。
It works for one item only, but if i want for example 5 items to be passed it wont work since it only passes the last time, which is logical when you see the code. 它仅适用于一个项目,但是如果我想例如传递5个项目,则它将无法正常工作,因为它仅通过了最后一次,这在您看到代码时是合乎逻辑的。
I cant seem to figure out, how to "referance" a listvie object from a form to the function and pass the items to it. 我似乎无法弄清楚如何“引用” Listvie对象从表单到函数并将项目传递给它。
Here is the code: (i know it returns only one item now, i tried different things.. none worked): 这是代码:(我知道它现在只返回一个项目,我尝试了不同的方法..没有任何效果):
public ListViewItem SQL_GetLogsHistory(string WKS)
{
SqlCommand mySQLCommand = new SqlCommand(Properties.Settings.Default.SQL_GetLogsHistroy, SQLconn);
mySQLCommand.Parameters.AddWithValue("@WKS", WKS);
SqlDataReader reader = mySQLCommand.ExecuteReader();
ListViewItem item = new ListViewItem();
while (reader.Read())
{
item = new ListViewItem(new[] {
reader.GetValue(4).ToString(), // Timestamp
reader.GetValue(2).ToString() // Log
});
}
reader.Close();
return item;
}
you can have list of items 你可以有项目清单
List<ListViewItem> items = new List<ListViewItem>();
inside your loop create new ListViewItem
and set the properties and add it to list 在循环内创建新的
ListViewItem
并设置属性并将其添加到列表
while (reader.Read())
{
ListViewItem item = new ListViewItem();
item = new ListViewItem(new[] {
reader.GetValue(4).ToString(), // Timestamp
reader.GetValue(2).ToString() // Log
});
items.Add(item);
}
finally return items
from the method and change the signature of the method as below 最终从方法中返回
items
并按如下所示更改方法的签名
public List<ListViewItem> SQL_GetLogsHistory(string WKS)
Side Note: better to use using
statements for SqlCommand
and SqlDataReader
旁注:最好对
SqlCommand
和SqlDataReader
使用using
语句
public List<ListViewItem> SQL_GetLogsHistory(string WKS)
{
List<ListViewItem> items = new List<ListViewItem>();
using (SqlCommand mySQLCommand = new SqlCommand(Properties.Settings.Default.SQL_GetLogsHistroy, SQLconn))
{
mySQLCommand.Parameters.AddWithValue("@WKS", WKS);
using (SqlDataReader reader = mySQLCommand.ExecuteReader())
{
while (reader.Read())
{
ListViewItem item = new ListViewItem(new[] {
reader.GetValue(4).ToString(), // Timestamp
reader.GetValue(2).ToString() // Log
});
items.Add(item);
}
}
}
return items;
}
You could use yield return: 您可以使用收益回报:
public IEnumerable<ListViewItem> SQL_GetLogsHistory(string WKS)
{
SqlCommand mySQLCommand = new SqlCommand(Properties.Settings.Default.SQL_GetLogsHistroy, SQLconn);
mySQLCommand.Parameters.AddWithValue("@WKS", WKS);
SqlDataReader reader = mySQLCommand.ExecuteReader();
ListViewItem item = new ListViewItem();
while (reader.Read())
{
yield return new ListViewItem(new[] {
reader.GetValue(4).ToString(), // Timestamp
reader.GetValue(2).ToString() // Log
});
}
reader.Close();
return item;
}
This will reduce multiple enumeration of your data. 这将减少数据的多个枚举。 You can then use this method to create a
List<ListViewItem>
if required: 然后,可以根据需要使用此方法创建
List<ListViewItem>
:
var list = SQL_GetLogsHistory(WKS).ToList();
Or you could directly use this as your list view data source: 或者,您可以直接将其用作列表视图数据源:
listView.DataSource = SQL_GetLogsHistory(WKS);
Do you just need to return a List<ListViewItem>
like this? 您是否只需要返回这样的
List<ListViewItem>
?
public List<ListViewItem> SQL_GetLogsHistory(string WKS)
{
SqlCommand mySQLCommand = new SqlCommand(Properties.Settings.Default.SQL_GetLogsHistroy, SQLconn);
mySQLCommand.Parameters.AddWithValue("@WKS", WKS);
SqlDataReader reader = mySQLCommand.ExecuteReader();
List<ListViewItem> items = new List<ListViewItem>();
while (reader.Read())
{
var item = new ListViewItem(new[] {
reader.GetValue(4).ToString(), // Timestamp
reader.GetValue(2).ToString() // Log
});
items.Add(item);
}
reader.Close();
return items;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.