Hi,
I am working on a GridView control to play a game. I am trying to make the cells editable; however, when I click on edit I lost my data or I get an error:
Object reference not set to an instance of an object
Here is a code sample (excuse the messiness, I am still debugging things and I like to clean-up at the end)
Any help would be greatly appreciated.
Thanks!
I am working on a GridView control to play a game. I am trying to make the cells editable; however, when I click on edit I lost my data or I get an error:
Object reference not set to an instance of an object
Here is a code sample (excuse the messiness, I am still debugging things and I like to clean-up at the end)
Code:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { NewGame(0); } else { lblStatus.Text = lblStatus.Text + "postback!"; //DataRow[] currentRows = mySet.Tables["numberset"].Select(null, null, DataViewRowState.CurrentRows); mySet = _newGame.GameSet; DataRow[] currentRows = mySet.Tables["problemcopyset"].Select(null, null, DataViewRowState.CurrentRows); foreach (DataRow row in currentRows) { foreach (DataColumn column in mySet.Tables["problemcopyset"].Columns) { lblStatus.Text = lblStatus.Text + row[column] ; //+ "(" + ; } ////lblStatus.Text = lblStatus.Text + row.RowState; } lblStatus.Text = lblStatus.Text + " "; } } private void NewGame(int index) { GameLevel[] levels = { GameLevel.SIMPLE, GameLevel.MEDIUM, GameLevel.COMPLEX }; if (index > -1) { _newGame.GenerateGame(levels[index]); gameGrid.Visible = false; _currentSet = _newGame.GameSet; mySet = _currentSet; _currentSet.Tables["numberset"].DefaultView.AllowNew = false; //_currentSet.Tables["numberset"].Columns["numbersets_Id"].ReadOnly = true; if (_currentSet != null) { _currentSet.Tables["answerset"].ColumnChanging += new DataColumnChangeEventHandler(this.CurrentSet_ColumnChanging); //DataRow[] currentRows = _currentSet.Tables["numberset"].Select(null, null, DataViewRowState.CurrentRows); } gameGrid.Visible = true; gameGrid.DataSource = _currentSet.Tables["numberset"]; gameGrid.DataBind(); } } protected void gameGrid_RowEditing(object sender, GridViewEditEventArgs e) { lblStatus.Text = lblStatus.Text + " editing! "; gameGrid.EditIndex = e.NewEditIndex; gameGrid.DataSource = mySet.Tables["numberset"]; gameGrid.DataBind(); }
Thanks!