Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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# 如何在2d数组中获取DataGridView数据?_C#_Arrays_Datagridview - Fatal编程技术网

C# 如何在2d数组中获取DataGridView数据?

C# 如何在2d数组中获取DataGridView数据?,c#,arrays,datagridview,C#,Arrays,Datagridview,在我的应用程序中,我试图将datagridview(5行和5列)数据获取到一个2d数组中,有人知道吗 帮助获取阵列中的数据 int countColumn = dataGridView1.ColumnCount -1; int j=0,k=0; string dataFromGrid = ""; foreach (DataGridViewRow i in dataGridView1.Rows) {

在我的应用程序中,我试图将datagridview(5行和5列)数据获取到一个2d数组中,有人知道吗 帮助获取阵列中的数据

        int countColumn = dataGridView1.ColumnCount -1;
        int j=0,k=0;
        string dataFromGrid = "";
        foreach (DataGridViewRow i in dataGridView1.Rows)
        {
            if (!i.IsNewRow)
            {


                //dataFromGrid = r.Cells[0].Value.ToString();

                for ( j = 0; j <= countColumn; j++)
                {
                    dataFromGrid = dataFromGrid + i.Cells[j].Value.ToString();


                }

            }
           var[k,j], Convert.ToInt32(dataFromGrid)
           k=k+1;
int countColumn=dataGridView1.ColumnCount-1;
int j=0,k=0;
字符串dataFromGrid=“”;
foreach(dataGridView1.Rows中的DataGridViewRow i)
{
如果(!i.IsNewRow)
{
//dataFromGrid=r.Cells[0]。Value.ToString();
对于(j=0;j
object[,]arr2d=newobject[dataGridView1.Rows.Count,dataGridView1.Columns.Count];
对于(int x=0;x

这应该是可行的

如果您喜欢foreach循环,可以这样做:

var array = new object[dataGridView1.RowCount,dataGridView1.ColumnCount];
foreach (DataGridViewRow i in dataGridView1.Rows)
{
    if (i.IsNewRow) continue;
    foreach (DataGridViewCell j in i.Cells)
    {
        array[j.RowIndex, j.ColumnIndex] = j.Value;
    }
}

内部循环条件应该是
j
@user3413736只需再次循环并将值添加到。text我不确定我是否理解正确,但是,数组“value”实际上是一个2x2数组,包含网格中的所有值。为了“获取值”从数组中,您需要构造一个包含数组中所有项的字符串,即再次解析数组并创建字符串。如果愿意,您可以在填充数组时创建此字符串,从而消除不必要的循环。但是,我不完全理解您的意思“在Tabbox中打印数组值”。那么,你必须考虑数组中有多个值。如果你只有一个文本框,那么你必须自己决定从数组中放入什么值。如果你已经有了索引,你就必须从数组中获取值:<代码> MyTrimBox。.ToString();
var array = new object[dataGridView1.RowCount,dataGridView1.ColumnCount];
foreach (DataGridViewRow i in dataGridView1.Rows)
{
    if (i.IsNewRow) continue;
    foreach (DataGridViewCell j in i.Cells)
    {
        array[j.RowIndex, j.ColumnIndex] = j.Value;
    }
}
//first of all create a 2D array varialble and count the rows and //column of data grid view
string[,] dataValue = new string[gridViewBillingItems.Rows.Count, gridViewBillingItems.Columns.Count];

//loop through the rows of data grid view 
foreach (DataGridViewRow row in gridViewBillingItems.Rows)
{
    //getting index of row
    int i = row.Index;
    //loop through the column of data grid view

    foreach (DataGridViewColumn col in gridViewBillingItems.Columns)
    {
        //getting index of column
        int j = col.Index;
        //add all the rows and column to 2D Array
        dataValue[row.Index, col.Index] = gridViewBillingItems.Rows[i].Cells[j].Value.ToString();
    }
}