please i have three textbox and datagridview. I have successfully sum up two columns and insert the result into their respective textbox but i now want to divide the two textbox which are integer and insert the result into the third textbox which should be in decimal
here is my code the problem is highlighted in blue.
private void calculate_Click(object sender, EventArgs e)
{
int sumunt = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sumunt += Convert.ToInt32(dataGridView1.Rows[i].Cells["crsunt"].Value);
}
totcrsunt.Text = sumunt.ToString();
int sumgp = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sumgp += Convert.ToInt32(dataGridView1.Rows[i].Cells["crsgp"].Value);
}
totcrsgp.Text = sumgp.ToString();
int grpnt = Convert.ToInt32(totcrsgp);
int unit = Convert.ToInt32(totcrsunt);
int cgpa = grpnt % unit;
mycgpa.Text = cgpa.ToString();
Loading


venkata kumarRank #5,034 · 314 reputationPosted Oct 28, 2013, 4:21 AM EDT
give like this
int cgpa =convert.ToInt32(grpnt % unit);
selva kumarRank #2,987 · 483 reputationPosted Oct 28, 2013, 4:44 AM EDT
int cgpa = grpnt / unit;
mycgpa.Text = Convert.ToString(cgpa);
Fadugba Jeremiah23 reputationPosted Oct 28, 2013, 4:53 AM EDT
Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'system.IConvertible'.
VulpesRank #55 · 29.2K reputationPosted Oct 28, 2013, 6:05 AM EDT
int grpnt = Convert.ToInt32(totcrsgp.Text);
int unit = Convert.ToInt32(totcrsunt.Text);
int cgpa = grpnt % unit;
mycgpa.Text = cgpa.ToString();
or you could replace all that with:
int cgpa = sumgp % sumunt;
mycgpa.Text = cgpa.ToString();
selva kumarRank #2,987 · 483 reputationPosted Oct 28, 2013, 6:08 AM EDT
Fadugba Jeremiah23 reputationPosted Oct 28, 2013, 7:10 AM EDT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CgpaCalculator
{
public partial class Infopage : Form
{
public Infopage()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
level.Items.Add("100.");
level.Items.Add("200.");
level.Items.Add("300.");
level.Items.Add("400.");
level.Items.Add("500.");
level.Items.Add("600.");
level.Focus();
modeofentry.Items.Add("D.E.");
modeofentry.Items.Add("U.T.M.E.");
modeofentry.Focus();
// datagridview combo box
crspnt.Items.Add("0");
crspnt.Items.Add("1");
crspnt.Items.Add("2");
crspnt.Items.Add("3");
crspnt.Items.Add("4");
crspnt.Items.Add("5");
crspnt.Items.Add("6");
crspnt.Items.Add("7");
}
private void save_Click(object sender, EventArgs e)
{
int sumunt = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sumunt += Convert.ToInt32(dataGridView1.Rows[i].Cells["crsunt"].Value);
}
totcrsunt.Text = sumunt.ToString();
int sumgp = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sumgp += Convert.ToInt32(dataGridView1.Rows[i].Cells["crsgp"].Value);
}
totcrsgp.Text = sumgp.ToString();
int grpnt = Convert.ToInt32(totcrsgp.Text);
int unit = Convert.ToInt32(totcrsunt.Text);
int cgpa = grpnt % unit;
mycgpa.Text = cgpa.ToString();
}
private void dataGridView1_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
{
int crsunt = Convert.ToInt32(dataGridView1.CurrentRow.Cells[1].Value);
int crspnt = Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value);
int crsgp = crspnt * crsunt;
dataGridView1.CurrentRow.Cells[4].Value = crsgp;
}
}
}
Jignesh TrivediRank #16 · 50.3K reputationPosted Oct 28, 2013, 8:18 PM EDT
as per my knowledge if you want to result in decimal so all your variable are in to decimal.
for example...
int i=4;
double p = 200.45;
var result = p/i;
so in this case result is int and value is 50.
but same thing
var result = p/(double)i;
so result is double and value is 50.1125
hope you understand.