如何在GridView中使用相同字段但值不同的两列

时间:2017-04-05 09:54:53

标签: c# mysql asp.net sql-server gridview

我目前正在ASP.NET / C#中做一个项目,我有一个GridView,它从两个不同的表中获取两种类型的数据,ID和缩写。

表格本身必须是这样的:

UNIT 1

ID - INITIALS

旁边有一个专栏:

UNIT 2

ID - INITIALS

正在从表2中调用ID,而INITIALS正在从表1中调用。每个ID都有自己的INITIALS,但我的问题是,由于INITIALS被调用两次,它们会重复以下内容和之前的列,如here所示。

我当前的代码(ASPX):

<asp:GridView ID="GridView1" runat="server" CssClass= "table table-hover table-bordered" AllowPaging="false" PageSize="4" OnPageIndexChanging="OnPaging" OnPageIndexChanged="SearchByTagButton_Click" Style="max-width:75%;" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="UNIDADE" HeaderStyle-ForeColor="#B70700" HeaderStyle-Width="34%">
                        <ItemTemplate>
                            <%# DataBinder.Eval(Container.DataItem, "unidade")%>
                            -
                            <%# DataBinder.Eval(Container.DataItem, "sigUnidade")%>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="UNIDADE APOIADA" HeaderStyle-ForeColor="#B70700" HeaderStyle-Width="34%">
                        <ItemTemplate>
                            <%# DataBinder.Eval(Container.DataItem, "unidadeApoiada")%>
                            -
                            <%# DataBinder.Eval(Container.DataItem, "sigUnidade")%>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>

和C#:

SqlDataAdapter da;
    DataSet ds1 = new DataSet();
    DataSet ds2 = new DataSet();    
SqlConnection conn = new SqlConnection(strConn);

    da = new SqlDataAdapter("SELECT ts.unidade, u.sigUnidade FROM T_SECRETARIAS ts INNER JOIN UNIDADES u on u.unidade = ts.unidade WHERE '%" + txtCodigoSearch.Text + "%' IS NULL OR LEN('%" + txtCodigoSearch.Text + "%') = 0 OR (ts.unidade='%" + txtCodigoSearch.Text + "%') OR ts.unidade LIKE '%" + txtCodigoSearch.Text + "%'", conn);
    da.Fill(ds1);
    da = new SqlDataAdapter("SELECT ts.unidadeApoiada, u.sigUnidade FROM T_SECRETARIAS ts INNER JOIN UNIDADES u on u.unidade = ts.unidadeApoiada WHERE '%" + txtCodigoSearch.Text + "%' IS NULL OR LEN('%" + txtCodigoSearch.Text + "%') = 0 OR (ts.unidadeApoiada='%" + txtCodigoSearch.Text + "%') OR ts.unidadeApoiada LIKE '%" + txtCodigoSearch.Text + "%'", conn);
    da.Fill(ds2);
    ds1.Merge(ds2);
    GridView1.DataSource = ds1;
    GridView1.DataBind();

我基本上加入了两个DataSet,每个SQL Query一个,因为我发现它是连接两个查询以获取两个ID的唯一方法(它们不同,因为它们都是表1中的“主ID”) ,但在表2中,它们也可以是“子ID”。

我试图解释我最好的,但希望我的表格格式化为ID - INITIALS而不重复。如果我能得到任何帮助,我将非常感激。

3 个答案:

答案 0 :(得分:0)

如何只使用一个UNION查询。 所以你会的 SELECT ts.unidade,u.sigUnidade FROM T_SECRETARIAS ts INNER JOIN UNIDADES .. 联盟 SELECT ts.unidadeApoiada as unidas,u.sigUnidade FROM T_SECRETARIAS ts INNER JOIN ...

您必须在两个查询

上为输出使用相同的名称

答案 1 :(得分:0)

从评论开始,您可以拥有以下代码:

select ename, hiredate, sal,
       (case when extract(year from hiredate) = 1980
             then sal * 0.3
             when extract(year from hiredate) = 1981
             then sal * 0.1
             when extract(year from hiredate) = 1982
             then sal * 0.2
             else 0
        end) as bonus
from emp;

并在aspx中:

SqlDataAdapter da;
DataSet ds1 = new DataSet(); 
SqlConnection conn = new SqlConnection(strConn);

da = new SqlDataAdapter("SELECT ts.unidade, ts.unidadeApoiada, u1.sigUnidade as sigUnidade1, u2.sigUnidade as sigUnidade2 FROM T_SECRETARIAS ts INNER JOIN UNIDADES u1 on u1.unidade = ts.unidade INNER JOIN UNIDADES u2 on u2.unidade = ts.unidadeApoiada WHERE....", conn);
da.Fill(ds1);
GridView1.DataSource = ds1;
GridView1.DataBind();

答案 2 :(得分:0)

您只需创建一列,例如电话号码,这就是您认为问题的最佳示例。我也在尝试解决这些类型的错误。这就是为什么我告诉您使用此代码的原因。希望对您有用。它是将多个文本框值合并到gridview中的单个单元格中的示例代码。

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=RVM\SQLEXPRESS;Initial Catalog=rvm;Persist Security Info=True;User ID=sa;Password=rvmbecse");
    string str = "insert into multext values('"+TextBox1.Text +" , " + TextBox2.Text + "')";
    con.Open();
    SqlCommand cmd = new SqlCommand(str,con);
    cmd.ExecuteNonQuery();
    con.Close();
    GridView1.DataBind();
    //Response.Write("<script>alert('HI RVM Data inserted Successfully')</script>");
}