Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于垂直合并单元格的自定义代码_C# - Fatal编程技术网

C# 用于垂直合并单元格的自定义代码

C# 用于垂直合并单元格的自定义代码,c#,C#,我有创建网格的自定义代码,但它不是网格视图 Grid dt=new Grid(); var row = dt.NewRow(); int cellIdx = 0; row.Cells[0].Text = "Hello"; dt.Rows.Add(row); row.Cells[1].Text=" "; dt.Rows.Add(row); if (row.Cells[1].Text == " ") { row.Cells.Remove(row.

我有创建网格的自定义代码,但它不是网格视图

Grid dt=new Grid();
var row = dt.NewRow();
 int cellIdx = 0;
 row.Cells[0].Text = "Hello";
 dt.Rows.Add(row);
    row.Cells[1].Text=" ";
    dt.Rows.Add(row);
    if (row.Cells[1].Text == " ")
    {
        row.Cells.Remove(row.Cells[1]);  
    }

使用此代码,我可以水平合并两个单元格。但如何垂直合并两个单元格。

如果水平合并意味着您正在合并同一行中的单元格,则这意味着垂直合并将合并不同行中的单元格

因此,一个简单的方法是:

//check if one more row exists
if(dt.Rows.Count >= rowIndex+1)
{
    //get text of cell to be merged
    string mergeText = dt.Rows[rowIndex + 1].Cells[cellIndex].Text;
    //add Text to cell above
    dt.Rows[rowIndex].Cells[cellIndex].Text += Sring.Format("; {0}", mergeText);
    //delete text
    dt.Rows[rowIndex + 1].Cells.Remove(dt.Rows[rowIndex + 1].Cells[cellIndex]);
}

显然,如果您想在另一个方向合并,您需要将
+
替换为
-
,并且还需要修改if子句。

如何查找cellindex。如row=row-1查看您的问题
int-cellIdx=0这是cellIIndex。我只是用了一个不同的名字。Thnx Serv用于回复!!它只是将数据放到上一个单元格中