I am studying "TaskVision" project, find datagridviewTask1 control Loading Data,always throw NullReferenceException.This application is in gear,But it show the exception In Debug.I think about that runing application' capability alittle has effect. I find causation that "dataGridViewTasks1_CurrentCellChanged" event cause exception,Beacause in GetSelectedTaskID funcation's dataGridViewTasks1.CurrentRow isnt'existing"CurrnentRow"at all time. Code is as follows:
private int GetSelectedTaskID()
{
// if our datagridview isn't ready then return -1
if (dataGridViewTasks1.DataSource == null)
return -1;
// this code will find the id column in the style and together with the current row index
// will return the taskid currently selected.
try
{
int taskIDColumnIndex = dataGridViewTasks1.Columns["TaskID"].Index;
int currentRow = dataGridViewTasks1.CurrentRow.Index;
return (int)dataGridViewTasks1[taskIDColumnIndex, currentRow].Value;
}
catch (Exception)
{
return -1;
}
}
So I think thatwe should estimatedn thatdataGridViewTasks1'CurrentRow existencebefore we get dataGridViewTasks1'CurrentRow,my code is as follows:
private int GetSelectedTaskID()
{
// if our datagridview isn't ready then return -1
if (dataGridViewTasks1.DataSource == null)
return -1;
// this code will find the id column in the style and together with the current row index
// will return the taskid currently selected.
try
{
int taskIDColumnIndex = dataGridViewTasks1.Columns["TaskID"].Index;
if(dataGridViewTasks1.CurrentRow !=null)
{
int currentRow = dataGridViewTasks1.CurrentRow.Index;
return (int)dataGridViewTasks1[taskIDColumnIndex, currentRow].Value;
}
else
{
return -1;
}
}
catch (Exception)
{
return -1;
}
}