在数据网格视图中显示数据 [英] Display data in data gridview

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

问题描述

我将第一个数据库值存储到数组并尝试用作数据库2中的搜索过滤器,所有工作正常但只有最后一个值被打印



这是我的代码:

I stored first database value to array and trying to use as a search filter in database 2 all working fine but only last value is getting printed

Here is my code:

//method1:

using System.Data.SqlClient;

// .....

string queryString = "Select productID from Table1 where custid="+ textbox1.text; 

List TempproductID = new List() ;


using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
if (!reader.IsDBNull(0))
{
Tempproductid.add( reader[0].ToString());


}
}
reader.Close();
tempproductid.toarray();
}

//method 2:
String stringprovider = "@database connection string "; 
for ( int I = 0; I < tempproductid.count; I++)
{
Sqlconnection con2 = new sqlconnection(stringprovider); Con2.open();
Cmd2.connection=con2; Cmd2.commandType = new commandType.text; Cmd2.commandText = "select * from Table2 where Productid = @productid"; Cmd2.parameters.addwithvalue("@productid",Tempproductid[i].to string());

Dataset Ds = new dataset(); sqldataadaptaer da1 = new sqldataadapter(cmd2); Datatable Table2 = new Data table(); Da1.fill(table2); 
Datagridview2.source = table2; 
} 
}





在for循环中,只有最后一个值在datagridview中没有得到全部?



In that for loop only last one value is getting in datagridview not getting all?

推荐答案

因为你每次都在循环中绑定gridview。在循环中的datatable中逐个添加数据,在整个循环结束后,将gridview绑定到侧循环。
Because you are binding gridview each time in loop. Add one by one data in datatable in loop and after whole loop gets over, bind gridview out side loop.


Sqlconnection con2 = new sqlconnection(stringprovider); Con2.open();

Cmd2.connection = con2; Cmd2.commandType = new commandType.text;



var parameters = new string [tempproductid.count];

for(int I = 0 ;我< tempproductid.count; I ++)

{



parameters [i] = string.Format(@ Productid {0} ,i);

Cmd2.Parameters.AddWithValue(parameters [i],tempproductid.items [i]);

}

Cmd2 .commandText = string.Format(select * from Table2 where Productid in({0}),string.Join(,,parameters));



数据集Ds = new dataset(); sqldataadaptaer da1 = new sqldataadapter(cmd2);

Datatable Table2 = new Data table(); Da1.fill(表2);

Datagridview2.source = table2
Sqlconnection con2 = new sqlconnection(stringprovider); Con2.open();
Cmd2.connection=con2; Cmd2.commandType = new commandType.text;

var parameters = new string[tempproductid.count];
for ( int I = 0; I < tempproductid.count; I++)
{

parameters[i] = string.Format("@Productid{0}", i);
Cmd2.Parameters.AddWithValue(parameters[i], tempproductid.items[i]);
}
Cmd2.commandText = string.Format("select * from Table2 where Productid in ({0})", string.Join(", ", parameters));

Dataset Ds = new dataset(); sqldataadaptaer da1 = new sqldataadapter(cmd2);
Datatable Table2 = new Data table(); Da1.fill(table2);
Datagridview2.source = table2


嗯(忽略无法编译的原因 - C#区分大小写,所以 TempproductID Tempproductid 不一样,并且正在使用您正在使用的危险代码)首先要注意的是您正在搜索完全匹配您的产品/客户ID值 - 除非您的系统中存在非常非常的错误,否则只会有一个与ID完全匹配的内容 - 如果您有两个或更多具有相同ID的客户,您要收取哪些费用?如果您有两个或两个以上具有相同ID的产品,您要发送哪个?



尝试LIKE查询:

Well (ignoring the reasons that won't compile - C# is case sensitive, so TempproductID is not the same as Tempproductid, and ingnoring the dangerous code you are using) the first thing to look at is that you are searching for an exact match on your product / customer id value - and unless there is something very, very amiss in your system, there will only ever be one exactly match on an ID - if you have two or more customers with the same id, who are you going to bill? If you have two or more products with the same ID, which are you going to send?

Try a LIKE query:
SELECT * FROM MyTable WHERE MyTextColumn LIKE '%Paul%'

将返回包含单词Paul的所有值 - '%'是SQL LIKE比较中的通配符。



但是严肃地说,永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询:

Will return all values that contain the word "Paul" - '%' is a wildcard character in SQL LIKE comparisons.

But seriously, never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead:

SELECT * FROM MyTable WHERE MyTextColumn LIKE '%' + @SEARCH + '%'

并通过@SEARCH

And provide the text via a parameter @SEARCH

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

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