ASP.Net - 从单个网格行构建数据集
ASP.Net - constructing a Dataset from a single Grid Row
我正在制作一个 ASP.Net 网站。其中一个页面包含我要显示的用户的 GridView。每行代表一个用户。
当点击 Search
按钮时,我想通过他们的名字找到一个特定的用户行(我已经实现了);然后,我想将仅包含那一行的数据集传递给缓存(然后将更新并显示在 gridView 中)。
所以我做了这个:
protected void Search_Click(object
sender, EventArgs e)
{
GridViewRow myRow = findRow(UsersView, userSearched.Text, 0);
if (myRow==null)//Pretty much a must-have since it's QD built.
{
ResponseLabel.ForeColor = System.Drawing.Color.Red;
ResponseLabel.Text = "User not found.";
return;
}
ResponseLabel.ForeColor = System.Drawing.Color.Green;
ResponseLabel.Text = "There you go.";
DataTable container = new DataTable(); // the problem starts here
DataRow convertedForTable = container.NewRow();
for (int i = 0; i < myRow.Cells.Count; i++)
{
convertedForTable.ItemArray[i] = myRow.Cells[i].Text;
}
Cache["Users"] = container;
UpdateSource(); // puts a dataset in the gridView.
}
我在数据行 itemArray 中得到一个 IndexOutOfBounds
。 - 这是在以许多不同的方式进行了许多不同的尝试之后。我想知道我怎样才能使这个工作,或者如果有更好的解决方案的话。
你的代码有问题
首先如果你运行它你会得到这个错误:
Index was outside the bounds of the array.
因此您可以通过 Cells.Count
创建一个行数组对象,然后将单元格项文本设置为数组项,然后您可以将其设置为 ItemArray
,如下所示:
DataTable container = new DataTable();
DataRow convertedForTable = container.NewRow();
object[] rowArray = new object[myRow.Cells.Count];
for (int i = 0; i < myRow.Cells.Count; i++)
{
rowArray[i] = myRow.Cells[i].Text;
}
convertedForTable.ItemArray = rowArray;
其次,但是通过 运行 上面的代码,你也会得到这个错误:
Input array is longer than the number of columns in this table.
你知道为什么,因为你的 container
数据 Table 单元格的数量必须等于 myRow
单元格,换句话说 containar
没有任何列!
所以我想 myRow
有类似下面代码的列:
DataTable container= new DataTable();
container.Columns.Add(new DataColumn("UserName", typeof(string)));
container.Columns.Add(new DataColumn("Name", typeof(string)));
container.Columns.Add(new DataColumn("Family", typeof(string)));
DataRow convertedForTable = container.NewRow();
object[] rowArray = new object[myRow.Cells.Count];
for (int i = 0; i < myRow.Cells.Count; i++)
rowArray[i] = myRow.Cells[i].Text;
convertedForTable.ItemArray = rowArray;
container.Rows.Add(convertedForTable);
上面的代码工作正常,
要解决您的问题,请查看 DataRow and ItemArray
Link and ASP.Net Caching Techniques and Best Practices 可用于数据Table 缓存。
希望这会有所帮助。
我正在制作一个 ASP.Net 网站。其中一个页面包含我要显示的用户的 GridView。每行代表一个用户。
当点击 Search
按钮时,我想通过他们的名字找到一个特定的用户行(我已经实现了);然后,我想将仅包含那一行的数据集传递给缓存(然后将更新并显示在 gridView 中)。
所以我做了这个:
protected void Search_Click(object
sender, EventArgs e)
{
GridViewRow myRow = findRow(UsersView, userSearched.Text, 0);
if (myRow==null)//Pretty much a must-have since it's QD built.
{
ResponseLabel.ForeColor = System.Drawing.Color.Red;
ResponseLabel.Text = "User not found.";
return;
}
ResponseLabel.ForeColor = System.Drawing.Color.Green;
ResponseLabel.Text = "There you go.";
DataTable container = new DataTable(); // the problem starts here
DataRow convertedForTable = container.NewRow();
for (int i = 0; i < myRow.Cells.Count; i++)
{
convertedForTable.ItemArray[i] = myRow.Cells[i].Text;
}
Cache["Users"] = container;
UpdateSource(); // puts a dataset in the gridView.
}
我在数据行 itemArray 中得到一个 IndexOutOfBounds
。 - 这是在以许多不同的方式进行了许多不同的尝试之后。我想知道我怎样才能使这个工作,或者如果有更好的解决方案的话。
你的代码有问题
首先如果你运行它你会得到这个错误:
Index was outside the bounds of the array.
因此您可以通过 Cells.Count
创建一个行数组对象,然后将单元格项文本设置为数组项,然后您可以将其设置为 ItemArray
,如下所示:
DataTable container = new DataTable();
DataRow convertedForTable = container.NewRow();
object[] rowArray = new object[myRow.Cells.Count];
for (int i = 0; i < myRow.Cells.Count; i++)
{
rowArray[i] = myRow.Cells[i].Text;
}
convertedForTable.ItemArray = rowArray;
其次,但是通过 运行 上面的代码,你也会得到这个错误:
Input array is longer than the number of columns in this table.
你知道为什么,因为你的 container
数据 Table 单元格的数量必须等于 myRow
单元格,换句话说 containar
没有任何列!
所以我想 myRow
有类似下面代码的列:
DataTable container= new DataTable();
container.Columns.Add(new DataColumn("UserName", typeof(string)));
container.Columns.Add(new DataColumn("Name", typeof(string)));
container.Columns.Add(new DataColumn("Family", typeof(string)));
DataRow convertedForTable = container.NewRow();
object[] rowArray = new object[myRow.Cells.Count];
for (int i = 0; i < myRow.Cells.Count; i++)
rowArray[i] = myRow.Cells[i].Text;
convertedForTable.ItemArray = rowArray;
container.Rows.Add(convertedForTable);
上面的代码工作正常, 要解决您的问题,请查看 DataRow and ItemArray Link and ASP.Net Caching Techniques and Best Practices 可用于数据Table 缓存。
希望这会有所帮助。