Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Forums » .NET » .NET »
delete record from gridview and database
Posted Date: 19 Nov 2008 Posted By: muhammadfirdaus Member Level: Bronze Points: 1
Responses:
3
|
hi im new to c#. currently i have this problem of deleting record from the gridview as well as from the dB.when i select the row in the gridview,the record display on my textbox will be the 1st record in the dB and not the selected row from the gridview.and if i use dataGridView1.CurrentRow.Index,the 1st row in the gridview will be zero follow by 1,2,3 and so on.. Im totally confused and really need help!
Here are code:
private void btnDelete_Click(object sender, EventArgs e) {
/* if (dataGridView1.CurrentRow.Index <= -1) { return; }
int Id = int.Parse(dataGridView1[dataGridView1.CurrentRow.Index, 0].ToString()); // AddressData st = AddressDataManager.GetAddressData(Id); if (st == null) { return; }
if (MessageBox.Show("Are you sure to delete the selected item ?", "AddressBook", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) { // st.Status = eStatus.Deleted; // AddressDataManager.UpdateAddressData(st); } else { return; } */ // dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
/* con = new System.Data.OleDb.OleDbConnection(); con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:/projectDb.mdb"; string sql = "DELETE FROM Table1 WHERE command = '" + textBox5.Text + "'"; OleDbCommand myCmd = new OleDbCommand(sql, con); try { DataSet dset = new DataSet(); da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
con.Open();
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
// da.Fill(dset, "Table1");
//da.Update(dset, "Table1"); // dataGridView1.DataSource = dset.Tables["Table1"]; da.Fill(dset, "Table1"); // myCmd.ExecuteNonQuery(); textBox4.Text = ""; textBox5.Text = ""; //dataGridView1.Refresh(); string sql2 = "SELECT * FROM Table1 "; da = new System.Data.OleDb.OleDbDataAdapter(sql2, con); DataSet dset2 = new DataSet(); //da.TableMappings.Add("Table1", "command"); //DataViewManager dviewmanager = new DataViewManager(); //dviewmanager = dset.DefaultViewManager; //dataGridView1.DataSource = table1BindingSource; da.Fill(dset2); //myCmd.ExecuteNonQuery(); dataGridView1.DataSource = dset2.Tables["Table1"].DefaultView;
} catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } finally {
con.Close(); } */ con = new System.Data.OleDb.OleDbConnection(); con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:/projectDb.mdb"; string sql = "DELETE FROM Table1 WHERE command = '" + textBox5.Text + "'"; OleDbCommand myCmd = new OleDbCommand(sql, con); DataSet dset = new DataSet(); da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
con.Open(); // DataTable myMapping = new DataTable("Table1"); // da.TableMappings.Add(myMapping); foreach (DataGridViewRow row in dataGridView1.SelectedRows) { if (row.Index != dataGridView1.Rows.Count+3) { dataGridView1.Rows.RemoveAt(row.Index); } } // da.Update(myMapping); da.Fill(dset, "Table1");
string sql2 = "SELECT * FROM Table1 "; da2 = new System.Data.OleDb.OleDbDataAdapter(sql2, con); //da.TableMappings.Add("Table1", "command"); da2.Fill(dset, "Table1"); //DataViewManager dviewmanager = new DataViewManager(); //dviewmanager = dset.DefaultViewManager; //dataGridView1.DataSource = table1BindingSource; //da.Fill(ds1); dataGridView1.DataSource = dset.Tables["Table1"].DefaultView;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
con = new System.Data.OleDb.OleDbConnection(); con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:/projectDb.mdb"; con.Open(); string sql = "SELECT * FROM Table1 where command = '" + dataGridView1.CurrentRow.Index +"'"; OleDbCommand command = new OleDbCommand(sql,con); OleDbDataReader dr = command.ExecuteReader();
DataSet dset = new DataSet(); da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
// DataGridViewRow row = new DataGridViewRow(); // dataGridView1.SelectedRows.Contains(row ); da.Fill(dset, "Table1"); //dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index); // if (row.Index != dataGridView1.Rows.Count) if (dr.Read())
{
textBox4.Text = dr["class"].ToString(); textBox5.Text = dr["command"].ToString(); } // dataGridView1.DataSource = dset.Tables["Table1"].DefaultView; // dataGridView1.DataSource = dr; dr.Close(); con.Close();
}
hope you guys can help me.. =(
|
Responses
|
| Author: Anil Kumar Pandey 19 Nov 2008 | Member Level: Diamond | Rating:  Points: 4 | hi,
U can use Simply this Event after the Delete option in the Grid View..
protected void grdDetails_RowDeleting(object sender, GridViewDeleteEventArgs e) { grdDetails.EditIndex = e.RowIndex; Bind(); }
Regards Anil Pandey
Thanks & Regards Anil Kumar Pandey
| | Author: muhammadfirdaus 19 Nov 2008 | Member Level: Bronze | Rating:  Points: 3 | erm i should type in the event(row_deleting) or double click on somewhere?
and im using c# window application.. and i see alot of design codes around in the net..does the codes apply to my case?
|
| Post Reply |
|
|
|
 | | This thread is locked for new responses. Please post your comments and questions as a separate thread. If required, refer to the URL of this page in your new post. |
|
|
|
|