本文介绍了如何在单个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帮助一个...

解决方案

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
2016jeya 1550
2035saran 5652
3027sasikumar 7063
3028sathishbabu 2204
4003manivannan 1953
1550
5652
7062
2204
1952

But I want output is:


EmpID UserName Assigned Count
2016jeya 1550 1550
2035saran 56525652
3027sasikumar 70637062
3028sathishbabu 22042204
4003manivannan 19531952

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

解决方案


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

09-27 04:20