Results 1 to 12 of 12

Thread: How can add row in DataGrid ?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    34

    Angry How can add row in DataGrid ?

    Hi
    How can add row in DataGrid ?

    Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How can add row in DataGrid ?

    Add a new record to your datasource.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    34

    Re: How can add row in DataGrid ?

    no
    i have 3 text in forn
    text1 , text2, text3
    How can add row in DataGrid ?
    the data row from text do not save in databases only in form

  4. #4
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Indiana
    Posts
    295

    Re: How can add row in DataGrid ?

    Why are you using a Datagrid? If your data is coming from text boxes, why not just use the MSFlexGrid control?

    To increase by one row in a Flesgrid the code would be...

    Code:
    MSFlexgrid1.rows = MSFlexgrid1.rows + 1

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    34

    Re: How can add row in DataGrid ?

    hi
    i use datagrid not MSFlexgrid
    can i add row in DataGrid from text ?

    datagrid only not MSFlexgrid
    please help me

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    34

    Re: How can add row in DataGrid ?

    can that or no ?
    please answer me

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: How can add row in DataGrid ?

    The DataGrid is bound to a recordset, as such in order for it to Add new rows you will need to add records to your database which will be reflected in the DataGrid.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: How can add row in DataGrid ?

    The DataGrid requires an OLEDB Datasource such as an ADO Recordset. As Hack & dee-u pointed out, to add a new row to the DataGrid simply add a record to it's underlying Recordset.

    You haven't indicated your requirements other than you don't want to use a database. Good news you don't need one. You can create and use an in-memory ADO Recordset. Add a Reference to the Microsoft ActiveX Data Object 2.X Library to your project.

    Code:
    Private Sub Form_Load()
        Dim rs As ADODB.Recordset
      
        'Don't allow user to modify data in grid directly. 
        With DataGrid1
            .AllowAddNew = False
            .AllowDelete = False
            .AllowUpdate = False
        End With
        
        'create an in-memory recordset with your required fields
        Set rs = New ADODB.Recordset
    
        rs.Fields.Append "LastName", adVarChar, 30
        rs.Fields.Append "FirstName", adVarChar, 15
        
        rs.Open
    
        'bind recordset to Grid
        Set DataGrid1.DataSource = rs
       
        
    End Sub
    
    Private Sub cmdAdd_Click()
        Dim rs As ADODB.Recordset
    
        'get the Grid's Recordset and add a new Record
        Set rs = DataGrid1.DataSource
        
        rs.AddNew Array("LastName", "FirstName"), Array(txtLastName.Text, txtFirstName.Text)
        
        'you could use this syntax instead
        'rs.AddNew
        'rs.Fields("LastName").Value = txtLastName.text
        'rs.Fields("FirstName").Value = txtFirstName.text
    
    
        'ReBind the Grid so the new record is visible
        DataGrid1.ReBind
    
    End Sub
    Note that when the program is closed the data is lost. But it is simple enough to create a Save procedure to write the data to a text file or whatever your needs might be.

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    34

    Re: How can add row in DataGrid ?

    thanks for soultion my Error

  10. #10
    Junior Member
    Join Date
    Apr 2020
    Posts
    29

    Re: How can add row in DataGrid ?

    Quote Originally Posted by brucevde View Post
    The DataGrid requires an OLEDB Datasource such as an ADO Recordset. As Hack & dee-u pointed out, to add a new row to the DataGrid simply add a record to it's underlying Recordset.

    You haven't indicated your requirements other than you don't want to use a database. Good news you don't need one. You can create and use an in-memory ADO Recordset. Add a Reference to the Microsoft ActiveX Data Object 2.X Library to your project.

    Code:
    Private Sub Form_Load()
        Dim rs As ADODB.Recordset
      
        'Don't allow user to modify data in grid directly. 
        With DataGrid1
            .AllowAddNew = False
            .AllowDelete = False
            .AllowUpdate = False
        End With
        
        'create an in-memory recordset with your required fields
        Set rs = New ADODB.Recordset
    
        rs.Fields.Append "LastName", adVarChar, 30
        rs.Fields.Append "FirstName", adVarChar, 15
        
        rs.Open
    
        'bind recordset to Grid
        Set DataGrid1.DataSource = rs
       
        
    End Sub
    
    Private Sub cmdAdd_Click()
        Dim rs As ADODB.Recordset
    
        'get the Grid's Recordset and add a new Record
        Set rs = DataGrid1.DataSource
        
        rs.AddNew Array("LastName", "FirstName"), Array(txtLastName.Text, txtFirstName.Text)
        
        'you could use this syntax instead
        'rs.AddNew
        'rs.Fields("LastName").Value = txtLastName.text
        'rs.Fields("FirstName").Value = txtFirstName.text
    
    
        'ReBind the Grid so the new record is visible
        DataGrid1.ReBind
    
    End Sub
    Note that when the program is closed the data is lost. But it is simple enough to create a Save procedure to write the data to a text file or whatever your needs might be.
    brucevde .. Yours is an example of a high-quality answer; most solutions on the internet are either half-mindedly put, too complicated, or are meant for off-showing purposes.

    Thank you very much; you solved my problem.

  11. #11
    Junior Member
    Join Date
    Apr 2020
    Posts
    29

    Re: How can add row in DataGrid ?

    How to make it save data into the actual database instead of having it save into an in-memory recordset?

  12. #12
    Junior Member
    Join Date
    Apr 2020
    Posts
    29

    Re: How can add row in DataGrid ?

    Since it is an in-memory recordset, is there a way to save its data for next program session?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width