for循环或每个或同时使用显示DataGridView [英] for loop or for each or while using display a DataGridView

查看:101
本文介绍了for循环或每个或同时使用显示DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataGridview1输出为:

1

2

3

4

5

6

7

8

9





DataGridview1 output is:
1
2
3
4
5
6
7
8
9


for (int i=0; datagridview1.row[i].column[0]<=9; i++)
{

            SqlConnection con1 = new SqlConnection(constr);
            con1.Open();
            string sql = "select Name from table_name where id=datagridview1.row[i].column[0]"
            SqlCommand cmd = new SqlCommand (sql, con1);
            SqlDataAdapter daa = new SqlDataAdapter(cmd);
            DataSet dss = new DataSet();
            daa.Fill(dss, "table_name");
            gridview_datacl.DataSource = dss.Tables["table_name"];

}





此值将显示在DataGridView2上(gridview_datacl)



输出只显示最后一个值:

名称:

-------

Sai



但是我希望输出如下:





名称

------

Anand

baskar

kumar

bharath

Ganesh

Selva

Dachu

naveen

sai



this values will be display on DataGridView2(gridview_datacl)

out put is displayed only last value :
Name:
-------
Sai

BUT I WANT THE OUTPUT is Given BELOW:


Name
------
Anand
baskar
kumar
bharath
Ganesh
Selva
Dachu
naveen
sai

推荐答案





为什么要在每个循环中调用select语句,而不是使用IN运算符和打电话给列出的人并将该信息存储在一个DataTable中并在DataGrid中显示相同的内容。



EX:



Hi,

Why are you calling select statement each and every loop, instead of that use "IN" operator and call the listed people and store that information in one DataTable and display the same in your DataGrid.

EX:

string str="";
for(int i=0; i <=9; i++)
{
    str=str+","+Convert.Tostring(i);
}
str=str.Remove(0);





现在,写下你的选择语句如下





Now, wrote your select statement like below

select * from tablename where Id IN ( str );





并将结果存储到一个DataTable中并将该DataTable绑定到DataGridView中。



希望这对你有帮助...



and store that result into one DataTable and bind that DataTable into your DataGridView.

Hope this will helpful to you...


你知道你必须获得前10行。使用id并在一行中实现这一点 -

从table_name中选择名称,其中id< = 9;
You know you have to get the top 10 rows. Use id and achieve this in one line -
select Name from table_name where id<=9;


这篇关于for循环或每个或同时使用显示DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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