【发布时间】:2015-05-01 16:33:13
【问题描述】:
我正在制作一个 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。 - 这是在许多不同的尝试以许多不同的方式做到这一点之后。我想知道如何才能完成这项工作,或者如果有更好的解决方案。
【问题讨论】:
-
您不应该在访问 ItemArray 之前为新的 DataTable 定义列吗?
-
你的代码有问题,你解决了吗?
标签: c# asp.net gridview dataset