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 CornerRank #1,221 · 1.5K reputationPosted Apr 28, 2011, 2:36 AM EDT
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();
}
}
CrishRank #1,486 · 1.1K reputationPosted Apr 26, 2011, 3:18 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Apr 26, 2011, 3:19 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Apr 26, 2011, 3:24 AM EDT
Dinesh BeniwalRank #9 · 64K reputationPosted Apr 26, 2011, 3:25 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Apr 26, 2011, 5:17 AM EDT
Sahil Jani196 reputationPosted Apr 27, 2011, 8:02 PM EDT
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.
Soft CornerRank #1,221 · 1.5K reputationPosted Apr 27, 2011, 10:39 PM EDT
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.
omkar mhaiskar84 reputationPosted Apr 27, 2011, 10:51 PM EDT
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.
Sahil Jani196 reputationPosted Apr 28, 2011, 12:55 AM EDT
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();
}
Soft CornerRank #1,221 · 1.5K reputationPosted Apr 28, 2011, 1:07 AM EDT
Check attachment now, data Binding in attached code is done by using SqlConnection.
Sahil Jani196 reputationPosted Apr 28, 2011, 2:16 AM EDT
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();
}
}
}
Sahil Jani196 reputationPosted Apr 28, 2011, 2:58 AM EDT
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.