【问题标题】:How to avoid duplicate rows in C# from binded datatable?如何避免绑定数据表中 C# 中的重复行?
【发布时间】:2014-05-19 11:50:55
【问题描述】:

我的场景很简单,但我很难在编码中实现它。

我有两个表,分别命名为 film 和 filmtime。我使用 sql 查询加入了两个表,并从 datatable 绑定到 gridview。

胶片表的结构是,

theatrename     filmname
   max          Spiderman
   well         Godzilla

电影时间表的结构是,

theatrename      time
   max           10:00 am
   max           01:00 pm
   max           04:00 pm
   well          11:30 am

我希望我的输出为,

 theatrename     filmname     time
                              10:00 am
   max            Spiderman   01:00 pm
                              04:00 pm  
   well           Godzilla    11:30 am

我使用了这样的查询。

 SELECT film.theatrename,film.filmname,filmtime.time FROM film  left join filmtime on film.theatrename=filmtime.theatrename

通过使用这个查询,我得到了这样的结果,

theatrename     filmname     time
   max           Spiderman    10:00 am
   max           Spiderman    01:00 pm
   max           Spiderman    04:00 pm  
   well           Godzilla    11:30 am       

任何帮助都会对我更有帮助。提前致谢。

【问题讨论】:

标签: c# asp.net sql ado.net


【解决方案1】:

如果影院“max”的电影不止一部怎么办?你能看电影时间表里所有时间的电影吗?


它总是会显示 theatrename 和 filmname 以及时间,但是您可以先从电影表中选择电影名称和剧院名称,然后在创建 GridView 时为每一行第二次选择电影时间你在 whereclause 中有 theatrename ...

我认为如果我告诉你我的意思会更容易。

public void FillGridview()
{
    reader = Select * from film;

    while (reader.Read())
    {
        reader2 = Select time from filmtime where theatername = reader["theatername"];
        //add the filmname and the theatrename to the gridview-cells here
        while (reader2.Read())
        {
            //add the times to the gridview-cell here (You can start a new line if you want a break with Environment.NewLine()
        }

    }
}

或者您只是将剧院名称和电影名称写入会话中,在 gridview 的每个新行上检查其是否在会话中,如果是,则不要将其添加到新行的单元格中,如果没有将其添加到单元格中在行中。

或在RowDataBound-Event 上检查它,并在标签/任何内容不止一次出现时使其不可见。

【讨论】:

    猜你喜欢
    • 2012-08-07
    • 2017-05-19
    • 2013-01-31
    • 2020-10-25
    • 2019-11-13
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多