0

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

Edited by VidyaYadav: n/a

3
Contributors
2
Replies
26
Views
8 Years
Discussion Span
Last Post by sanch01r
0

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.....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.