Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Problems with ListView output

$
0
0

Hello,

I have a Windows Forms Application that Access a database and displays the contents during a Form1_Load event. The data pulled from the database is a string of numbers that looks like 1-2-3-4-5. The goal of this program is to determine how many time a single number comes up. so if i have three entries:

   5-10-15-18-25,

   3-9-14-18-30,

   5-12-18-25-32

the program should output something like:(how its outputted is not important, just as long as i can see how many times each number comes up)

(3, 1)(5, 2)(9, 1)(10, 1)(12, 1)(15, 1)(18, 3)(25, 2)(30, 1)(32, 1)

To get the numbers counted my research found that a Dictionary would work best, but if there is a better way I'd like to know about it. While the numbers were string format I kept getting an error message from the IDE. Ihat is why the number string was placed into an int array.

I currently have a ListBox that displays the set of numbers pulled from the database with the hyphens removed. Next to the list box is a ListView which is where I want the numbers with how many times they appear to be shown. I tried both a ListBox and ListView to display the counted numbers and both controls show ListViewItem:{} for each line

while (dbRead.Read()) { string number; number = (dbRead["Table-Name"].ToString()); number = number.Replace("-", " "); this.listBox1.Items.Add(number); int[] count = number.Split(' ').Select(n => Convert.ToInt32(n)).ToArray(); Dictionary<int, int> counter = new Dictionary<int,int>(); foreach (int counted in count) { if (counter.ContainsKey(counted)) counter[counted]++; else counter[counted] = 1; } foreach (KeyValuePair<int, int> entry in counter) { ListViewItem item = new ListViewItem(); item.SubItems.Add(Convert.ToString(entry.Key));

item.SubItems.Add(Convert.ToString(entry.Value)); listView1.Items.Add(Convert.ToString(item)); }




Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>