locked
Datatable add value to a specific Row.Column index. RRS feed

  • Question

  • Hallo

    I've been trying to understand how to address a specific row and column in a datatable, we created a grid which will be filled by numbers from a loop, but I cannot find a way to address the datatable eg. dt.Rows[1].Column[6] = 100 to add for instance a value to Rows 1 Column 6..

    Below the matrix, if there is a way please let us know.. I don't have fixed value and will need to be able to control the index for both the row and the columns.

    public static DataTable CreateSetTableMatrix(string TableName,int NrColumns, int NrRows)
            {
                DataTable dt = new DataTable();
                dt.TableName = TableName;
    
    
                int countRows = 1;
                int countColumns = 1;
    
                dt.Columns.Add("ID", typeof(string));
                
                while (countColumns <= NrColumns)
                {
                    dt.Columns.Add("C" + countColumns.ToString(),typeof(String));
                    countColumns++;
                } 
                
                while (countRows <= NrRows)
                {
                    dt.Rows.Add("R" + countRows.ToString());                
                    countRows++;
                }
                //dt.Clear(); 
                return dt;
            }

    Result of above method.


    labjac

    Monday, June 22, 2020 8:09 PM

Answers

  • The preferred method

    dt.Rows[1].SetField("Your colum name",100);

    Or

    dt.Rows[1][x] = 100;

     

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Monday, June 22, 2020 8:12 PM

All replies

  • The preferred method

    dt.Rows[1].SetField("Your colum name",100);

    Or

    dt.Rows[1][x] = 100;

     

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Monday, June 22, 2020 8:12 PM
  • Thanks Karen

    Been staring at this for to long..

     dt.Rows[countRow-1][countColumn] = WallNumberGenerator.PaddLeft(nrArea, "0") + WallNumberGenerator.PaddLeft(nrZones, "0") +
                                                             WallNumberGenerator.PaddLeft(countRow, "0") + WallNumberGenerator.PaddLeft(countColumn, "0"); 


    labjac

    Tuesday, June 23, 2020 5:23 AM