Click here to Skip to main content
15,885,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a form the will search through my database and add those data into a data grid view or list view. I have a text box where I input the product ID for example and when I click the button I want the data of that Product ID to show on the grid view (product name, product description for example). My problem is that instead of adding data on the grid view, it just replaces the old content of the grid view in to a new one. How do I add multiple data on a grid view or list view? Thank you!
Posted

In place of direct binding use the following ways

first put your filtered data in a dataset/datatable. then

C#
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
  DataRow rw = ds.Tables[0].Rows[i];
  int cnt = rw.ItemArray.Length;
  string row1 = "";
  string[] cells = new string[cnt + 1];
  for (int j = 0; j < cnt; j++)
  {
     cells[j] = rw.ItemArray.GetValue(j).ToString();
  }
  dataGridView1.Rows.Add(cells);
}
 
Share this answer
 
Comments
shimaro82 25-Jul-11 3:19am    
Hmm I think this might do it... I'll try it when I get home.. Thank you :)
shimaro82 25-Jul-11 10:33am    
@Raju.. I have a question.. what is that ds.tables? is that the data adapter? or the data table? because It doesn't come out in the code of data adapter and data table.
Raju Prajapati 1-Aug-11 5:46am    
ds is a DataSet
shimaro82 25-Jul-11 11:59am    
I get an error on DataRow rw = ds.Tables[0].Rows[i]; saying that Object reference not set to an instance of an object. I replaced ds with dt because I'm using a datatable...
Raju Prajapati 1-Aug-11 5:53am    
Ok, If we use dataset(a collection of tables) then we need to specify table with its name or index that's why we write DataSet.Tabele[0].Rows. But if we use datatable(a single table) we need not to do so, here we can directly indicate i.e. Datatable.Rows
Hi,
just think about your code,after your first binding the data will seen in the gridview,then you are trying to bind the gridview with new condition.At that time the gridview contains only new data.


regards,
shefeek
 
Share this answer
 
Comments
shimaro82 25-Jul-11 3:21am    
I know, but I don't know a way on how can I add multiple data.. do you have any ideas? I can't find a way on how I can send the current data after post back.

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