Forums » .NET » .NET »

Delete selected row from datagridview


Posted Date: 30 Jul 2012      Posted By:: ptina     Member Level: Bronze    Member Rank: 1584     Points: 5   Responses: 5



OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Student\Desktop\Database 4.accdb");
conn.Open();
string vsql = String.Format(" insert into [Memo] values ({0},'{1}','{2}','{3}')", int.Parse(textBox1.Text), textBox2.Text,textBox3.Text,textBox5.Text);
//textBox1 for Hours , textBox2 for Model ,textbox3 for Address
OleDbCommand com = new OleDbCommand(vsql, conn);
com.ExecuteNonQuery();
MessageBox.Show("Data stored successfully");
com.Dispose();
i'm able to save data into the datagrid view as above-mentioned code.

but i'm unable delete any selected row.

hw should i write the code to delete only selected row (i.e. clicking on the row) of the datagridview followed by clicking on the "delete" btn?




Responses

#682423    Author: bulli guruvu setty      Member Level: Gold      Member Rank: 335     Date: 30/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

Code for deleting record from gridview:


protected void gvCurdOperations_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int empId= int.Parse(gvCurdOperations.Rows[e.RowIndex].Cells[1].Text);
SqlConnection con = new SqlConnection("YourConnectionStringDetails");
con.Open();
string st = "delete from [EmployeeDetails] where EmployeeId=" + empId;
sqlcommand cmd = new SqlCommand(st, con);
cmd.ExecuteNonQuery();
con.Close();
BindDataToGrid();//This method is for reloading the grid view data again...
}



public void BindDataToGrid()
{
SqlConnection con = new SqlConnection("YourConnectionStringDetails");
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM [EmployeeDetails]", con);
DataSet ds = new DataSet();
ad.Fill(ds, "Categories");
gvCurdOperations.DataSource = ds;
gvCurdOperations.DataBind();
}


Thanks & Regards
D.Bulli Guruvu Setty



 
#682425    Author: ptina      Member Level: Bronze      Member Rank: 1584     Date: 30/Jul/2012   Rating: 2 out of 52 out of 5     Points: 1

is it possible to modify the code i ve sent for saving data to deleting only selected row?


 
#682433    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 30/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

ptina,

You can delete using below simple code


using System.Data;
using System.Data.OleDb;

namespace DataGridViewOperation
{
public partial class Form1 : Form
{
int rindex;
OleDbConnection con = new OleDbConnection("Connection String");
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataTable dt = new DataTable();
Boolean bind;
public string colindex;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
LoadGrid();
}

void LoadGrid()
{
con.Open();
cmd = new OleDbCommand("select * from emp", con);
da = new OleDbDataAdapter(cmd);
da.Fill(dt);
bind = true;
dataGridView1.DataSource = dt;
con.Close();
//I have set Primary key column value in the DataGridview as Readonly because based on that value we can update record
dataGridView1.Columns[0].ReadOnly = true;
bind = false;
}

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
//store current row index in the variable
rindex = e.RowIndex;
}
//delete using below code for selected row
private void button1_Click(object sender, EventArgs e)
{
con.Open();
string query;
query = "delete from emp where eno='" + dataGridView1.Rows[rindex].Cells[0].Value + "'";
cmd = new OleDbCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Information update Successfully", "Customer Information");
}


}
}


Regards
N.Ravindran
Your Hard work never fails






 
#682438    Author: ptina      Member Level: Bronze      Member Rank: 1584     Date: 30/Jul/2012   Rating: 2 out of 52 out of 5     Points: 1

okay! Thanks a lot ! I'll try n reply tmrw.


 
#682469    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 30/Jul/2012   Rating: 2 out of 52 out of 5     Points: 2

You can try the below sample code for deleting the records


protected void btnDeleteRows_Click(object sender, EventArgs e)
{

foreach (GridViewRow row in GridView1.Rows)
{
CheckBox checkbox = (CheckBox)row.FindControl("chkSelect");

if (checkbox.Checked)
{

int employeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["con"]);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "DELETE FROM EmployeesCopy WHERE Id="+employeeID ;
cmd.Connection = con;
con.Open();
int numberDeleted = cmd.ExecuteNonQuery();

Lable1.Text = numberDeleted.ToString() + " employees were deleted.";

con.Close();
}
}
}


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
Post Reply
You must Sign In to post a response.

Next : Getting Excel cell value with it's format
Previous : Create a 'pop up box' in a widows application form C#
Return to Discussion Forum
Post New Message
Category:

Related Messages
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.