简体   繁体   English

如何在GridView中有两列具有相同字段但值不同

[英]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.我目前正在 ASP.NET/C# 中做一个项目,在那里我有一个 GridView,它从两个不同的表中获取两种类型的数据,ID 和 Initials。

The table itself must be sort of like this:表格本身必须是这样的:


UNIT 1单元1

ID - INITIALS ID - 首字母缩写


And a Column right next to it:旁边有一列:


UNIT 2单元2

ID - INITIALS ID - 首字母缩写


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 . ID 是从表 2 中调用的,而 INITIALS 是从表 1 中调用的。每个 ID 都有自己的 INITIALS,但我的问题是,由于 INITIALS 被调用两次,它们在以下和前面的列中重复,如这里所见。

My current code (ASPX):我当前的代码(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#:和 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".我基本上加入了两个数据集,每个 SQL 查询一个,因为我发现这是加入两个查询以获得两个 ID 的唯一方法(它们是不同的,因为它们都是表 1 中的“主 ID”,但是在表 2 它们也可以是“子 ID”。

I tried to explain my best, but would like my table to be formatted as ID - INITIALS without any repetitions.我试图解释我最好的,但希望我的表格被格式化为 ID - INITIALS 没有任何重复。 If I could get any help, I'd be really appreciated.如果我能得到任何帮助,我将不胜感激。

How about using just one query with UNION .只使用一个查询与 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 ...所以你会有 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:在 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.这是将多个文本框值合并到 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>");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将相同的字段绑定到gridview中的两列中 - Binding same field into two columns in gridview 如何在GridView上使用不同的数据源列? - how to have columns on GridView with different data sources? 如何将相同名称的不同列添加到Gridview - How To Add Different Columns With Same Name To Gridview 在 autocompleteextender 中显示来自同一行不同列的两个不同值 - Display two different values from different columns of same row in autocoompleteextender 当不同表中的两列具有相同名称时,如何使用SqlDataReader读取数据? - How to read data using SqlDataReader when two columns in different tables have same name? 如果两个不同的类具有相同的属性,如何自动映射两个不同类的实例之间的值? - How to automatically map the values between instances of two different classes if both have same properties? 断言动态集合中两个不同的对象是否具有相同的值 - Assert if two different objects have same values in a dynamic collection 从GridView中的两列中划分值 - Dividing values from two columns in a GridView 如何比较具有相同键但不同值的两个字典值? - how to compare two Dictionaries values with same keys but different values? 让Gridview根据单击的按钮显示不同的列 - Have Gridview show different columns based on button clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM