C# DataGrid合並單元格


1.欄位枚舉

  private enum DataGridColumn
    {
        ROWNUM = 0,
        EMPID,
        EMPNAME,
        SEX,
        SALARY,
        ADRRESS,
        PHONE,
        TEL,
        POSITIION,
        REMARK
    }


2.合並函數

 private void SpanRow(DataGrid dg)
    {
        int i = 0;
        int j = 0;
        int rowNum = 0;//序號
        int rowSpan;
        string SEX = ""; //相同SEX,合並SEX單元格
        for (i = 0; i < dg.Items.Count; i++)
        {
            rowSpan = 1;
            SEX = dg.Items[i].Cells[(int)DataGridColumn.SEX].Text;
            for (j = i + 1; j < dg.Items.Count; j++)
            {
                if (string.Compare(SEX, dg.Items[j].Cells[(int)DataGridColumn.SEX].Text) == 0)
                {
                    rowSpan += 1;
                    dg.Items[i].Cells[(int)DataGridColumn.SEX].RowSpan = rowSpan;
                    dg.Items[j].Cells[(int)DataGridColumn.SEX].Visible = false;
                    dg.Items[i].Cells[(int)DataGridColumn.EMPNAME].RowSpan = rowSpan;
                    dg.Items[j].Cells[(int)DataGridColumn.EMPNAME].Visible = false;
                    dg.Items[i].Cells[(int)DataGridColumn.EMPNAME].RowSpan = rowSpan;
                    dg.Items[j].Cells[(int)DataGridColumn.EMPNAME].Visible = false;
                    dg.Items[i].Cells[(int)DataGridColumn.ROWNUM].RowSpan = rowSpan;
                    dg.Items[j].Cells[(int)DataGridColumn.ROWNUM].Visible = false;
                }
                else
                {
                    break;
                }
            }
            rowNum = rowNum + 1;
            dg.Items[i].Cells[(int)DataGridColumn.ROWNUM].Text = System.Convert.ToString(rowNum);
            i = j - 1;
        }
    }

 3.調用

  SpanRow(dgDataGrid);

 

作者:Adolf Ye
出處:http://www.cnblogs.com/dt520/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM