如何从TextBox中提取数据到GridView(要在GridView中显示多行)? [英] How to extract data from a TextBox to a GridView (multiple rows to be displayed in the GridView)?

查看:86
本文介绍了如何从TextBox中提取数据到GridView(要在GridView中显示多行)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将文本框和组合框中的数据显示到网格视图,而不将数据保存到数据库。当我在文本框中输入数据,然后单击按钮时,这些值将显示在网格视图中。它按预期工作,没有任何错误。

但是我想调整一下代码,以便Grid View能够通过保留Grid数据视图中的旧数据来从Text框中添加新数据(多行应该是显示在网格视图而不是单行中。



这是一个桌面应用程序,我正在使用视觉基础和visual studio 2012.我的代码到目前为止:< br $> b $ b

 公共  Sub  DataTabel_TblDetali()

Dim TblDetali As DataTable()

' Se kreira dataTabel so oderdeniKoloni
TblDetali.Columns.Add( BrojBaranje GetType String ))
TblDetali.Columns.Add( RedBroj GetType 整数))
TblDetali.Columns.Add( Apoen GetType Decimal ))
TblDetali.Columns.Add( Evidenciski_Broj GetType String ))
TblDetali.Columns.Add( InventarenBroj GetType String ))
TblDetali.Columns.Add( OznakaSerija GetType String ))
TblDetali.Columns.Add( VidSeriskiBroj GetType String ))
TblDetali.Columns.Add( GodinaIzdavanje GetType String ))
TblDetali.Columns.Add( BrojParcinja GetType 整数))
TblDetali.Columns.Add( SeriskiBroj1 GetType String ))
TblDetali.Columns.Add( SeriskiBroj2 GetType Stri ng ))
TblDetali.Columns.Add( BrojIndikativ,< span class =code-keyword> GetType ( String ))
TblDetali.Columns.Add( TipKlasa GetType 字符串))
TblDetali.Columns.Add( TipReprodukcija,< span class =code-keyword> GetType ( String ))
TblDetali.Columns.Add( BrojNalog GetType 字符串))



Dim RedenBroj As 整数 = 0
TblDetali.Rows.Add(BrojBaranje,RedenBroj + 1 ,Apoen,EvidenciskiBroj,InventarenBroj,OznakaZaSerija,VidSeriskibroj,GodinaIzdavanje,BrojParcinja ,SeriskiBroj1,SeriskiBroj2,BrojIndikativ,TipKlasa,TipReprodukcija,

grdDetaliVnos.DataSource = TblDetali
RedenBroj = RedenBroj + 1

解决方案

< blockquote>嘿那里,



您可以在会话中保存 DataTable

这样当你想添加另一行时,只需从<$获取 DataTable c $ c> Session
,向表中添加一行,将其绑定到GridView并将其保存回相同的会话



这里:

使用此声明:

 如果会话(  MyTable IsNot   Nothing  然后 
TblDetali = ctype (会话( MyTable),DataTable)结束 如果



之后:

  Dim  TblDetali  As   DataTable()





这一行:

会话(  MyTable)= TblDetali 

 grdDetaliVnos.DataSource = TblDetali 







希望有所帮助。



Azee


I have used the following code to display data from Text Boxes and combo boxes to a Grid View without saving the data to the database. When I enter the data in the Text Boxes, and click on the Button, those values will be displayed in the Grid view. It is working as expected without any errors.
But I want to tweak the code a bit so that the Grid View should be able to add new data from the Text boxes by retaining the old data as it is in the Grid view (multiple rows should be displayed in the Grid view instead of single rows).

It is an desktop application, I'm using visual basics and visual studio 2012. My code so far:

Public Sub DataTabel_TblDetali()

        Dim TblDetali As New DataTable()

        'Se kreira dataTabel so oderdeniKoloni
        TblDetali.Columns.Add("BrojBaranje", GetType(String))
        TblDetali.Columns.Add("RedBroj", GetType(Integer))
        TblDetali.Columns.Add("Apoen", GetType(Decimal))
        TblDetali.Columns.Add("Evidenciski_Broj", GetType(String))
        TblDetali.Columns.Add("InventarenBroj", GetType(String))
        TblDetali.Columns.Add("OznakaSerija", GetType(String))
        TblDetali.Columns.Add("VidSeriskiBroj", GetType(String))
        TblDetali.Columns.Add("GodinaIzdavanje", GetType(String))
        TblDetali.Columns.Add("BrojParcinja", GetType(Integer))
        TblDetali.Columns.Add("SeriskiBroj1", GetType(String))
        TblDetali.Columns.Add("SeriskiBroj2", GetType(String))
        TblDetali.Columns.Add("BrojIndikativ", GetType(String))
        TblDetali.Columns.Add("TipKlasa", GetType(String))
        TblDetali.Columns.Add("TipReprodukcija", GetType(String))
        TblDetali.Columns.Add("BrojNalog", GetType(String))



        Dim RedenBroj As Integer = 0
        TblDetali.Rows.Add(BrojBaranje, RedenBroj + 1, Apoen, EvidenciskiBroj, InventarenBroj, OznakaZaSerija, VidSeriskibroj, GodinaIzdavanje, BrojParcinja, SeriskiBroj1, SeriskiBroj2, BrojIndikativ, TipKlasa, TipReprodukcija, "")

        grdDetaliVnos.DataSource = TblDetali
        RedenBroj = RedenBroj + 1

解决方案

Hey there,

You could save the DataTable in the Session after adding row to it.
This way when you want to add another row, just get the DataTable back from Session, add a row to the table, bind it to the GridView and save it back to the same Session

Here:
Use this statement:

If Session("MyTable") IsNot Nothing Then
TblDetali = ctype(Session("MyTable"), DataTable)  End If


after:

Dim TblDetali As New DataTable()


and
this line:

Session("MyTable") = TblDetali 

before this line:

grdDetaliVnos.DataSource = TblDetali




Hope it helps.

Azee


这篇关于如何从TextBox中提取数据到GridView(要在GridView中显示多行)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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