如何在Foreach循环中将数据集绑定到Datagrid [英] How Do I Bind Dataset To Datagrid In A Foreach Loop

查看:80
本文介绍了如何在Foreach循环中将数据集绑定到Datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello house,



请问有谁知道如何在foreach循环中将数据集绑定到datagrid?我尝试过这样的事情:



Hello house,

Please does anyone know how to bind dataset to datagrid in a foreach loop? i have tried something like this:

foreach ( long k in listAttributeRegion )
                    {
                        var dataId = k;

                        try
                        {

                            using ( var strCon = new SqlConnection( ConfigurationManager.ConnectionStrings["edms"].ConnectionString ) )
                            {
                                using ( var com = new SqlCommand( "Select l.AttrID , l.ValStr from csowner.LLAttrData l join csowner.dtree d on  ID = d.DataID where d.DataID = @DataID order by l.AttrID asc", strCon ) )
                                {
                                    com.CommandType = CommandType.Text;
                                    // command.Parameters.Add( "@SelectOption", SqlDbType.VarChar, 25 ).Value = "staffLastname";
                                    com.Parameters.Add( "@DataID", SqlDbType.Int ).Value = 56059;
                                    using ( var adapter = new SqlDataAdapter( com ) )
                                    {
                                        //DataSet ds;
                                        using ( var ds = new DataSet( ) )
                                        {
                                            try
                                            {
                                                adapter.Fill( ds );

                                                dataGridView1.DataSource = ds.Tables[0];
                                            }
                                            catch ( Exception ex )
                                            {

                                                MessageBox.Show( ex.Message );
                                            }

                                        }
                                    }
                                }
                            }
                        }
                        catch ( SqlException ex )
                        {
                            // log.Error( ex.Message + "\r\n\r\n " + ex.StackTrace );
                            MessageBox.Show( ex.Message );
                        }
                    }


                    reader.Close( );
                    //Console.ReadKey( );
                }
            }
            catch ( Exception ex )
            {
                MessageBox.Show( ex.Message );
            }



但它不起作用!任何协助将不胜感激。在此先感谢。


But it is not working! any assistance will be appreciated. Thanks in advance.

推荐答案

创建一个sql语句,返回所需的所有数据。

create one sql statement which return all the data you want.
string sql = string.Format("Select l.AttrID , l.ValStr from csowner.LLAttrData l join csowner.dtree d on  ID = d.DataID where d.DataID in ({0}) order by l.AttrID asc", string.Join(",", listAttributeRegion));
using (var strCon = new SqlConnection( ConfigurationManager.ConnectionStrings["edms"].ConnectionString ) )
using (var com = new SqlCommand(sql, strCon))
using (var adapter = new SqlDataAdapter( com ))
{
   var ds = new DataSet();
   adapter.Fill(ds);
   dataGridView1.DataSource = ds.Tables[0];

}


这篇关于如何在Foreach循环中将数据集绑定到Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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