如何通过跳过列将值插入ListViewItem? [英] how to insert values into ListViewItem by skipping column?
问题描述
我有一个包含3列的ListViewItem(货币,买入,卖出)。我想根据我从源表中获得的交换类型显示买入/卖出列中的汇率。
有人可以提供一些有关如何执行此操作的线索吗?下面是我的代码。
目前所有内容都显示在Buy列上。
public void getExchangeRates()
{
lsvExchangeRate.Clear();
lsvExchangeRate.Columns.Add(Currency,100,HorizontalAlignment.Left);
lsvExchangeRate.Columns .Add(Buy,100,HorizontalAlignment.Left);
lsvExchangeRate.Columns.Add(Sell,100,HorizontalAlignment.Left);
尝试
{
List< getexchangeratesvo> geRate = erBAL.getExchangeRatesBAL()。ToList();
foreach(geRate中的getExchangeRatesVO grVO)
{
ListViewItem myItem = new ListViewItem(grVO.currencyName);
if(grVO.exchangeType ==Buy)
{
myItem .SubItems.Add(grVO.exchangeRate.ToString());
lsvExchangeRate.Items.Add(myItem);
}
else < br $>
{
myItem.SubItems.Add(grVO.exchangeRate.ToString());
lsvExchangeRate.Items.Add(myItem);
}
}
}
Hi,
I have a ListViewItem with 3 columns (Currency, Buy, Sell). I want to show the rate in Buy/Sell Columns base on the exchange type i get from the source table.
Could someone provide some clues on how to do this? Below is my code.
Currently everything displays on Buy column.
public void getExchangeRates()
{
lsvExchangeRate.Clear();
lsvExchangeRate.Columns.Add("Currency", 100, HorizontalAlignment.Left);
lsvExchangeRate.Columns.Add("Buy", 100, HorizontalAlignment.Left);
lsvExchangeRate.Columns.Add("Sell", 100, HorizontalAlignment.Left);
try
{
List<getexchangeratesvo> geRate = erBAL.getExchangeRatesBAL().ToList();
foreach (getExchangeRatesVO grVO in geRate)
{
ListViewItem myItem = new ListViewItem(grVO.currencyName);
if (grVO.exchangeType == "Buy")
{
myItem.SubItems.Add(grVO.exchangeRate.ToString());
lsvExchangeRate.Items.Add(myItem);
}
else
{
myItem.SubItems.Add(grVO.exchangeRate.ToString());
lsvExchangeRate.Items.Add(myItem);
}
}
}
推荐答案
嗨。
我目前使用以下扩展方法
Hi.
I currently use the following extension methods
public static partial class ListViewSubItemExtensions
{
public static void SetText(this ListViewItem.ListViewSubItemCollection self,
int columnIndex, string text)
{
while (self.Count <= columnIndex)
self.Add(string.Empty);
self[columnIndex].Text = text;
}
public static void SetText<T>(this ListViewItem.ListViewSubItemCollection self,
int columnIndex, T value)
{
SetText(self, columnIndex, value.ToString());
}
public static void SetText(this ListViewItem.ListViewSubItemCollection self,
ColumnHeader column, string text)
{
SetText(self, column.Index, text);
}
public static void SetText<T>(this ListViewItem.ListViewSubItemCollection self,
ColumnHeader column, T value)
{
SetText(self, column.Index, value.ToString());
}
}
循环体将变为:
the body of your loop would become:
ListViewItem myItem = new ListViewItem(grVO.currencyName);
if (grVO.exchangeType == "Buy")
myItem.SubItems.SetText(1, grVO.exchangeRate);
else
myItem.SubItems.SetText(2, grVO.exchangeRate);
lsvExchangeRate.Items.Add(myItem);
问候,
Daniele。
Regards,
Daniele.
谢谢Daniele。我非常感谢你的帮助。您提供的代码完美无缺。我是新手编程,看到你的解决方案后我还有很长的路要走。
问候,
Nate
Thank you Daniele. I really appreciate the help. The code you provided work perfectly. I am new to to programming, after seeing your solutions I have a long way to go.
Regards,
Nate
这篇关于如何通过跳过列将值插入ListViewItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!