在网格视图或列表视图上添加数据 [英] add data on grid view or list view

查看:95
本文介绍了在网格视图或列表视图上添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个表单,它将搜索我的数据库并将这些数据添加到数据网格视图或列表视图中.我有一个文本框,例如,在其中输入产品ID,当我单击按钮时,我希望该产品ID的数据显示在网格视图中(例如产品名称,产品说明).我的问题是,它没有在网格视图上添加数据,而是仅将网格视图的旧内容替换为新的内容.如何在网格视图或列表视图上添加多个数据?谢谢!

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!

推荐答案

使用以下方法代替直接绑定

首先将过滤后的数据放入数据集/数据表中.然后

In place of direct binding use the following ways

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

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);
}



只需考虑您的代码,在第一次绑定数据后,将在gridview中看到该数据,然后您尝试将Gridview与新条件绑定.那时,gridview仅包含新数据.


问候,
shefeek
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


这篇关于在网格视图或列表视图上添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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