Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
DataGridview1 output is:
1
2
3
4
5
6
7
8
9


C#
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"];

}


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
Posted
Updated 18-Sep-15 19:16pm
v3

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:

C#
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

SQL
select * from tablename where Id IN ( str );


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

Hope this will helpful to you...
 
Share this answer
 
Comments
Krunal Rohit 18-Sep-15 4:56am    
5!
-KR
Abhinav S 18-Sep-15 5:11am    
Not required IMO. Simple one line query should be enough.
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;
 
Share this answer
 

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