Это форма Master-Detail. Мастер - это GridView. И, деталь - это DetailsView.
Все это достигается программно.
Как видно из кода, DetailsView использует идентификатор Master-объектов для извлечения элементов Detail.
Мне нужно сделать столбец идентификатора Master-GridView невидимым. Потому что это не имеет значения для пользователя страницы. Но это не должно вредить логике страницы.
Но строка кода, 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();
}
}
