Source Code Helpful?
That's project is helpful for those students that's wants to develop / create your on project in C# window form that has totally professional level development criteria.
That's source code has following control functions:
- Add values into dataGridView control
- Calculations in dataGridView Control
- Update same row without repeat that row again
- Add multi-pal record into dataGridView
- Debug Source Code for Result Test
- Use of for loop
That is helpful when we create a invoice in sale system
Watch Video For Debugging the source code
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.Windows.Forms;
namespace DGVDemoAddRows
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Add row when datagridview is empty
if (dataGridView1.Rows.Count == 0)
{
int n= dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = txtId.Text;
dataGridView1.Rows[n].Cells[1].Value = comboBox1.Text;
dataGridView1.Rows[n].Cells[2].Value = txtPrice.Text;
dataGridView1.Rows[n].Cells[3].Value = txtQty.Text;
dataGridView1.Rows[n].Cells[4].Value = Convert.ToDecimal(txtPrice.Text) * Convert.ToDecimal(txtQty.Text);
return;
}
//update existing row value
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == txtId.Text.Trim()) //column1=Id
{
//int n = dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = txtId.Text;
dataGridView1.Rows[i].Cells[1].Value = comboBox1.Text;
dataGridView1.Rows[i].Cells[2].Value = txtPrice.Text;
decimal qty = Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value) + Convert.ToDecimal(txtQty.Text);
dataGridView1.Rows[i].Cells[3].Value = qty.ToString();
dataGridView1.Rows[i].Cells[4].Value = Convert.ToDecimal(txtPrice.Text) * Convert.ToDecimal(qty);
return;
}
}
//add row when rows already present
int a = dataGridView1.Rows.Add();
dataGridView1.Rows[a].Cells[0].Value = txtId.Text;
dataGridView1.Rows[a].Cells[1].Value = comboBox1.Text;
dataGridView1.Rows[a].Cells[2].Value = txtPrice.Text;
dataGridView1.Rows[a].Cells[3].Value = txtQty.Text;
dataGridView1.Rows[a].Cells[4].Value = Convert.ToDecimal(txtPrice.Text) * Convert.ToDecimal(txtQty.Text);
return;
}
}
}

0 Comments