单列Sql Server 2008查询中的多行

时间:2014-07-30 06:51:36

标签: c# asp.net sql-server-2008

我的表结构和数据看起来像这样

JobNo  JobStage         JobStatus

00645  Documentation    Copy Doc Recvd in mail / Courier 
00645  Documentation    Waiting for Arrival of shipment 
00645  Documentation    Online filing 
00645  Documentation    DutyIntimation
00645  Documentation    Collection of DO
00645  Documentation    DutyPaid
00645  Documentation    Examination in Process
00645  Documentation    OOC Taken
00645  Documentation    Delivered
00645  Shed             Goods Receiced 
00645  Shed             Verification 
00645  Shed             Online filing 
00645  Delivery         Deliverd

我想要这样

JobNo JobStage      JobStatus

00645 Documentation Copy Doc Recvd in mail / Courier
                    Waiting for Arrival of shipment 
                    Online filing
                    DutyIntimation
                    Collection of DO
                    DutyPaid
                    Examination in Process
                    OOC Taken
                    Deleiverd
00645  Shed         Goods Received
                    Verification
                    Online Filling
00645  Deleivery    Delivered

To display single row with multiple rows like above 
Kindly advise me 
Thanks in Advance





1 个答案:

答案 0 :(得分:1)

您可以使用asp.net GridView控制来显示上述数据。

您需要做的就是对数据进行分组。 您可以使用ado.net或Entity框架从数据库中获取数据。并将其与你的网格绑定。

HTML标记:

<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    runat = "server" AutoGenerateColumns="false" OnDataBound="OnDataBound">
    <Columns>
        <asp:BoundField DataField="JobNo" HeaderText="Job number" ItemStyle-Width="150" />
        <asp:BoundField DataField="JobStage" HeaderText="Job stage" ItemStyle-Width="150" />
        <asp:BoundField DataField="JobStatus" HeaderText="Job status" ItemStyle-Width="150" />
    </Columns>
</asp:GridView>

代码背后:

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            GridView1.DataSource = GetData("SELECT JobNo, JobStage, JobStatus FROM YourTABLE GROUP BY JobNo, JobStage, JobStatus");

            GridView1.DataBind();
        }
    }

private DataTable GetData(string query)
{
    DataTable dt = new DataTable();

    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                sda.Fill(dt);
            }
        }
        return dt;
    }
}

你必须在GridView的OnDataBound()事件中编写一些逻辑。在GridView填充记录后执行GridView的OnDataBound事件。反向循环在GridView Rows上执行,然后识别公共单元并将其合并到单个单元格中。

protected void OnDataBound(object sender, EventArgs e)
{
for (int i = GridView1.Rows.Count - 1; i > 0; i--)
{
    GridViewRow row = GridView1.Rows[i];
    GridViewRow previousRow = GridView1.Rows[i - 1];
    for (int j = 0; j < row.Cells.Count; j++)
    {
        if (row.Cells[j].Text == previousRow.Cells[j].Text)
        {
            if (previousRow.Cells[j].RowSpan == 0)
            {
                if (row.Cells[j].RowSpan == 0)
                {
                    previousRow.Cells[j].RowSpan += 2;
                }
                else
                {
                    previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                }
                row.Cells[j].Visible = false;
            }
        }
    }
}
}

希望这会对你有所帮助。