blah blah blah is here! blah blah » Close

up1down
link

Hi,

I am working on a simple windows forms application and I use the following code to copy the text when a cell is clicked on the datagridview.

if (e.RowIndex >= 0 && dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
Clipboard.SetText(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());


Some of the cells of datagridview are empty and when I click on an empty cell by mistake it gives an error and stops running the application. Is there a solution to this?

It says something like "value cannot be null".

last answered one year ago

2 answers

up1down
link

If the DGV is bound to a database table, then an empty cell may in fact be represented by DBNull.Value rather than null.

I'd try checking for them both with:

if (e.RowIndex >= 0
&& dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != DBNull.Value
&& dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
Clipboard.SetText(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}


EDIT - see comments below

Try this instead:
if (e.RowIndex >= 0
&& !DBNull.Value.Equals(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value)
&& dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
Clipboard.SetText(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}

ademmeda
108

Thanks a lot, again you solved my issue. Can you suggest a resource about C# that will be useful for a beginner like me? A website, forum, book, tutorials, anything. I don't want to learn the whole language, I am trying to create simple applications that will interact with web pages and that's what my focus will be.

vulpes
17279

I think you might benefit from reading a book on Windows Forms as this will teach you a lot more than an online tutorial or a video. The two I'd check out are by Chris Sells (http://www.amazon.com/Windows-Forms-Programming-Microsoft-Development/dp/0321267966/ref=pd_sim_b_1) and Erik Brown (http://www.amazon.com/Windows-Forms-Action-Second-Programming/dp/1932394656/ref=dp_ob_title_bk). Both are based on .NET 2.0 (VS 2005) as later books have tended to focus on WPF. However, there hasn't been much change in Windows Forms programming in the meantime.

ademmeda
108

Thank you very much for the suggestions, I will check them as soon as possible.

up0down
link

Well, I thought the problem was solved but today when working with the file again, it gives the same error when I click empty cells on the datagridview. It says "Value cannot be null."

There is not any difference in using either of the following codes:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != DBNull.Value && dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null )
Clipboard.SetText(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}


or
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 )
Clipboard.SetText(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}

vulpes
17279

Try some slightly different syntax when checking for DBNull. Please see my edit. If it's no better, can you tell from the debugger what the Value property actually is when you hit that line?

Feedback