Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I want to add more than 1 record in datagrid using the below code, some modifications has to be done. I cant use for loop, i think i should add the 1st record and append all the other records.

VB
Public Function FillDataGrid()
        Dim Table1 As DataTable
        Dim Dt As DataSet
        Dim dr As DataRow
        Dim file As New System.IO.StreamReader("Data Source=\Application\sam.sdf")
        Table1 = New DataTable("Medicines")
        Dim SrNo As DataColumn = New DataColumn("Sr.No")
        SrNo.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(SrNo)
        Dim medname As DataColumn = New DataColumn("Medicine Name")
        medname.DataType = System.Type.GetType("System.String")
        medname.MaxLength = 500
        Table1.Columns.Add(medname)
        Dim dosage As DataColumn = New DataColumn("Dosage")
        dosage.DataType = System.Type.GetType("System.String")
        dosage.MaxLength = 500
        Table1.Columns.Add(dosage)
        Dim freq As DataColumn = New DataColumn("Frequency")
        freq.DataType = System.Type.GetType("System.String")
        freq.MaxLength = 500
        Table1.Columns.Add(freq)
        
        dr = Table1.NewRow()
        dr(0) = txtmed.Text
        dr(1) = Me.txtdosage.Text
        dr(2) = txtfreq.Text
        Table1.Rows.Add(dr)
               
        Dt = New DataSet
        Dt.Tables.Add(Table1)
        DataGrid1.DataSource = Dt.Tables("Medicines")
        txtmed.Text = ""
        txtdosage.Text = ""
        txtfreq.Text = ""
    End Function

    Private Sub Button62_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Call FillDataGrid()
    End Sub



Can anyone help me

Thanks in advance
Posted

1 solution

There are a number of problems here:
1) Your function does not return anything.
2) You open a file "sam.sdf" which you do nothing with.
3) You overwrite the datasource for the datagrid each time you put a new entry in, so you will only ever get a single row.
4) You haven't learnt that proper names are important.
5) Your GUI is a nightmare! How can I tell? "Button62" is a very good clue!

To be honest, it looks like you have thrown together code fragments at random without trying to understand any of it, and hoped like hell it would work.

To quote an expert: "BTW, in software, hope and pray is not a viable strategy." (Luc Pattyn)
 
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