Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 7-Nov-12 0:40am
v2
Comments
Shahan Ayyub 7-Nov-12 6:13am    
How do you do :
>>according to ID data is getting filled in the DataGridview.
I mean to say how do you populate data in row ? Have you tried to add a new row in grid and target it to populate with data ?
Karwa_Vivek 7-Nov-12 6:31am    
i am setting the Datasource to the datagridview.
faisal23 7-Nov-12 7:02am    
You are setting datasource to the gridview, so on form load it is binded. When you enter id from text box so you have to use concate the existing grid values
Karwa_Vivek 7-Nov-12 7:03am    
Can you explain this a little Bit
faisal23 7-Nov-12 7:04am    
use gv.items.add() function

1 solution

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)

VB
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!
 
Share this answer
 
v4
Comments
Karwa_Vivek 7-Nov-12 7:16am    
I think you can help me.
Please explain your second way more.
Shahan Ayyub 7-Nov-12 8:05am    
I assumed "RetrieveDataTable" is a method that will fetch data from database according to the id user entered. Say for example user enter id = 1. this method will retrieve all info related to id=1 in datatable using SqlDataAdapter.Fill method. In next step you can iterate using for loop on all rows exists in DataTable and call DataGridView1.Rows.Add() method to append these row at the end of datagridview. Is it clear now ? In case no please provide details about the issue which is still unclear.
Karwa_Vivek 7-Nov-12 8:30am    
I want to clear my query More .
Let for Example user enter id =1 and Fetched the related Data in datagridview.
Now next user enter id =2 .here I just want that the related data with id 2. will get filled just below the related data with id 1 previously user has entered.
I have understood the "RetriveDataTable" method.which will fetch the data related to ID =1.. but what to do when user enter ID =2 and again press enter.Here the Requirement is that Datagridview should not get clear only the data related to ID =2 get filled below it.
Shahan Ayyub 7-Nov-12 8:37am    
If you noticed my updated solution you will see i have not cleared Rows of grid. so if "RetrieveDataTable" retrieves data for id=1 and filled grid, later user entered id=2, here rows are "as it is". So "DataGridView1.Rows.Add()" will add another row in grid with data of id=2. Now you have two rows in grid one for id=1 and one for id=2.
Shahan Ayyub 7-Nov-12 11:34am    
Is it clear now ?

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