Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 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 9-Jan-13 17:04pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);
 
Regards,
Daniele.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
jibesh at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 550
1 mhegazy94 460
2 Ravi Bhavnani 230
3 Kornfeld Eliyahu Peter 185
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 7,295
1 OriginalGriff 6,841
2 CPallini 5,420
3 George Jonsson 3,654
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100