将值从 datagridview 复制到二维字符串 C#
Copy values from datagridview to 2 dimension string C#
我有一个二维变量,如下所示:
string[] Dimension = new string[]
{
"+-----+", // -----> X /\Y
"|XXXXX|",
"|XXXXX|",
"|XXXXX|",
"|XXXXX|",
"|XXXXX|",
"+-----+",
};
其次,我有 datagridview,其中有 6 行 + 6 列。
datagridview 的代码如下:
//Adding columns to datagridview
dataGridView1.Columns.Add("X0", "X0");
dataGridView1.Columns.Add("X1", "X1");
dataGridView1.Columns.Add("X2", "X2");
dataGridView1.Columns.Add("X3", "X3");
dataGridView1.Columns.Add("X4", "X4");
dataGridView1.Columns.Add("X5", "X5");
//Adding rows to datagridview
dataGridView1.Rows.Add("Y0");
dataGridView1.Rows.Add("Y1");
dataGridView1.Rows.Add("Y2");
dataGridView1.Rows.Add("Y3");
dataGridView1.Rows.Add("Y4");
dataGridView1.Rows.Add("Y5");
//Adding values to datagridview.
dataGridView1.Rows[1].Cells[1].Value = "X";
dataGridView1.Rows[2].Cells[1].Value = "X";
dataGridView1.Rows[2].Cells[2].Value = "X";
dataGridView1.Rows[1].Cells[2].Value = "X";
dataGridView1.Rows[3].Cells[3].Value = "X";
dataGridView1.Rows[4].Cells[3].Value = "X";
dataGridView1.Rows[4].Cells[4].Value = "X";
dataGridView1.Rows[3].Cells[5].Value = "X";
添加此值后,它在 winforms 中显示如下:
如何将值从 datagridview 转换(复制)到名为 Dimension 的字符串?
新字符串应该如下所示:
string[] map = new string[]
{
"+-----+", // -----> X /\Y
"|XX |",
"|XX |",
"| X X|",
"| XX |",
"| |",
"+-----+",
};
有可能吗?把这个 "X" 放在里面才能得到这个结果?
所以基本上您会使用 2 个 for 循环查看数据网格视图 Columns
和 Rows
。
1) 创建新地图:
string[] map = new string[dataGridView1.Rows.Count+1];
// give it a header
map[0] = "+" + new string('-', dataGridView1.Columns.Count - 1) + "+";
使用 StringBuilder 您可以为每个 line/row 构造一个取决于网格中的值的字符串。
for (int i = 1; i < dataGridView1.Rows.Count; i++)
{
StringBuilder sb = new StringBuilder();
sb.Append("|"); // frame left border
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
// try to get the value
string value = dataGridView1.Rows[i].Cells[j].Value?.ToString();
// if there is no value => take a space otherwise take the value
value = string.IsNullOrEmpty(value) ? " " : value;
// plug it into your string builder
sb.Append(value);
}
sb.Append("|"); // frame right border
// lunge the finished string into the array
map[i] = sb.ToString();
}
// close the frame
map[dataGridView1.Rows.Count] = "+" + new string('-', dataGridView1.Columns.Count - 1) + "+";
Console.WriteLine(string.Join(Environment.NewLine, map));
控制台中的结果输出如下所示:
+-----+
|XX |
|XX |
| X X|
| XX |
| |
| |
+-----+
我有一个二维变量,如下所示:
string[] Dimension = new string[]
{
"+-----+", // -----> X /\Y
"|XXXXX|",
"|XXXXX|",
"|XXXXX|",
"|XXXXX|",
"|XXXXX|",
"+-----+",
};
其次,我有 datagridview,其中有 6 行 + 6 列。 datagridview 的代码如下:
//Adding columns to datagridview
dataGridView1.Columns.Add("X0", "X0");
dataGridView1.Columns.Add("X1", "X1");
dataGridView1.Columns.Add("X2", "X2");
dataGridView1.Columns.Add("X3", "X3");
dataGridView1.Columns.Add("X4", "X4");
dataGridView1.Columns.Add("X5", "X5");
//Adding rows to datagridview
dataGridView1.Rows.Add("Y0");
dataGridView1.Rows.Add("Y1");
dataGridView1.Rows.Add("Y2");
dataGridView1.Rows.Add("Y3");
dataGridView1.Rows.Add("Y4");
dataGridView1.Rows.Add("Y5");
//Adding values to datagridview.
dataGridView1.Rows[1].Cells[1].Value = "X";
dataGridView1.Rows[2].Cells[1].Value = "X";
dataGridView1.Rows[2].Cells[2].Value = "X";
dataGridView1.Rows[1].Cells[2].Value = "X";
dataGridView1.Rows[3].Cells[3].Value = "X";
dataGridView1.Rows[4].Cells[3].Value = "X";
dataGridView1.Rows[4].Cells[4].Value = "X";
dataGridView1.Rows[3].Cells[5].Value = "X";
添加此值后,它在 winforms 中显示如下:
如何将值从 datagridview 转换(复制)到名为 Dimension 的字符串?
新字符串应该如下所示:
string[] map = new string[]
{
"+-----+", // -----> X /\Y
"|XX |",
"|XX |",
"| X X|",
"| XX |",
"| |",
"+-----+",
};
有可能吗?把这个 "X" 放在里面才能得到这个结果?
所以基本上您会使用 2 个 for 循环查看数据网格视图 Columns
和 Rows
。
1) 创建新地图:
string[] map = new string[dataGridView1.Rows.Count+1];
// give it a header
map[0] = "+" + new string('-', dataGridView1.Columns.Count - 1) + "+";
使用 StringBuilder 您可以为每个 line/row 构造一个取决于网格中的值的字符串。
for (int i = 1; i < dataGridView1.Rows.Count; i++)
{
StringBuilder sb = new StringBuilder();
sb.Append("|"); // frame left border
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
// try to get the value
string value = dataGridView1.Rows[i].Cells[j].Value?.ToString();
// if there is no value => take a space otherwise take the value
value = string.IsNullOrEmpty(value) ? " " : value;
// plug it into your string builder
sb.Append(value);
}
sb.Append("|"); // frame right border
// lunge the finished string into the array
map[i] = sb.ToString();
}
// close the frame
map[dataGridView1.Rows.Count] = "+" + new string('-', dataGridView1.Columns.Count - 1) + "+";
Console.WriteLine(string.Join(Environment.NewLine, map));
控制台中的结果输出如下所示:
+-----+
|XX |
|XX |
| X X|
| XX |
| |
| |
+-----+