C# 将字段从表传递到模型

C# 将字段从表传递到模型,c#,datagridview,C#,Datagridview,我使用此方法将数据从DataGridView传递到模型: for (rows = 0; rows < CView.dataGridView1.Rows.Count; rows++) { if (CView.dataGridView1.Rows[rows].Cells[col].Value != null) { // Creates a model, then populates each field from the cells in the table.

我使用此方法将数据从DataGridView传递到模型:

for (rows = 0; rows < CView.dataGridView1.Rows.Count; rows++)
{
    if (CView.dataGridView1.Rows[rows].Cells[col].Value != null)
    {
        // Creates a model, then populates each field from the cells in the table.
        SModel = new SupplierID_Model();
        SModel.Xsec = Convert.ToDouble(CView.dataGridView1.Rows[rows].Cells[0].Value);
        SModel.Insulation = Convert.ToString(CView.dataGridView1.Rows[rows].Cells[1].Value);
        // etc...
        // Passes the model into the Database.
    }
    else
    {
        // Passes the empty fields into dummy variables to avoid null entries in the Db.
        suppPartNo = string.Empty;
        xSec = string.Empty;
        insul = string.Empty;
        // etc...
    }
}
for(rows=0;rows
我知道这个特定实例中的问题是,我在DataGridView的底部有两个空行,这导致
else
无法正确实现。它只是返回到
SModel.Xsec=Convert.ToDouble…
并说“输入字符串的格式不正确”

EDIT:我也尝试了
while
循环,但是最后一个条目以
0开始并继续循环。也就是说,它继续无限期地输入相同的值,而不输入表的其余部分


是否有更好的方法允许出现更多错误(空字段)?

数据网格中有什么类型的值?如果它们只是字符串,那么您最好在执行强制转换操作后使用string.isNullOrEmpty。@goldenelf2是
int
double
string
的混合体,为什么不简单地将网格绑定到
IBindingList