How To Create Multiplication Table In C#
In This C# Tutorial We Will See How To Mutiplication Table Using DataGridView And ListBox In CSharp Programming Language.
Project Source Code:
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 WindowsFormsApplication1
{
public partial class C_MLTP_Table : Form
{
public C_MLTP_Table()
{
InitializeComponent();
}
DataTable table = new DataTable();
private void C_MLTP_Table_Load(object sender, EventArgs e)
{
table.Columns.Add("Select A Number", typeof(string));
dataGridView1.DataSource = table;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns[0].DefaultCellStyle.Font =
new Font("Tahoma", 16, FontStyle.Italic);
// Header Text
dataGridView1.Columns[0].HeaderCell.Style.Font =
new Font("Tahoma", 20, FontStyle.Bold);
// center header text
dataGridView1.Columns[0].HeaderCell.Style.Alignment =
DataGridViewContentAlignment.MiddleCenter;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
table.Clear();
int n1 = int.Parse(listBox1.SelectedItem.ToString());
for(int i = 1; i < 16; i++)
{
int result = n1 * i;
table.Rows.Add(n1 + " X " + i + " = " + result);
}
// change column header text
dataGridView1.Columns[0].HeaderText = "<< " + n1 + " >>";
dataGridView1.DataSource = table;
// change datagridview rows color
for(int i = 0; i < dataGridView1.Rows.Count; i++)
{
if(i%2 == 0)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
}
else
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightCoral;
}
}
}
}
}
// OUTPUT :