A
Alex Ting
Hi Everybody,
I have an issue about deleting an object from an arrayList.
I've bounded a datagrid using this code
where it will first run through all of the code in loadQuestions() and bind
a arrayList to the datagrid.
private void BindQuestions()
{
dataGridQuestions.DataSource= this.AppQuiz.getQuestions();
this.toolBarBtnSave.Enabled=true;
this.toolBarBtnDel.Enabled=true;
}
public void loadQuestions()
{
this.BindQuestions();//this.AppQuiz.getQuestions();//AppQuiz();
if(this.DataGridSet==false)
{
DataGridTableStyle gs = new DataGridTableStyle(); // gs = Grid Style
gs.MappingName = AppQuiz.getQuestions().GetType().Name;
//dummyDataArr.GetType().Name;
gs.AllowSorting = true;
gs.AlternatingBackColor = System.Drawing.Color.AntiqueWhite;
DataGridTextBoxColumn cs2 = new DataGridTextBoxColumn();
cs2.MappingName = "QuestionID";
cs2.HeaderText = "No.";
cs2.Alignment = HorizontalAlignment.Left;
cs2.Width = 33;
cs2.ReadOnly = true;
gs.GridColumnStyles.Add(cs2);
DataGridBoolColumn cs = new DataGridBoolColumn(); // cs = Column
Style
cs.MappingName = "Publish";
cs.HeaderText = "Publish";
cs.Width = 75;
gs.GridColumnStyles.Add(cs);
cs = new DataGridBoolColumn();
cs.MappingName = "Required";
cs.HeaderText = "Mandatory";
cs.Width = 75;
cs2.ReadOnly = true;
gs.GridColumnStyles.Add(cs);
cs2 = new DataGridTextBoxColumn();
cs2.MappingName = "QuestionType";
cs2.HeaderText = "Question Type";
cs2.Alignment = HorizontalAlignment.Center;
cs2.Width = 80;
cs2.ReadOnly = true;
gs.GridColumnStyles.Add(cs2);
cs2 = new DataGridTextBoxColumn();
cs2.MappingName = "Ask";
cs2.HeaderText = "Question";
cs2.Alignment = HorizontalAlignment.Left;
cs2.Width = 255;
cs2.ReadOnly = true;
gs.GridColumnStyles.Add(cs2);
dataGridQuestions.TableStyles.Add(gs);
}
panelDgQuestions.Show();
panelSplash.Hide();
}
And then after that, they can delete a row from the datagrid. Then this
function is called
/// <summary>
/// this will remove the questions and then shuffle up all the questions
after it
/// </summary>
/// <param name="questionID">question ID to remove</param>
public void removeAndShuffle(int questionID)
{
for(int i=0; i< this.myQuestions.Count; i++)
{
if(((Learningfast.QGM.Question)this.myQuestions).QuestionID
== questionID)
{
this.myQuestions.RemoveAt(i);
}
}
}
In debug mode, for example if there were 2 rows, it is now 1 row and
everything seems fine, but when i rebind the datagrid with the arraylist
after an object has been removed,
private void BindQuestions()
{
dataGridQuestions.DataSource= this.AppQuiz.getQuestions();
this.toolBarBtnSave.Enabled=true;
this.toolBarBtnDel.Enabled=true;
}
Mind you it does not run through the initializing statements of the
datagrid, it just rebinds the datasource. And then at this point when the
datagrid is meant to be displayed, it gives me an error index [index number
that was deleted] is out of bounds.
Can someone please tell me whats happening? I've read up in several cases
and usually this means that I'm trying to bring up the object that was
deleted but I'm just changing the datasource to the amended arraylist. Could
this have to do with my existing datagrid where i'm not initializing it
again? because I've tried that and it also brings up an error saying that
i'm using the same "mapping name"
Thanks in advance.
Kind Regards
Alex Ting
I have an issue about deleting an object from an arrayList.
I've bounded a datagrid using this code
where it will first run through all of the code in loadQuestions() and bind
a arrayList to the datagrid.
private void BindQuestions()
{
dataGridQuestions.DataSource= this.AppQuiz.getQuestions();
this.toolBarBtnSave.Enabled=true;
this.toolBarBtnDel.Enabled=true;
}
public void loadQuestions()
{
this.BindQuestions();//this.AppQuiz.getQuestions();//AppQuiz();
if(this.DataGridSet==false)
{
DataGridTableStyle gs = new DataGridTableStyle(); // gs = Grid Style
gs.MappingName = AppQuiz.getQuestions().GetType().Name;
//dummyDataArr.GetType().Name;
gs.AllowSorting = true;
gs.AlternatingBackColor = System.Drawing.Color.AntiqueWhite;
DataGridTextBoxColumn cs2 = new DataGridTextBoxColumn();
cs2.MappingName = "QuestionID";
cs2.HeaderText = "No.";
cs2.Alignment = HorizontalAlignment.Left;
cs2.Width = 33;
cs2.ReadOnly = true;
gs.GridColumnStyles.Add(cs2);
DataGridBoolColumn cs = new DataGridBoolColumn(); // cs = Column
Style
cs.MappingName = "Publish";
cs.HeaderText = "Publish";
cs.Width = 75;
gs.GridColumnStyles.Add(cs);
cs = new DataGridBoolColumn();
cs.MappingName = "Required";
cs.HeaderText = "Mandatory";
cs.Width = 75;
cs2.ReadOnly = true;
gs.GridColumnStyles.Add(cs);
cs2 = new DataGridTextBoxColumn();
cs2.MappingName = "QuestionType";
cs2.HeaderText = "Question Type";
cs2.Alignment = HorizontalAlignment.Center;
cs2.Width = 80;
cs2.ReadOnly = true;
gs.GridColumnStyles.Add(cs2);
cs2 = new DataGridTextBoxColumn();
cs2.MappingName = "Ask";
cs2.HeaderText = "Question";
cs2.Alignment = HorizontalAlignment.Left;
cs2.Width = 255;
cs2.ReadOnly = true;
gs.GridColumnStyles.Add(cs2);
dataGridQuestions.TableStyles.Add(gs);
}
panelDgQuestions.Show();
panelSplash.Hide();
}
And then after that, they can delete a row from the datagrid. Then this
function is called
/// <summary>
/// this will remove the questions and then shuffle up all the questions
after it
/// </summary>
/// <param name="questionID">question ID to remove</param>
public void removeAndShuffle(int questionID)
{
for(int i=0; i< this.myQuestions.Count; i++)
{
if(((Learningfast.QGM.Question)this.myQuestions).QuestionID
== questionID)
{
this.myQuestions.RemoveAt(i);
}
}
}
In debug mode, for example if there were 2 rows, it is now 1 row and
everything seems fine, but when i rebind the datagrid with the arraylist
after an object has been removed,
private void BindQuestions()
{
dataGridQuestions.DataSource= this.AppQuiz.getQuestions();
this.toolBarBtnSave.Enabled=true;
this.toolBarBtnDel.Enabled=true;
}
Mind you it does not run through the initializing statements of the
datagrid, it just rebinds the datasource. And then at this point when the
datagrid is meant to be displayed, it gives me an error index [index number
that was deleted] is out of bounds.
Can someone please tell me whats happening? I've read up in several cases
and usually this means that I'm trying to bring up the object that was
deleted but I'm just changing the datasource to the amended arraylist. Could
this have to do with my existing datagrid where i'm not initializing it
again? because I've tried that and it also brings up an error saying that
i'm using the same "mapping name"
Thanks in advance.
Kind Regards
Alex Ting