Hi,
In my datagridview there are 3 columns. I am entering values in all of these but second and third column values are not compulsory.
So user can escape those two columns.

But, when inserting those values in database
If the cell 2,3 contain value then to insert that value and if it doesnot contain value (if cell is empty) then enter "----" in database.

I have used the following code, but there is no success...

int i = dataGridView1.Rows.Count;
for (int j = 0; j < i; j++)
{
       string strCol2 = null;
       string strCol3 = null;

       if (dataGridView1.Rows[j].Cells[1].Value == null)
              strCol2 = "----";
       else
               strCol2 = dataGridView1.Rows[j].Cells[1].Value.ToString();

        if (dataGridView1.Rows[j].Cells[2].Value == null)
                strCol3 = "----";
         else
                strCol3 = dataGridView1.Rows[j].Cells[2].Value.ToString();
}

Regards,
Vidya

Try:

if (dataGridView1.Rows[j].Cells[1].Value == DBNull.Value)

You can create a method that uses a ternary operator that determines if a null value exist, if it does then send back an empty string or "----" rather than a null value.

logic: Does DataGridView(columnXX) have a null value? If Yes, send back an empty string "" or else pass the value to screen.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.