kevin

kevin

  • NA
  • 23
  • 16.3k

C# Database using Datagridview

Oct 31 2014 2:30 AM

I want to delete a record from the datagridview I receive this exception error, Index was out of range. Must be non-negative and less than the size of the collection.

 

here is my code
 

private void Form1_Load(object sender, EventArgs e)

{

// TODO: This line of code loads data into the 'impiDbDataSet.Impi' table. You can move, or remove it, as needed.

//this.impiTableAdapter.Fill(this.impiDbDataSet.Impi);

try

{

string conStrings = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ImpiDb.mdf;Integrated Security=True;Connect Timeout=30";

string sql = "Select * from tblImpi";

con = new SqlConnection(conStrings);

con.Open();

dataAdapter = new SqlDataAdapter(sql, con);

//ImpiDbDataSet impds = new ImpiDbDataSet();

ds = new DataSet();

SqlCommandBuilder cmdb = new SqlCommandBuilder(dataAdapter);

dataAdapter.Fill(ds, "tblImpi");

BindingSource bndS = new BindingSource();

bndS.DataSource = ds.Tables["tblImpi"];

impdg.DataSource = bndS;

// impdg.DataSource = ds.Tables["tblImpi"].DefaultView;

//impdg.DataSource = impds.Tables["tblImpi"].DefaultView;

tootip = new ToolTip();

this.impdg.AlternatingRowsDefaultCellStyle.BackColor = Color.Wheat;

// NavigateRecords();

con.Close();

}

catch (SqlException ex)

{

MessageBox.Show(ex.Message);

}

fontSize();

InstalledFonts();

}

 
my delete button.
 
 
 

private void btnDelete_Click(object sender, EventArgs e)

{

if (MessageBox.Show("I you sure you want to delete this record?","Delete",MessageBoxButtons.YesNo)==DialogResult.Yes)

{

impdg.Rows.RemoveAt(impdg.SelectedRows[0].Index);

dataAdapter.Update(ds,"tblImpi");

}

}

 please assist.

Answers (2)