合并GridView中某列相同信息的行的方法:
1 /// <summary> 2 /// 合并GridView中某列相同信息的行(单元格) 3 /// </summary> 4 /// <param name="GridView1">GridView</param> 5 /// <param name="cellNum">第几列</param> 6 public static void GroupRows(GridView GridView1, int cellNum) 7 { 8 int i = 0, rowSpanNum = 1; 9 while (i < GridView1.Rows.Count - 1) 10 { 11 GridViewRow gvr = GridView1.Rows[i]; 12 for (++i; i < GridView1.Rows.Count; i++) 13 { 14 GridViewRow gvrNext = GridView1.Rows[i]; 15 if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text) 16 { 17 gvrNext.Cells[cellNum].Visible = false; 18 rowSpanNum++; 19 } 20 else 21 { 22 gvr.Cells[cellNum].RowSpan = rowSpanNum; 23 rowSpanNum = 1; 24 break; 25 } 26 if (i == GridView1.Rows.Count - 1) 27 { 28 gvr.Cells[cellNum].RowSpan = rowSpanNum; 29 } 30 } 31 } 32 }
方法调用:
GroupRows(SearchGrird, 0); //针对第一列进行相同数据行合并 GroupRows(SearchGrird, 1); //针对第二列进行相同数据行合并
效果图:
注意:本方法不支持gridview模板列,如下:
1 <asp:TemplateField HeaderText="序号"> 2 <ItemTemplate> 3 <%#Container.DataItemIndex+1%> 4 </ItemTemplate> 5 <ControlStyle Font-Underline="False" /> 6 </asp:TemplateField>
支持下列的写法:
1 <asp:BoundField DataField="Name" HeaderText="病人姓名" /> 2 <asp:BoundField DataField="patient_id" HeaderText="门诊号" /> 3 <asp:BoundField DataField="visit_id" HeaderText="住院次数" /> 4 <asp:BoundField DataField="REPORT_NAME" HeaderText="报告类型" /> 5 <asp:BoundField DataField="SEX" HeaderText="性别" /> 6 <asp:BoundField DataField="Age" HeaderText="年龄" /> 7 <asp:BoundField DataField="Weight" HeaderText="体重" /> 8 <asp:BoundField DataField="Diagnosis" HeaderText="原患病历" /> 9 <asp:BoundField DataField="DOSAGE" HeaderText="怀疑药品名称" /> 10 <asp:BoundField DataField="START_TIME" HeaderText="用药开始时间" /> 11 <asp:BoundField DataField="END_TIME" HeaderText="用药结束时间" /> 12 <asp:BoundField DataField="FS_DATE" HeaderText="不良反应发生时间" /> 13 <asp:BoundField DataField="XX_MS" HeaderText="药品不良反应名称" /> 14 <asp:BoundField DataField="CS_FF" HeaderText="处理情况" /> 15 <asp:BoundField DataField="report_name_seven" HeaderText="结果" /> 16 <asp:BoundField DataField="REPORT_DATE" HeaderText="上报时间" /> 17 <asp:BoundField DataField="DEPT_NAME" HeaderText="上报科室" /> 18 <asp:BoundField DataField="REPORTER_SIGN" HeaderText="上报人" />