从嵌套数组中插入datagridview中的行 [英] Insert row in datagridview from nested array
问题描述
我正在尝试从嵌套数组字符串中添加DataGridView中的行。但是,我无法从阵列中获取数据。在我的DataGridView中,我有7列。现在,我想将7列的数据与嵌套数组分开。
我的代码:
I am trying to add row in DataGridView from an nested array string. But, I could't get data from the array. And in my DataGridView I have 7 Columns. Now, I want to separate 7 column's data from nested array.
My code:
private void btnAddItems_Click(object sender, EventArgs e)
{
string[, , , , , ,] row = new string[,,,,,,] { { { { { { { txtItemCode.Text.ToString(), txtItemType.Text.ToString(),
txtModel.Text.ToString(), txtItemDescription.Text.ToString(),
txtSerialNumber.Text.ToString(), txtQuantity.Text.ToString(),
txtUnit.Text.ToString() } } } } } } };
dtgridItemReceipt.Rows.Add(row);
}
请帮我从这个嵌套数组中获取7个数据,在DataGridView中设置为一行。在此先感谢。
我尝试过:
通过嵌套循环获取数据。
Please help me to get 7 data from this nested array to set in DataGridView as a Row. Thanks in advance.
What I have tried:
Just trying by nested loop to get data.
推荐答案
为什么要声明一个多维数组?您只需要一个包含7个元素的单维数组:
Why are you declaring a multi-dimensional array? All you need is a single-dimensional array with 7 elements:
string[] row = new string[]
{
txtItemCode.Text,
txtItemType.Text,
txtModel.Text,
txtItemDescription.Text,
txtSerialNumber.Text,
txtQuantity.Text,
txtUnit.Text
};
dtgridItemReceipt.Rows.Add(row);
阵列教程(C#) [ ^ ]
数组(C#编程指南) [ ^ ]
DataGridView.Rows属性(System.Windows.Forms) [ ^ ]
此外,无需在 Text
属性上调用 .ToString()
,因为它已经返回一个字符串。
Arrays Tutorial (C#)[^]
Arrays (C# Programming Guide)[^]
DataGridView.Rows Property (System.Windows.Forms)[^]
Also, there's no need to call .ToString()
on the Text
property, as it already returns a string.
这篇关于从嵌套数组中插入datagridview中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!