Question multiplication table in DataGridView ?

Hashirmama Senji

New member
Joined
Jun 11, 2014
Messages
2
Programming Experience
Beginner
Hello, I am a beginner in C# and I have made a program. Here is the code:
C#:
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            double of = 0;
            double from = 0;
            double to = 0;
            double ans = 0;
            Table.Items.Clear();
            if (double.TryParse(Of.Text, out of) && double.TryParse(From.Text, out from) && double.TryParse(To.Text, out to))
            {


                for (double i = from; i <= to; i++)
                {
                    ans = i * of;
                    Table.Items.Add(of + " x " + i + " = " + ans.ToString());
                }
            }
            else
            {
                MessageBox.Show("Please enter a valid value");
            }
        }
Visual Studio...
This is a multiplication table generator. I have noticed that adding too many rows for listboxes made the program hang. Someone told me that I can use data grid view to overcome that problem. However,since I am a beginner, I need a little help to do that. I have searched internet but they are all codes about how to add SQL data to data grid view.
Any help will be appreciated :)
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
The question of whether to use a ListBox or DataGridView does not depend on the number of items. There's no issue adding a relatively large number of items to a ListBox if you do it properly. Adding a large number of items to a DataGridView will be just as slow or even slower if you don't do it the right way. The question is whether you want a simple list of items that will be displayed in one column or you want a table of data that will be displayed in multiple columns. If it's the former then you should use a ListBox and if it's the latter then you should use a DataGridView.

If you do use a ListBox then you should not be calling Add in a loop. You should put all your items into an array or collection and then either call AddRange or bind via the DataSource property to add all items in a single batch.

If you do use a DataGridView then there are plenty of examples around. Whether or not the data comes from a database is irrelevant. Data is data regardless of where it comes from and gets added to the grid the same way regardless. Again, you can add the data directly or you can put it in an appropriate data structure and bind it. Choose whether you want to use the grid bound or unbound and then find an appropriate example to follow.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
The question of whether to use a ListBox or DataGridView does not depend on the number of items. There's no issue adding a relatively large number of items to a ListBox if you do it properly. Adding a large number of items to a DataGridView will be just as slow or even slower if you don't do it the right way. The question is whether you want a simple list of items that will be displayed in one column or you want a table of data that will be displayed in multiple columns. If it's the former then you should use a ListBox and if it's the latter then you should use a DataGridView.

If you do use a ListBox then you should not be calling Add in a loop. You should put all your items into an array or collection and then either call AddRange or bind via the DataSource property to add all items in a single batch.

If you do use a DataGridView then there are plenty of examples around. Whether or not the data comes from a database is irrelevant. Data is data regardless of where it comes from and gets added to the grid the same way regardless. Again, you can add the data directly or you can put it in an appropriate data structure and bind it. Choose whether you want to use the grid bound or unbound and then find an appropriate example to follow.
 
Top Bottom