将所有通用值的单元格值合并为一个

时间:2019-04-23 05:30:11

标签: asp.net gridview merge datatable

我有一个带有示例数据的DataTable

PATIENT_NAME        LAB_TEST                PRICE
P1                  BLOOD SUGAR             200
P1                  FASTING GLUCOSE         200
P1                  RFT                     1200
P2                  LFT                     1200
P2                  TBC                     1500
P3                  RBC                     1200

现在我想做的是,在PATIENT_NAME列中,应将与所有相应报告一起的病人单元格合并为一个excel。我在互联网上搜索了该程序,但找不到任何程序。在asp.net网格视图中有可能吗? 必需的输出::

PATIENT_NAME        LAB_TEST                PRICE
-----------------------------------------------------------
                    BLOOD SUGAR             200
P1                  FASTING GLUCOSE         200
                    RFT                     1200
----------------------------------------------------------
                    LFT                     1200
P2                  TBC                     1500
----------------------------------------------------------
P3                  RBC                     1200
----------------------------------------------------------

我找到了解决方案,但是问题是,它对所有列都进行了单元合并,但是我只希望它用于第一列而不是其他列。如何仅对第一列进行单元格合并?

protected void StdGrid_DataBound(object sender, EventArgs e)
{
    for (int i = StdGrid.Rows.Count - 1; i > 0; i--)
    {
        GridViewRow row = StdGrid.Rows[i];
        GridViewRow previousRow = StdGrid.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;
                }
            }
        }
    }
}

0 个答案:

没有答案