这是一个主 - 明细表格。 Master是一个GridView。而Detail则是一个DetailsView。asp.net使gridView列隐形
整个事情是通过编程实现的。
从代码中可以看到,DetailsView使用主对象的ID来检索Detail项。
我需要使Master-GridView的ID列不可见。因为这对页面的用户来说是无足轻重的。但它不能损害页面逻辑。
但代码行GridView1.Columns[1].Visible = false;
正在生成异常。
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
我该如何解决这个问题?
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
List<Order> orders = Order.Get();
GridView1.DataSource = orders;
GridView1.DataBind();
// This is giving Error...............!!!
GridView1.Columns[1].Visible = false;
// At first, when the page first loads,
// GridView1.SelectedIndex == -1
// So, this is done to automatically select the 1st item.
if (GridView1.SelectedIndex < 0)
{
GridView1.SelectedIndex = 0;
}
int selRowIndex = GridView1.SelectedIndex;
int selMasterId = Convert.ToInt32(GridView1.Rows[selRowIndex].Cells[1].Text);
Order master = Order.Get(selMasterId);
labItemsCount.Text = master.Items.Count.ToString();
DetailsView1.DataSource = master.Items;
DetailsView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
BindData();
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
BindData();
}
}
什么是错误? – Phaedrus 2009-11-30 17:49:02