如何在Datagridview中添加项目而不删除VB.NET中的先前获取的数据 [英] how to add items in Datagridview without removing the Previous fetched Data in VB.NET

查看:63
本文介绍了如何在Datagridview中添加项目而不删除VB.NET中的先前获取的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单应用程序中有一个DataGridview和一个文本框.
用户在文本框中输入ID,然后输入.
根据ID数据正在DataGridview中填充.
让这里总共有5个项目被填充到Datagridview中.
下一步在这里,如果用户再次在文本框中输入另一个ID,我希望得到它.
我希望获取的数据应仅填充到先前的数据下方.
我的意思是说,新数据应该填充在Datagridview之前获取的那5项下面.

请提出一些想法.
需要帮助.

I have a DataGridview and a text box in form application.
user is entering ID in the text box and enter.
according to ID data is getting filled in the DataGridview.
Let here total 5 items are getting filled in the Datagridview .
Next Here I want that If user Again entering another ID in the Textbox.
I want that the fetched data should just get filled below the previous Data.
I mean to say that new data should get filled below those 5 items fetched earlier in the Datagridview.

Please suggest some Ideas .
Help needed.

推荐答案

设置了datagridview的DataSource属性后,如果分配了源,它将覆盖以前的数据源.这就是为什么您只获得最近请求的那些记录的原因.
我建议用两种方法修复它:

1)每次以字符串"(逗号分隔)或列表(整数)"的形式收集所有ID,并在每次从数据库中获取数据时传递这些ID.使用这种方式,可以使用数据源"方式.

2)请勿设置DataGridView的DataSource属性,而是在DataTable/DataSet的所有行(已设置为DataSource属性)上进行迭代,并使用
DataGridView.Rows.Add()方法是这样的:

这段代码可以为您提供有关如何完成任务的想法:(未经测试)

Once you set DataSource property of datagridview it overrides the previously datasource if source assigned. That is why you are getting only those records which were recently requested.

I would suggest it to fix with two ways:

1) Collect all IDs in either a "string" (comma separated) or "List(of Integer)" and pass these Ids every time you fetch data from database. Using this way "DataSource" way would work.

2) Don''t set DataSource property of DataGridView instead, iterate on all rows of DataTable/DataSet (which were set to DataSource property) and use
DataGridView.Rows.Add() methods something like this:

This piece of code can provide you an idea on how to accomplish task:(untested)

Dim dt as DataTable = RetrieveDataTable()   ' Gets records in DataTable according to Ids user entered uptil now
For Each row as DataRow in dt.Rows
   DataGridView1.Rows.Add(new Object() { row(0), row(1),....}) '' access elements from "dt" and add in the datagridview's row
Next



希望对您有所帮助!



Hope it helps!


这篇关于如何在Datagridview中添加项目而不删除VB.NET中的先前获取的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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