Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > No bug's bug
 

No bug's bug

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

kitzone  Friday, September 28, 2007 4:44 AM

Hi Msr KitZone . In MSDN , In remark Part you will find this line

To change the current row, you must set the CurrentCell property to a cell in the desired row.

MSDN Link : -

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.currentrow.aspx

I hope this will help you .

Pr.Wael  Wednesday, October 17, 2007 9:07 AM

Thank you,dear Pr,wa'el.My subject is comment,not is question.I haveknown resovled solutions,

Yet thank you for your hot heart.
kitzone  Wednesday, October 17, 2007 9:25 AM
I knew it wasn't mine.
nobugz  Thursday, October 18, 2007 12:08 AM

You can use google to search for other answers

Custom Search

More Threads

• Thumbnail Generation
• Application Updater Component
• Unable to Install TV Server on Windows Server 2003 Std.
• How to access Issue Vision from Remote PC
• TaskVision server 1.1 install fails on Windows Server 2003 Enterprise
• Display Detail Grid in Combobox Control
• Local Server Not Working
• Can not load my creature.
• write transaction
• Trouble with DataGridView - Exception: Object reference not set to an instance of an object.