Hi,
I have attched two file over here.
In 1st file I have put the screen shot of my original datagrid view and selected coloumn which I want to delete.
In 2nd file i have put the screen shot in that I am getting Exception. and the code is also visible over there.
I am getting exception in rowDelete() function.
This application is Windows Application and I am using C# as a language and MS Access as a Database.
Help me to resolve that exception.
Loading

Soft CornerPosted Apr 28, 2011, 6:36 AM
As per your requirement , it can be done using CellClick event of datagridview. but in your code u haven't implemented that, so check my code again and for deleting row dynamically code will look like this:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
DeleteRow();
}
private void DeleteRow()
{
try
{
if (id != -1)
{
MyConnection.Open();
OleDbCommand cmd = new OleDbCommand("DELETE from Locations where Sr_Num=" + id, MyConnection);
cmd.ExecuteNonQuery();
MyConnection.Close();
BindData();
}
else
{
MessageBox.Show("You haven't Selected any Row");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MyConnection.Close();
}
}
Sahil JaniPosted Apr 28, 2011, 6:58 AM
Thank you so much man. Problem is resolved.
If any one wants the correct code then they can download the file I have attached here.
Sahil JaniPosted Apr 28, 2011, 6:16 AM
This code was very help ful to me. Will you please tell me that If I want to make it dynamically then how should it possible...?
Dynamically means, when I will select any row of the datagrid then that row should be deleted. Example :: If I will select row no. 3 then that 3no. row will be deleted.
Here I have put my code which is working statically but not dynamically... :::
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;
using System.Data.OleDb;
namespace WindowsFormsApplication1.Forms
{
public partial class Location_Entry_Frm : Form
{
private System.Data.DataTable Locations;
OleDbConnection MyConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\summary.mdb");
DataSet ds1;
OleDbDataAdapter da;
int id = -1;
public Location_Entry_Frm()
{
InitializeComponent();
}
private void Location_Entry_Frm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'summaryDataSet.Locations' table. You can move, or remove it, as needed.
//this.locationsTableAdapter.Fill(this.summaryDataSet.Locations);
}
private void btn_View_Click(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
MyConnection.Open();
da = new OleDbDataAdapter("SELECT * FROM Locations",MyConnection);
ds1 = new DataSet();
da.Fill(ds1,"Locations");
dgv_Locations.DataSource = ds1.Tables[0];
MyConnection.Close();
id = 1;
}
private void btn_Delete_Click(object sender, EventArgs e)
{
rowDelete();
}
private void rowDelete()
{
if (id == 1)
{
MyConnection.Open();
OleDbCommand cmd = new OleDbCommand("DELETE from Locations where Sr_Num=" + id, MyConnection);
cmd.ExecuteNonQuery();
MyConnection.Close();
BindData();
}
else
{
MessageBox.Show("You haven't selected any row");
}
}
private void btn_Close_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Soft CornerPosted Apr 28, 2011, 5:07 AM
Check attachment now, data Binding in attached code is done by using SqlConnection.
Sahil JaniPosted Apr 28, 2011, 4:55 AM
I am not able to open .rar file. So, will you pls send me .zip file?
Actully, that code example you have written that I am not able to understand and cop-up with that.
Hi Omkar,
This is my code for binding the data :::
private void btn_View_Click(object sender, EventArgs e)
{
OleDbConnection MyConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\summary.mdb");
//conn = new OleDbConnection(MyConnection);
String str2 = "SELECT Locations FROM Locations";
//Sr_Num,
OleDbDataAdapter da = new OleDbDataAdapter(str2, MyConnection);
MyConnection.Open();
DataSet ds1 = new DataSet();
OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(da);
da.Fill(ds1, "others");
BindingSource bsource = new BindingSource();
bsource.DataSource = ds1.Tables["others"];
dgv_Locations.DataSource = bsource;
MyConnection.Close();
}
omkar mhaiskarPosted Apr 28, 2011, 2:51 AM
I want to know how did u bind data to the datagrid.
also when you passing textbox's value to delete command on that this is not integer value.
you need to trace have you provided id to the grid or not.
Soft CornerPosted Apr 28, 2011, 2:39 AM
looking at your code you will not get the selected row index , for this implement CellClick event for datagridview.
and then while clicking on any row you can store it selected index :
int idx = -1;
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
idx = e.RowIndex;
}
might be this will help you otherwise i have another example, check the attachment.
Sahil JaniPosted Apr 28, 2011, 12:02 AM
Sorry for the late reply. I was on leave yesterday so.
By the way that Sr. No I am taking from Database using query.
And I have put the breakpoint on yellow line and I am getting value 0.
Suthish NairPosted Apr 26, 2011, 9:17 AM
Dinesh BeniwalPosted Apr 26, 2011, 7:25 AM
Suthish NairPosted Apr 26, 2011, 7:24 AM
Suthish NairPosted Apr 26, 2011, 7:19 AM
CrishPosted Apr 26, 2011, 7:18 AM