this is my code. On Row databound i want to show some rows in If and Hide in some rows in else conditon. But it generate an exception that (Index was out of range. Must be non-negative and less than the size of the collection Parameter name: index.
protected void gvHotels_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = new DataTable();
if (e.Row.RowType == DataControlRowType.DataRow)
{
int index = Convert.ToInt32(gvHotels.DataKeys[e.Row.RowIndex].Values[1].ToString());
GridView gv = (GridView)e.Row.FindControl("gvRooms");
bool exist = status(listHotelCode, hotelCode);
if (exist == false)
{
if (ds != null)
{
var result = from r in ds.Tables["tblRooms"].AsEnumerable()
join trs in ds.Tables["tblHotels"].AsEnumerable() on (int)r["HotelId"] equals (int)trs["HotelId"]
where trs["hotelCode"].ToString() == hotelCode
select new
{
processId = trs["processId"].ToString(),
boardType = trs["boardType"].ToString(),
totalPrice = r["amount"].ToString(),
roomCategory = r["roomCategory"].ToString(),
adultPax = r["adultPax"].ToString(),
Childpax = r["Childpax"].ToString()
};
dt = LINQToDataTable(result);
}
listHotelCode.Add(hotelCode);
gv.DataSource = dt;
gv.DataBind();
}
else
{
gvHotels.Rows[index].Visible = false;
listHotelCode.Add(hotelCode);
}
}