I have a form with a datagridview that is connected to a access database. I am having issues to get my update button and my delete button to function properly. I can click a row in the datagridview and populate textboxes, but when I edit the textboxes and try to update the row and database, I'm not getting any useful results, only a error on the delete button click(button1)'Index 1 is either negative or above rows count.' The error is on the stockBindingSource.RemoveAt(rowIndex);
line.
Please can anyone see what is the issue and advice me on how I can rectify this issue? Thank you
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
namespace AMS_POS
{
public partial class Home : Form
{
OleDbCommand cmd;
int ID = 0;
public Home()
{
InitializeComponent();
}
private DataTable DTStock = new DataTable();
private DataTable DTCust = new DataTable();
private DataTable DTSuppl = new DataTable();
private void Home_Load(object sender, EventArgs e)
{
DateLbl.Text = DateTime.Now.ToLongDateString();
TimeLbl.Text = DateTime.Now.ToLongTimeString();
dataGridView1.DataSource = GetDataStock();
dataGridView4.DataSource = GetDataCust();
dataGridView3.DataSource = GetDataSuppl();
// TODO: This line of code loads data into the 'aMSDataDataSet.Customers' table. You can move, or remove it, as needed.
this.customersTableAdapter.Fill(this.AMSDataDataSet.Customers);
// TODO: This line of code loads data into the 'aMSDataDataSet.Suppliers' table. You can move, or remove it, as needed.
this.suppliersTableAdapter.Fill(this.AMSDataDataSet.Suppliers);
}
private DataTable GetDataStock()
{
string conns = ConfigurationManager.ConnectionStrings["AMS_POS.Properties.Settings.AMSDataConnectionString"].ConnectionString;
using (OleDbConnection constr = new OleDbConnection(conns))
{
using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM Stock", constr))
{
constr.Open();
OleDbDataReader reader = cmd.ExecuteReader();
DTStock.Load(reader);
}
}
return DTStock;
}
private DataTable GetDataSuppl()
{
string connsl = ConfigurationManager.ConnectionStrings["AMS_POS.Properties.Settings.AMSDataConnectionString"].ConnectionString;
using (OleDbConnection constra = new OleDbConnection(connsl))
{
using (OleDbCommand cmda = new OleDbCommand("SELECT * FROM Suppliers", constra))
{
constra.Open();
OleDbDataReader reader = cmda.ExecuteReader();
DTSuppl.Load(reader);
}
}
return DTSuppl;
}
private DataTable GetDataCust()
{
string connc = ConfigurationManager.ConnectionStrings["AMS_POS.Properties.Settings.AMSDataConnectionString"].ConnectionString;
using (OleDbConnection constrb = new OleDbConnection(connc))
{
using (OleDbCommand cmdb = new OleDbCommand("SELECT * FROM Customers", constrb))
{
constrb.Open();
OleDbDataReader reader = cmdb.ExecuteReader();
DTCust.Load(reader);
}
}
return DTCust;
}
private void textBox31_TextChanged(object sender, EventArgs e)
{
DataView dvStock = DTStock.DefaultView;
dvStock.RowFilter = "Part + Description LIKE '%" + textBox31.Text + "%'";
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
textBox1.Text = row.Cells["Part"].Value.ToString();
textBox2.Text = row.Cells["Description"].Value.ToString();
textBox3.Text = row.Cells["Year"].Value.ToString();
textBox8.Text = row.Cells["Application"].Value.ToString();
textBox9.Text = row.Cells["Engine #"].Value.ToString();
textBox4.Text = row.Cells["Alt1"].Value.ToString();
textBox5.Text = row.Cells["Alt2"].Value.ToString();
textBox6.Text = row.Cells["Alt3"].Value.ToString();
textBox7.Text = row.Cells["Alt4"].Value.ToString();
textBox10.Text = row.Cells["Department"].Value.ToString();
textBox11.Text = row.Cells["Cost"].Value.ToString();
textBox12.Text = row.Cells["Cost Incl"].Value.ToString();
textBox13.Text = row.Cells["Markup %"].Value.ToString();
textBox14.Text = row.Cells["Qty Stock"].Value.ToString();
textBox15.Text = row.Cells["Rtl Price Excl"].Value.ToString();
textBox16.Text = row.Cells["Rtl Price Incl"].Value.ToString();
textBox17.Text = row.Cells["SUPPLIER"].Value.ToString();
comboBox1.Text = row.Cells["Discount"].Value.ToString();
textBox19.Text = row.Cells["Green"].Value.ToString();
textBox20.Text = row.Cells["Red"].Value.ToString();
textBox21.Text = row.Cells["Yellow"].Value.ToString();
textBox22.Text = row.Cells["Blue"].Value.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
StockView.Rows.Add(StockView.SelectedRows[0].Index);
stockTableAdapter.Update(AMSDataDataSet.Stock);
}
private void button1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you SURE you want to delete this item?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
if (StockView.SelectedCells != null)
{
StockView.Rows.RemoveAt(StockView.SelectedRows[0].Index);
stockTableAdapter.Update(AMSDataDataSet.Stock);
}
}
}
}
}