Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi dear
my question: how do I delete selected row via DataGridViewButtonColumn in datagridview when datagridview insert data to database?
thanks
a friend in need is a friend indeed.
Posted
Comments
syed shanu 15-Dec-14 2:33am    
Check my article related to your question.
http://www.codeproject.com/Articles/841755/DataGridView-Helper-Class

You can find the Datagridview Button column with click event.You can use that fucntiona and in Datagridview button click event you can get row value.Using the row value delete data from your db and rebind your grid.hope this will help you.
vajihe.mirzaei 15-Dec-14 3:03am    
Thank you. I hope someday you'll be able to take a step to the highest peaks of science.

Hi Friend,


Based on your post, my understanding of your question is that you want to add DataGridViewButtonColumn to DataGridView control and use this column's cell to delete whole row. Here is the code snippet to achieve these actions on DataGridView control. This should satisfy your requirements. If you have any further questions, please feel free to let me know.

VB Code :

Dim buttoncolumn As DataGridViewButtonColumn

buttoncolumn = New DataGridViewButtonColumn()

buttoncolumn.HeaderText = "Sales"

buttoncolumn.Name = "remove"

buttoncolumn.Text = "Remove the rows"

buttoncolumn.UseColumnTextForButtonValue = True

buttoncolumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

buttoncolumn.FlatStyle = FlatStyle.Standard

buttoncolumn.CellTemplate.Style.BackColor = Color.Honeydew

dataGridView1.Columns.Add(buttoncolumn)



Private Sub dataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)

If dataGridView1.Columns(e.ColumnIndex).Name = "remove" AndAlso Me.dataGridView1.Rows(e.RowIndex).IsNewRow = False Then

Me.dataGridView1.EndEdit()

Me.dataGridView1.Rows.RemoveAt(e.RowIndex)

End If

End Sub


Hope this helps.
 
Share this answer
 
Comments
vajihe.mirzaei 15-Dec-14 2:40am    
thanx it's good but it's vb
I convert code (online) and see: An error occured converting your code, probably due to a syntax error: -- line 1 col 1: EOF expected
vajihe.mirzaei 15-Dec-14 2:47am    
Let me see if I understand right algorithm.
Click the cell in the Data Grid View
and dataGridView1.Columns(e.ColumnIndex).Name = "remove" ===> true
Me.dataGridView1.Rows(e.RowIndex).IsNewRow = False ====> unknow?
end edit
vajihe.mirzaei 15-Dec-14 3:17am    
Thank you. I hope someday you'll be able to take a step to the highest peaks of science.
 
Share this answer
 
v2
Comments
vajihe.mirzaei 15-Dec-14 3:03am    
Thank you. I hope someday you'll be able to take a step to the highest peaks of science.
/\jmot 15-Dec-14 3:06am    
what was that?? i really didn't understand. :)
/\jmot 15-Dec-14 3:07am    
is it help you??
vajihe.mirzaei 15-Dec-14 3:22am    
yes thanx
thanx a lot ,I found it with your help


C#
private void dgvPurchase_CellClick(object sender, DataGridViewCellEventArgs e)
       {

           if (dgvPurchase.CurrentCell.ColumnIndex==0 )

           {

               this.dgvPurchase.Rows.RemoveAt(e.RowIndex);

           }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900