Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
}
}
}
Posted

Hi.

I currently use the following extension methods

C#
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);


Regards,
Daniele.
 
Share this answer
 
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
 
Share this answer
 
Comments
Jibesh 10-Jan-13 14:29pm    
Please use the Comment widget of the solution to post your comment, in that way the solution provider gets notification of what you typed. putting your comment as solution doesnt notify Daniele. so please use the proper fields to post your comments in future. thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900