简体   繁体   中英

How to have two columns with the same field but different values in a GridView

I'm currently doing a project in ASP.NET/C# on where I have a GridView which gets two types of data from two different tables, ID and Initials.

The table itself must be sort of like this:


UNIT 1

ID - INITIALS


And a Column right next to it:


UNIT 2

ID - INITIALS


The IDs are being called from Table 2, while the INITIALS are being called from Table 1. Each ID has it's own INITIALS, but my problem is that, since the INITIALS are being called twice, they repeat themselves on the following and previous Columns, as seen here .

My current code (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>

And 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();

I basically join two DataSets, one for each SQL Query, as I found it the only way to join both queries to get both ID's (they're different, as in they're all a "main ID" in Table 1, but in Table 2 they can also be "sub ID's".

I tried to explain my best, but would like my table to be formatted as ID - INITIALS without any repetitions. If I could get any help, I'd be really appreciated.

How about using just one query with UNION . So you would have SELECT ts.unidade , u.sigUnidade FROM T_SECRETARIAS ts INNER JOIN UNIDADES .. UNION SELECT ts.unidadeApoiada as unidas, u.sigUnidade FROM T_SECRETARIAS ts INNER JOIN ...

You have to use the same name for the output on both Queries

Starting from comments, you can have the following code:

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();

and in aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="UNIDADE">
                    <ItemTemplate>
                        <%# DataBinder.Eval(Container.DataItem, "unidade")%>
                        -
                        <%# DataBinder.Eval(Container.DataItem, "sigUnidade1")%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="UNIDADE APOIADA">
                    <ItemTemplate>
                        <%# DataBinder.Eval(Container.DataItem, "unidadeApoiada")%>
                        -
                        <%# DataBinder.Eval(Container.DataItem, "sigUnidade2")%>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

You just create one column like phone number, it's the perfect example for your question I think. I am also trying to resolve these type of errors. That's why I am telling you use this code. I hope it's useful for you. It's sample code to merge multiple textbox values into a singe cell in 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>");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM