想要在datagrid中添加多个记录 [英] Want to add more than 1 record in datagrid

查看:89
本文介绍了想要在datagrid中添加多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想使用以下代码在datagrid中添加1条以上的记录,必须进行一些修改.我不能使用for循环,我想我应该添加第一个记录并附加所有其他记录.

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.

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

推荐答案

这里有很多问题:
1)您的函数不返回任何内容.
2)打开一个文件"sam.sdf",该文件不做任何操作.
3)每次您添加新条目时,都会覆盖数据网格的数据源,因此您只会得到一行.
4)您还没有了解到专有名称很重要.
5)您的GUI是一场噩梦!我怎么知道? "Button62"是一个很好的线索!

坦白地说,您似乎在没有试图理解任何代码片段的情况下就将它们随机地组合在一起,并希望它会起作用.

引用专家的话:顺便说一句,在软件中,希望和祈祷不是可行的策略." (Luc Pattyn)
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)


这篇关于想要在datagrid中添加多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆