在datagridview中显示上一个和新数据 [英] Display previous and new data in datagridview

查看:103
本文介绍了在datagridview中显示上一个和新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个checkedlistbox和datagridview,我想从checkedlistbox中选择多个项目,相应的数据需要从数据库中获取并显示在datagridview中。



它是当我从checkedlistbox中选择一个项目时,工作绝对正确,当我选择两个项目时,它只显示最新项目选择的值。



我尝试过什么:



I have one checkedlistbox and datagridview, i want to select multiple items from checkedlistbox and the corresponding data need to fetch from database and display in datagridview.

It is working absolutely correct when i select one of the item from checkedlistbox, when i select two item then it shows only latest item selected values.

What I have tried:

for (int j = 0; j <= i - 1; j++)
{
    string s = "select * from Final where Assigrp ='" + checkedListBox2.CheckedItems[j].ToString() + "'";
    sda = new SqlDataAdapter(s, con);
    ds = new DataSet();
    SqlCommandBuilder scb = new SqlCommandBuilder(sda);
    sda.Fill(ds, "Final");
    bsource.DataSource = ds.Tables["Final"];
    dataGridView1.DataSource = bsource;

}

推荐答案

试试这个



try this

string where = "";
           foreach (object item in checkedListBox1.CheckedItems)
               where += string.Format("'{0}',", item);
           where = where.TrimEnd(',');
           string query = "select * from Final where Assigrp in ({0})";
           string s = string.Format(query, where);
           sda = new SqlDataAdapter(s, con);


你的sql WHERE是得到一个项目。使用WHERE Assigrp IN()并以这种方式构建id列表。
Your sql WHERE is getting a single item. Use WHERE Assigrp IN () and build your list of ids that way.


这篇关于在datagridview中显示上一个和新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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