locked
insert selected rows from datagridview to sql database RRS feed

  • Question

  • I have a datagridview with a column checkbox, now my problem is how can i insert those checked checkbox into my table in sql database???

     

    and one more thing,how can i remove the addnew thing at the last row of my datagridview? because it represents null.. 

    Friday, August 19, 2011 2:24 AM

Answers

  • Hi kein,

    #  how can i insert those checked checkbox into my table in sql database

    For i As Integer = 0 To Me.DataGridView1.RowCount - 2
       If CBool(Me.DataGridView1.Rows(i).Cells(0).Value) = True Then
        MessageBox.Show(Me.DataGridView1.Rows(i).Cells(1).Value.ToString())
       End If
      Next
    

    You can use above code to insert the row values that a checked checkbox is in.

    # how can i remove the addnew thing at the last row of my datagridview? because it represents null

    In the DataGridView properties (small arrow at the top right corner of the control) uncheck Enable Adding, or in the Properties window at the right side of the screen set AllowUserToAddRows to False.

    # another thing how can i select all with one button??

    For i As Integer = 0 To Me.DataGridView1.RowCount - 2
          Me.DataGridView1.Rows(i).Cells(0).Value = True
    Next  'cells(0) refer to the checkbox cell
    


    Best Regards,


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by kein2930 Monday, August 22, 2011 12:33 PM
    Saturday, August 20, 2011 9:41 AM

All replies

  • by the way, im using vb 2008 and sql server 2005...another thing how can i select all with one button??
    Friday, August 19, 2011 2:28 AM
  • hi

    add a check box filed to your grid view and convert in to template field. in edit template field mode of your grid view change the name of that check box filed to something like "chk_select" , end editing template field. now in code behind you can check all rows and find out if user selected them or not ...

     

    For Each row As GridViewRow In YourGridControlID.Rows
    	Dim CheckboxControl As CheckBox = DirectCast(row.FindControl("chk_select"), CheckBox)
    			' insert to database
    	If CheckboxControl.Checked = True Then
    
    	End If
    Next


     

     

    use the following code to select all rows

     

     For Each row As GridViewRow In YourGridControlID.Rows
    	Dim CheckboxControl As CheckBox = DirectCast(row.FindControl("chk_select"), CheckBox)
    
    	CheckboxControl.Checked = True
    Next



     

     





    Friday, August 19, 2011 12:30 PM
  • If you do it right, than that columnbox is just a value member in your collection set by the  the DataPropertyName

    Here a simple sample on our website

    http://www.vb-tips.com/DataGridViewCombo.aspx


    Success
    Cor
    Friday, August 19, 2011 1:00 PM
  • Pooyan what do you mean by FindControl? because its give an error "it is not a member of the system.windows.forms.DataGridViewRow

     

    im not using asp.net...so what it is in visual basic codes?

     

    thank you for your reply.


    Friday, August 19, 2011 1:28 PM
  • Hi kein,

    #  how can i insert those checked checkbox into my table in sql database

    For i As Integer = 0 To Me.DataGridView1.RowCount - 2
       If CBool(Me.DataGridView1.Rows(i).Cells(0).Value) = True Then
        MessageBox.Show(Me.DataGridView1.Rows(i).Cells(1).Value.ToString())
       End If
      Next
    

    You can use above code to insert the row values that a checked checkbox is in.

    # how can i remove the addnew thing at the last row of my datagridview? because it represents null

    In the DataGridView properties (small arrow at the top right corner of the control) uncheck Enable Adding, or in the Properties window at the right side of the screen set AllowUserToAddRows to False.

    # another thing how can i select all with one button??

    For i As Integer = 0 To Me.DataGridView1.RowCount - 2
          Me.DataGridView1.Rows(i).Cells(0).Value = True
    Next  'cells(0) refer to the checkbox cell
    


    Best Regards,


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by kein2930 Monday, August 22, 2011 12:33 PM
    Saturday, August 20, 2011 9:41 AM
  • Hi kein,

    #  how can i insert those checked checkbox into my table in sql database

    For i As Integer = 0 To Me.DataGridView1.RowCount - 2
      If CBool(Me.DataGridView1.Rows(i).Cells(0).Value) = True Then
      MessageBox.Show(Me.DataGridView1.Rows(i).Cells(1).Value.ToString())
      End If
     Next
    

    You can use above code to insert the row values that a checked checkbox is in.

    # how can i remove the addnew thing at the last row of my datagridview? because it represents null

    In the DataGridView properties (small arrow at the top right corner of the control) uncheck Enable Adding, or in the Properties window at the right side of the screen set AllowUserToAddRows to False.

    # another thing how can i select all with one button??

     

    For i As Integer = 0 To Me.DataGridView1.RowCount - 2
       Me.DataGridView1.Rows(i).Cells(0).Value = True
    Next 'cells(0) refer to the checkbox cell
    


    Best Regards,

     


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    thank you so much kee you answered my problems...but i change some of the codes like the "- 2" to - 1...that's all... i hope you can help with my other problems....thank you so much...i'll just post some of my problem...
    Monday, August 22, 2011 12:36 PM
  • Sure, please feel free to ask in this forum if you have further questions. :-)

     


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 23, 2011 2:41 AM
  • Hi Kee Poppy,

    How to solve same problem using c# in windows form.

    Sunday, December 31, 2017 6:03 PM
  • Hi Kee Poppy,

    How to solve same problem using c# in windows form.

     This forum is for Vb.Net.  If you are using C#,  then you need to ask your own question on the C# forum.  Add a link back to this thread in that question if you think it will help.

    Msdn C# Forum


    If you say it can`t be done then i`ll try it

    Sunday, December 31, 2017 6:39 PM
  • Hi Kee Poppy,

    How to solve same problem using c# in windows form.

    Hello,

    Please see my reply in the following post where I replied this morning to pretty much the same question using C#. In that post the developer wanted to copy the checked rows to another table e.g. original table Customers, other table to copy to CustomersCopy so you would need to modify the INSERT statement in the method InsertRecords. If you want to try the solution I even included scripts to create the database and table (no data, you would have to insert a few rows).

    VS2017 solution for download.

    Lastly, the C# forum is not the best forum for this as this is really for "forms data controls" forum.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, January 2, 2018 1:21 AM