c# - Error "Object Reference not set to an Instance of an object" on read from DataGrid -


ok did quick test see if code set read datagrid, , does, once finishes reading error "object reference not set instance of object" , i'm not sure it's referring to. because loop continues after rows has reached end of datagrid?

public static void uploadfromextrernalsource(plantareacode_createview paccreate) {     // each row in datagrid, , each column, stores information in string.     (int rows = 0; rows < paccreate.datagridview1.rows.count; rows++)     {         (int col = 0; col < paccreate.datagridview1.rows[rows].cells.count; col++)         {             string value = paccreate.datagridview1.rows[rows].cells[col].value.tostring();             console.writeline(value + ",");         }     } } 

edit: when print console, prints each value on new line. why?

problem1 : trying convert null value 1 of cells string.

solution1 : before converting cell value string null check.

problem 2: using console.writeline() method display each cell value.so prints each cell value in new row/line.

solution 2: need use console.write() method instead of console.writeline() print cell values, , once after printing cell values given row need use console.writeline() print new line.

suggestion: don't need declare string variable value each time in loop, can move string variable declaration out of loop.

try this:

public static void uploadfromextrernalsource(plantareacode_createview paccreate) {     // each row in datagrid, , each column, stores information in string.     string value = string.empty;     (int rows = 0; rows < paccreate.datagridview1.rows.count; rows++)     {         (int col = 0; col < paccreate.datagridview1.rows[rows].cells.count; col++)         {             if(paccreate.datagridview1.rows[rows].cells[col].value!=null)             {                value=paccreate.datagridview1.rows[rows].cells[col].value;               console.write(value + ",");              }             else              { value=string.empty; }         }         console.writeline();//it prints new line      } } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -