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

Jignesh TrivediPosted Oct 29, 2013, 12:18 AM
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.
Fadugba JeremiahPosted Oct 28, 2013, 11:10 AM
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;
}
}
}
selva kumarPosted Oct 28, 2013, 10:08 AM
VulpesPosted Oct 28, 2013, 10:05 AM
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();
Fadugba JeremiahPosted Oct 28, 2013, 8:53 AM
Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'system.IConvertible'.
selva kumarPosted Oct 28, 2013, 8:44 AM
int cgpa = grpnt / unit;
mycgpa.Text = Convert.ToString(cgpa);
venkata kumarPosted Oct 28, 2013, 8:21 AM
give like this
int cgpa =convert.ToInt32(grpnt % unit);