Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Delete The Data From The Row Of Datagridview

gridview grid datagridview

  • Please log in to reply
5 replies to this topic

#1 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 30 July 2012 - 12:45 AM


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 any selected row (i.e. clicking on the row) of the datagridview followed by clicking on the "delete" btn?
  • 0

#2 techsolnltd

techsolnltd

    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Programming Language:C, C++, C#, (Visual) Basic, Visual Basic .NET
  • Learning:PHP, Python, Perl

Posted 30 July 2012 - 01:24 AM

You would have to use few events of gridview in order to get the selected row and column of the datagridview.
Once you get the selected row and column index then perform the database related activity like row deletion. Update the database and refresh the datagridview then and there only.

Events of Datagridview to be used

1. CurrentCellDirtyChanged
2. CellContentClick
3. CellValueChanged
  • 0

#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 30 July 2012 - 01:34 AM

Learn and apply SQL DELETE statement.
  • 0

#4 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 30 July 2012 - 04:47 AM

Learn and apply SQL DELETE statement.


cn i change insert part to delete? i want to delete only any selected row. how?
  • 0

#5 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 31 July 2012 - 02:44 PM

To DELETE only the selected row, there must something a way to map your selected row into a database row. As example, there may a PRIMARY KEY id column in your database. So when you select a row, you need to get the PRIMARY KEY value for that row and then execute the command
DELETE FROM <table_name> WHERE <primary_key_column> = value_for_primary_key_column_for_that_row;

  • 0

#6 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 02 August 2012 - 11:56 AM

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 any selected row (i.e. clicking on the row) of the datagridview followed by clicking on the "delete" btn?


You have CellDoubleClick event on your dataGridView control. Create event handler for that event so you can parse value from clicked cell (and you must write your parsing code with arrays of columns) to any textual control (the best way is textBox control). After that you can make your query to delete record with value from textBoxes if there is those values in table.
  • 0

Microsoft Student Partner, Microsoft Certified Professional






Also tagged with one or more of these keywords: gridview, grid, datagridview

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download