943,148 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1715
  • C# RSS
Mar 18th, 2010
0

Delete Row

Expand Post »
Hey,

I currently have a dataGridView which displays information from my SQL database. I have a button that I use to update/add into the SQL database - for example a user ammends a field in the dataGridView this automatically updates the database.

I now want to implement a button that deletes the selected row from the database.

My dataGridView loadData:

C# Syntax (Toggle Plain Text)
  1. private void LoadData()
  2. {
  3. string connectionString = @"Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False";
  4. conn = new SqlConnection(connectionString);
  5. sql = "SELECT * FROM coffeeType2";
  6.  
  7. da = new SqlDataAdapter(sql, conn);
  8. conn.Open();
  9. ds = new DataSet();
  10. SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);
  11. da.Fill(ds, "coffeeType2");
  12. bsource.DataSource = ds.Tables["coffeeType2"];
  13. dataGridView1.DataSource = bsource;
  14.  
  15. // this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  16.  
  17. dataGridView1.AutoResizeColumns();
  18. dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
  19. }

Add/Update Button:

C# Syntax (Toggle Plain Text)
  1. private void btnAdd_Click(object sender, EventArgs e)
  2. {
  3. DataTable dt = ds.Tables["coffeeType2"];
  4. this.dataGridView1.BindingContext[dt].EndCurrentEdit();
  5. this.da.Update(dt);
  6.  
  7. LoadData();
  8. }

How can I now create a button to delete that particular row from the database?

Thank you.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
dawsonz is offline Offline
49 posts
since Feb 2010
Mar 18th, 2010
0
Re: Delete Row
http://www.codeguru.com/csharp/.net/...cle.php/c13041
http://social.msdn.microsoft.com/for...-0088cfcc1ea3/

thers many posts out there concerning this a quick google search got me this...
Last edited by phoenix911; Mar 18th, 2010 at 7:24 am.
Reputation Points: 38
Solved Threads: 21
Junior Poster
phoenix911 is offline Offline
169 posts
since May 2009
Mar 18th, 2010
0
Re: Delete Row
Ok thank you i'll have a look.

Is there anyway i'm able to get the ID of a select row at all?

Say my datagridview has ID, name, location.

When I click a cell can I get the ID of that particular row?
Reputation Points: 10
Solved Threads: 1
Light Poster
dawsonz is offline Offline
49 posts
since Feb 2010
Mar 18th, 2010
0
Re: Delete Row
there should be a way yes
Reputation Points: 38
Solved Threads: 21
Junior Poster
phoenix911 is offline Offline
169 posts
since May 2009
Mar 18th, 2010
0
Re: Delete Row
Ok, do you have any idea how I would do this?
Reputation Points: 10
Solved Threads: 1
Light Poster
dawsonz is offline Offline
49 posts
since Feb 2010
Mar 19th, 2010
0
Re: Delete Row
Can anyone be kind enough to help me with this?
Reputation Points: 10
Solved Threads: 1
Light Poster
dawsonz is offline Offline
49 posts
since Feb 2010
Mar 19th, 2010
0
Re: Delete Row
Add a column in your grid with a button or an image button as is set on my example:
C# Syntax (Toggle Plain Text)
  1.  
  2. protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
  3. {
  4. ImageButton Button1 = (ImageButton)sender;
  5. GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;
  6. DataSourceSelectArguments args = new DataSourceSelectArguments();
  7. DataView view = (DataView)this.SqlDataSource2.Select(args);
  8. DataTable table = view.ToTable();
  9. string id = table.Rows[grdRow.RowIndex].ItemArray[0].ToString();
  10. }
Hope it helps. let me know if you need a futher explanation. Good luck.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yaninea is offline Offline
7 posts
since May 2008
Mar 24th, 2010
0
Re: Delete Row
Click to Expand / Collapse  Quote originally posted by yaninea ...
Add a column in your grid with a button or an image button as is set on my example:
C# Syntax (Toggle Plain Text)
  1.  
  2. protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
  3. {
  4. ImageButton Button1 = (ImageButton)sender;
  5. GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;
  6. DataSourceSelectArguments args = new DataSourceSelectArguments();
  7. DataView view = (DataView)this.SqlDataSource2.Select(args);
  8. DataTable table = view.ToTable();
  9. string id = table.Rows[grdRow.RowIndex].ItemArray[0].ToString();
  10. }
Hope it helps. let me know if you need a futher explanation. Good luck.
Hi i've added a button onto my datagridivew its on their as column.

I'm having trouble getting the above working however.

I've got a button (btnDelete) am I able to link that up, is there anyway I can simply get the coffeeID from the datagridview row that has been selected?

I have something like this:

C# Syntax (Toggle Plain Text)
  1. DataTable dt = ds.Tables["coffeeType2"];
  2. label1.Text = System.Convert.ToString(dt.Rows[0][2]);

However that just selects the DataTable id second row - how can I do it dependent on what row the user has selected?
Last edited by dawsonz; Mar 24th, 2010 at 11:04 am.
Reputation Points: 10
Solved Threads: 1
Light Poster
dawsonz is offline Offline
49 posts
since Feb 2010
Mar 24th, 2010
0
Re: Delete Row
I've now sorted this

C# Syntax (Toggle Plain Text)
  1. lblCustomerID.Text = dataGridView1.CurrentRow.Cells["customerID"].FormattedValue.ToString();
  2.  
  3. string connectionString = @"Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False";
  4. conn = new SqlConnection(connectionString);
  5. sqlDelete = "DELETE FROM customer WHERE customerID = '" + lblCustomerID.Text + "'";
  6. SqlCommand cmd = new SqlCommand();
  7. cmd.CommandText = sqlDelete;
  8. cmd.Connection = conn;
  9. conn.Open();
  10. cmd.ExecuteNonQuery();
  11. conn.Close();

Thanks for any help anyhow.
Reputation Points: 10
Solved Threads: 1
Light Poster
dawsonz is offline Offline
49 posts
since Feb 2010
Mar 24th, 2010
0
Re: Delete Row
You will now have a gridview with a column that cointains a button "btnDelete".
You need to create an event for the btnDelete_Click and on it add the code I posted.
The code will get the row id from the row where the button was clicked, or well you can actually obtain any value from the datasource related to the grid, you just need to set the value position that you need:
string id = table.Rows[grdRow.RowIndex].ItemArray[0].ToString();
In the explample, the row id is the first value from the query in the SqlDataSource2, meaning, the position 0.
You need to create an event for the btnDelete_Click and on it add the code I posted.
C# Syntax (Toggle Plain Text)
  1. #
  2. protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
  3. #
  4. {
  5. #
  6. ImageButton Button1 = (ImageButton)sender;
  7. #
  8. GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;
  9. #
  10. DataSourceSelectArguments args = new DataSourceSelectArguments();
  11. #
  12. DataView view = (DataView)this.SqlDataSource2.Select(args);
  13. #
  14. DataTable table = view.ToTable();
  15. #
  16. string id = table.Rows[grdRow.RowIndex].ItemArray[0].ToString();
  17. #
  18. string name= table.Rows[grdRow.RowIndex].ItemArray[1].ToString();
  19. }

Let me know how it went, I can send you an app if you need.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yaninea is offline Offline
7 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: subset sum task
Next Thread in C# Forum Timeline: startTime





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC