This is my code
I need help to handle a few errors that i dont know why im seeing
1. Automatically a new grid for Image is created.
2. An error keeps popping up


- 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;
- using System.IO;
- namespace Amal_Inventory
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void button3_Click(object sender, EventArgs e)
- {
- }
- private void button4_Click(object sender, EventArgs e)
- {
- }
- private void button6_Click(object sender, EventArgs e)
- {
- }
- private void button5_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show("Sure ??", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)==DialogResult.OK)
- {
- if (this.dataGridView1.SelectedRows.Count > 0)
- {
- dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
- }
- textBox1.Text = "";
- textBox2.Text = "";
- textBox3.Text = "";
- textBox4.Text = "";
- textBox5.Text = "";
- textBox6.Text = "";
- textBox7.Text = "";
- textBox8.Text = "";
- textBox9.Text = "";
- }
- }
- private void button7_Click(object sender, EventArgs e)
- {
- textBox1.Text = "";
- textBox2.Text = "";
- textBox3.Text = "";
- textBox4.Text = "";
- textBox5.Text = "";
- textBox6.Text = "";
- textBox7.Text = "";
- textBox8.Text = "";
- textBox9.Text = "";
- pictureBox1.Image = "";
- }
- private void button8_Click(object sender, EventArgs e)
- {
- Update();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- int n = dataGridView1.Rows.Add();
- dataGridView1.Rows[n].Cells[0].Value = textBox1.Text;
- dataGridView1.Rows[n].Cells[1].Value = textBox2.Text;
- dataGridView1.Rows[n].Cells[2].Value = textBox3.Text;
- dataGridView1.Rows[n].Cells[3].Value = textBox4.Text;
- dataGridView1.Rows[n].Cells[4].Value = textBox5.Text;
- dataGridView1.Rows[n].Cells[5].Value = textBox6.Text;
- dataGridView1.Rows[n].Cells[6].Value = textBox7.Text;
- dataGridView1.Rows[n].Cells[7].Value = textBox8.Text;
- dataGridView1.Rows[n].Cells[8].Value = textBox9.Text;
- MemoryStream mmst = new MemoryStream();
- pictureBox1.Image.Save(mmst, pictureBox1.Image.RawFormat);
- byte[] img = mmst.ToArray();
- dataGridView1.Rows.Add(img);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- DataGridViewImageColumn dvgimage = new DataGridViewImageColumn();
- dvgimage.HeaderText = "Image";
- dvgimage.ImageLayout = DataGridViewImageCellLayout.Stretch;
- dataGridView1.Columns.Add(dvgimage);
- dataGridView1.RowTemplate.Height = 120;
- dataGridView1.AllowUserToAddRows = false;
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- DialogResult confirm = MessageBox.Show("Are you sure?", "Exit", MessageBoxButtons.YesNo);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog openfiledialogue1 = new OpenFileDialog();
- openfiledialogue1.Filter = "Choose Image (*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif";
- if (openfiledialogue1.ShowDialog() == DialogResult.OK)
- {
- pictureBox1.Image = new Bitmap(openfiledialogue1.FileName);
- }
- }
- }
- }