Results 1 to 15 of 15

Thread: datagrid

  1. #1

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    datagrid

    Hi,

    I have a datagrid view, textboes and a dataset.
    I would like the data i have in the datagridview and textboxes to be saved in my datasset.
    Can you please show me how i can do that

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagrid

    Just bind your DataTable to you controls and you're done. Any changes you make to one will be automatically pushed to the other:
    vb.net Code:
    1. Me.BindingSource1.DataSource = myDataSet
    2. Me.BindingSource1.DataMember = "MyTable"
    3.  
    4. Me.DataGridView1.DataSource = Me.BindingSource1
    5.  
    6. Me.TextBox1.DataBindings.Add("Text", Me.BindingSource1, "MyColumn")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: datagrid

    Hi,

    Thanks for quick reply. Can you pleas ecplain me in detail the code here

    #
    Me.BindingSource1.DataSource = myDataSet
    #
    Me.BindingSource1.DataMember = "MyTable"
    Thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagrid

    The DataSource is the source of the data. The DataMember is the member of that data source that contains the data you want to bind. I suggest that you open your MSDN Library and read the documentation for the BindingSource class and fro those properties in particular. I recommend that you do that every time you have a question, then post if you can't find what you need or don't understand what you find. You don't need us to provide information that you already have.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: datagrid

    ok, well i made my code like this, i was able to show that on the boes in the report, but not the datagrid view.

    vb Code:
    1. Dim dt As DataTable
    2.         Dim dr As DataRow
    3.  
    4.         Me.BindingSource1.DataMember = "dt"
    5.         Me.BindingSource1.DataSource = DataSet2
    6.  
    7.         dt = New DataTable("tableReport")
    8.         dt.Columns.Add(New DataColumn("nom"))
    9.         dt.Columns.Add(New DataColumn("prenom"))
    10.         dt.Columns.Add(New DataColumn("datedenaissance"))
    11.  
    12.         dr = dt.NewRow
    13.  
    14.         dr("nom") = loginid.Text
    15.         dr("prenom") = ComboBox1.Text
    16.         dr("datedenaissance") = TextBox2.Text
    17.  
    18.         dt.Rows.Add(dr)
    19.         Dim report1 As New rptreport
    20.         report1.SetDataSource(dt)
    21.         Form1.CrystalReportViewer1.ReportSource = report1
    22.         Form1.Show()

    Me.BindingSource1.DataSource = DataSet2
    here i get an error 'DataSet2' is a type and cannot be used as an expression.
    In the data set i have a datatable named dt and three column.

    What could be the problem?

    thx

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagrid

    The problem could be that you have to bind an instance of the DataSet2 class, not the DataSet2 class itself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: datagrid

    ok, i did it as u said now no code error, but i don't see my datagrid data in my dataset or report, what else do i have to make to make it appear in my report or dataset?

    Thanks

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagrid

    Have you retrieved any data from the database? It doesn't happen on its own. You have to provide code to get the data.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: datagrid

    Yes i filled the datagrid then put the code u gave me. I think the code u gave is to retrive data from dataset to datagrid. i want from datagrid to dataset

    thx

  10. #10

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: datagrid

    i tried to write the code like this
    vb Code:
    1. Me.BindingSource1.DataSource = dataGridEleveMoyen.DataSource
    2.         DataSet3 = BindingSource1.DataSource

    but i'm getting this error Unable to cast object of type 'System.Data.DataView' to type 'EleveMoyen.DataSet3'.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagrid

    You're doing this all wrong. You don't have to do anything to get the data from the grid to the DataSet. That's the whole point of data-binding. You populate a DataTable and bind that to the grid. Now, any changes you make to the grid will automatically affect the DataTable because they are bound. So:

    1. Populate a DataTable.
    2. Bind the table to a BindingSource.
    3. Bind the source to the grid.

    That's it. That is all you have to do. The user makes their edits in the grid and then you save the changes in the DataTable back to the database. That is ALL you need to do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: datagrid

    Hi,

    i guess its here where i am blocked.

    2. Bind the table to a BindingSource.
    3. Bind the source to the grid.

    how do i do that?

    thx

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagrid

    Go back and read post #2. An alternative to this:
    vb.net Code:
    1. Me.BindingSource1.DataSource = myDataSet
    2. Me.BindingSource1.DataMember = "MyTable"
    is this:
    vb.net Code:
    1. Me.BindingSource1.DataSource = myDataSet.Tables("MyTable")
    or, if you're using a typed DataSet, this:
    vb.net Code:
    1. Me.BindingSource1.DataSource = myDataSet.MyTable
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: datagrid

    Hi,

    I did it like that but nothing appearing in the report.
    vb Code:
    1. Dim dt As DataTable
    2.         Dim dr As DataRow
    3.  
    4.  
    5.         Me.BindingSource1.DataSource = DataSet3
    6.         Me.BindingSource1.DataMember = "moyenn"
    7.         Me.BindingSource1.DataSource = DataSet3.Tables("moyenn")
    8.  
    9.  
    10.  
    11.  
    12.         '   Me.BindingSource1.DataMember = "EleveMoyen"
    13.         '  DataSet3 = dsResult.Tables("EleveMoyen")
    14.  
    15.  
    16.         dt = New DataTable("tableReport")
    17.         dt.Columns.Add(New DataColumn("nom"))
    18.         dt.Columns.Add(New DataColumn("prenom"))
    19.         dt.Columns.Add(New DataColumn("datedenaissance"))
    20.  
    21.         dr = dt.NewRow
    22.  
    23.         dr("nom") = loginid.Text
    24.         dr("prenom") = ComboBox1.Text
    25.         dr("datedenaissance") = TextBox2.Text
    26.  
    27.         dt.Rows.Add(dr)
    28.         Dim report1 As New rptreport
    29.         report1.SetDataSource(dt)
    30.         Form1.CrystalReportViewer1.ReportSource = report1
    31.         Form1.Show()

    thanks

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagrid

    1. Why are you binding the same data to the BindingSource twice?
    2. Why are you binding the data to the BindingSource at all because the BindingSource isn't then used again in that code.
    3. Why are you creating a new DataTable to use for the report when you already have a DataTable in the DataSet?

    You seem to have parts of two completely unrelated sections of code there so i don't know what's supposed to be happening. Also, I can't really help you with CR as I don't use it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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