Click here to Skip to main content
Sign Up to vote bad
good
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:04


2 solutions

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  
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 - 10 Jan '13 - 14:29
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 435
1 OriginalGriff 315
2 Arun Vasu 293
3 CPallini 213
4 Zoltán Zörgő 194
0 Sergey Alexandrovich Kryukov 10,005
1 OriginalGriff 7,654
2 CPallini 4,171
3 Rohan Leuva 3,447
4 Maciej Los 2,974


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid