views:

200

answers:

1

This is a Master-Detail form. Master is a GridView. And, the Detail is a DetailsView.

The entire thing is achieved programmatically.

As you can see from the code, DetailsView is using the Master-objects's ID to retrieve the Detail items.

I need to make the ID column of the Master-GridView invisible. Coz, it is irrelevent for the user of the page. But it must not harm the page logic.

But the code-line, GridView1.Columns[1].Visible = false; is generating an exception.

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

How should I solve this problem?

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();
        }
    }

alt text

+3  A: 

Have you considered using the DataKeyNames property of the gridview? This way you can remove the 'id' column from the GridView bu still access the 'id' value in the Page_Load.

DataKeyNames = "id"

Then you can get the value of the id like this.

int selRowIndex = GridView1.SelectedIndex;
int selMasterId = Convert.ToInt32(GridView.DataKeys[selRowIndex].Value);
Order master = Order.Get(selMasterId);

Alternately, you could try changing the visibility of the column in the OnRowBound event of the GridView.

protected void GridView_RowDataBound(object sender,   GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header ||
        e.Row.RowType == DataControlRowType.DataRow ||
        e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[1].Visible = false;
    }
}
Phaedrus
Do you have AutoGenerateColumns="true"?
Phaedrus
Yes I have that set to true.
JMSA
The columns collection of the GridView is not populated when using AutoGenerateColumns="true", this is why you are receiving the index out of range exception.
Phaedrus