从GridView获取数据到RdotNet(R.net)

时间:2017-07-10 15:32:59

标签: sql asp.net .net r.net

我的任务很简单我只是想从gridview获取数据到REngine做随机函数返回行并在gridview中显示它们,我试过了

            TextBox_Ville.Text = "I'm here";
            DataTable dtb = (DataTable)Session["Grid"];
            REngine engine = REngine.GetInstance();
            string[,] stringData = new string[dtb.Rows.Count, dtb.Columns.Count];
            for (int row = 0; row < dtb.Rows.Count; row++)
            {
                for (int col = 0; col < dtb.Columns.Count; col++)
                {
                    stringData[row, col] = dtb.Rows[row].ItemArray[col].ToString();
                }
            }
            CharacterMatrix matrix = engine.CreateCharacterMatrix(stringData);
            engine.SetSymbol("myRDataFrame", matrix);
            engine.Evaluate("myRDataFrame <- as.data.frame(myRDataFrame, stringsAsFactors = FALSE)");
            //   engine.Evaluate("str(myRDataFrame)");
            DataFrame dataset = engine.Evaluate("myRDataFrame[sample(nrow(myRDataFrame), 1), ]").AsDataFrame();

            DataTable dtt = new DataTable();
            for (int i = 0; i < dataset.ColumnCount; ++i)
            {
                dtt.Columns.Add(new DataColumn(dataset.ColumnNames[i]));
            }

            for (int i = 0; i < dataset.RowCount; ++i)
            {
                var row = dtt.NewRow();

                for (int k = 0; k < dataset.ColumnCount; ++k)
                {
                    row[dataset.ColumnNames[k]] = dataset[i, k];
                }

                dtt.Rows.Add(row);
            }

            GridView1.DataSource = dtt;
            GridView1.DataBind();

但是它给了我Stackoverflow错误,任何人都可以帮忙。谢谢:))

1 个答案:

答案 0 :(得分:0)

在构建DataTable时,我看到你省略了类型。也许这就是问题?

dtt.Columns.Add(new DataColumn(dataset.ColumnNames[i], typeof(string)));

我注意到的另一件事是你使用&#39; row&#39;作为for循环中的迭代器,然后再次使用它来构建DataRow。我无法判断它是否存在问题。