如何在单个datagridview中显示两个不同的表值 [英] How to two different table values display in single datagridview

查看:101
本文介绍了如何在单个datagridview中显示两个不同的表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   void  find1()
{
SqlConnection con = new SqlConnection(constr);
DataTable DT = new DataTable();
DataTable DT1 = new DataTable();
使用(System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient。 SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = select x.EmpID,x.UserName,a .Assigned from(select distinct UserName ,来自LPER.dbo.Passwords的EmpID,其中status ='Active')x内连接(选择不同的VEVCAssign,count(VEVCAssign)为从datacl分配,其中VEVCAssign<>''和PID =' + txt_pid。文字+ '和(LID =' + txt_lid1.Text + '或LID =' + txt_lid2.Text + '或LID =' + txt_lid3.Text + ') vEVCAssign上的a)a.VEVCAssign上的x.EmpID = a.VEVCAssign命令;
使用(System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient。 SqlDataAdapter(cmd))
{
adp.Fill(DT);

}
}
if (con.State == ConnectionState.Closed)
{
con.Open();
}


使用(System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = 从中选择b.cnt(从LPER.dbo.Passwords中选择不同的UserName,EmpID其中status ='Active')x内连接(选择不同的VEVCAssign,VEVC,计数(VEVC)作为来自datacl的cnt,其中VEVCAssign<>''和(VEVC<>'')和PID = 210和(LID =' + txt_lid1.Text + '或LID =' + txt_lid2.Text + '或LID =' + txt_lid3.Text + ')group by VEVCAssign,VEVC)b on x.EmpID = b.VEVCAssign order by b.VEVCAssign;
使用(System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient。 SqlDataAdapter(cmd))
{


adp.Fill(DT1);

}
}

DT.Merge(DT1);
gridview_datacl.DataSource = DT;
if (con.State!= ConnectionState.Closed)
{
con.Close();
}

}





输出为:



EmpID用户名分配数量

2016 jeya 1550

2035 saran 5652

3027 sasikumar 7063

3028 sathishbabu 2204

4003 manivannan 1953

1550

5652

7062

2204

1952



但我想输出的是:





EmpID用户名分配数量

2016 jeya 1550 1550

2035 saran 5652 5652

3027 sasikumar 7063 7062

3028 sathishbabu 2204 2204

4003 manivannan 1953 1952



第二个表输出显示在不同的行上。这是我想要在同一个DataGridView中合并两个表的问题。 Plz帮助一个...

解决方案

你好,

为什么不使用简单的连接。

 创建  #tmp(empid  varchar  50 ),用户名 varchar  50 ),Acount  int 
insert into #tmp values ' 2035'' saran' 5662
插入 进入 #tmp values ' 3027'' Sashi kumar' 7063
选择 * 来自 #tmp

- 创建第二个表
创建 #tmp1(empid varchar 50 ),Ccount int
插入 进入 #tmp1 ' 2035' 5663

选择 a.empid,a.acount,b.ccount 来自 #tmp a inner join #tmp1 b on a.empid = b.empid



输出将是

 empid acount ccount 
2035 5662 5663


public void find1()
        {
            SqlConnection con = new SqlConnection(constr);
            DataTable DT = new DataTable();
            DataTable DT1 = new DataTable();
            using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = "select x.EmpID,x.UserName,a.Assigned from (select distinct UserName,EmpID from LPER.dbo.Passwords where status='Active') x inner join (select distinct VEVCAssign,count(VEVCAssign) as Assigned from datacl where VEVCAssign<>'' and PID='" + txt_pid.Text + "' and (LID = '" + txt_lid1.Text + "' or LID = '" + txt_lid2.Text + "' or LID = '" + txt_lid3.Text + "') group by VEVCAssign) a on x.EmpID=a.VEVCAssign order by a.VEVCAssign";
                using (System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter(cmd))
                {
                    adp.Fill(DT);

                }
            }
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
         

            using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = "select b.cnt from (select distinct UserName,EmpID from LPER.dbo.Passwords where status='Active') x inner join (select distinct VEVCAssign,VEVC,count(VEVC) as cnt from datacl where VEVCAssign<>'' and (VEVC<>'')  and PID=210 and (LID = '" + txt_lid1.Text + "' or LID = '" + txt_lid2.Text + "' or LID = '" + txt_lid3.Text + "') group by VEVCAssign,VEVC) b on x.EmpID=b.VEVCAssign order by b.VEVCAssign";
                 using (System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter(cmd))
                 {
              
                
                     adp.Fill(DT1);

                }
            }

            DT.Merge(DT1);
            gridview_datacl.DataSource = DT;
            if (con.State != ConnectionState.Closed)
            {
                con.Close();
            }

        }



output is:

EmpID UserName Assigned Count
2016 jeya 1550
2035 saran 5652
3027 sasikumar 7063
3028 sathishbabu 2204
4003 manivannan 1953
1550
5652
7062
2204
1952

But I want output is:


EmpID UserName Assigned Count
2016 jeya 1550 1550
2035 saran 5652 5652
3027 sasikumar 7063 7062
3028 sathishbabu 2204 2204
4003 manivannan 1953 1952

Second table output is display on different rows. That is the problem i want merge both table in same DataGridView. Plz Help Some one...

解决方案

Hello ,
Why not you use simple join .

create    table #tmp (empid varchar(50),username varchar(50),Acount int)
insert into #tmp values('2035','saran',5662)
insert into #tmp values('3027','Sashi kumar',7063)
select * from #tmp

--creating second table
create table #tmp1 (empid varchar(50),Ccount int )
insert into  #tmp1 values('2035' , 5663)

select a.empid , a.acount , b.ccount from #tmp a inner join #tmp1 b on a.empid=b.empid


Out put will be

empid	acount	ccount
2035	5662	5663


这篇关于如何在单个datagridview中显示两个不同的表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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