2012-09-03 64 views
0

我对每行都有GridView和Edit链接。我可以单击Edit并填充GridView单元格中的数据,并可以更新GridView行,并且数据库中的相应表格也会更新。如何忽略GridView中的空单元格

我有一个保存按钮,它on_click读取每一行,逐列并执行一些操作。

如果GridView中的所有单元格都填充了一些数据,则该函数可以正常工作。如果GridView中的单元格为空,则会出现错误:错误:输入字符串格式不正确。

我已经尝试的代码是:

protected void SAVE_GRID_Click(object sender, EventArgs e) 
    { 
     foreach (GridViewRow row in GridView2.Rows) 
     { 
      string Loc = row.Cells[1].Text; 

      string strg = "SELECT Location_Type FROM Quantity WHERE Locations='" + Loc + "'"; 
      SqlCommand com = new SqlCommand(strg, con); 
      con.Open(); 
      SqlDataReader sdr = com.ExecuteReader(); 
      while (sdr.Read()) 
      { 
       Loctype.Text = sdr[0].ToString().Trim(); 
      } 
      con.Close(); 

      for (int i = 1; i < GridView2.Columns.Count; i++) 
      { 
       String header = GridView2.Columns[i].HeaderText;     

       string str = "SELECT Profile_Type FROM Profile_Tooltip WHERE Profile_Name='"+header+"'"; 
       SqlCommand cmd = new SqlCommand(str,con); 
       con.Open(); 
       SqlDataReader dr = cmd.ExecuteReader(); 
       while (dr.Read()) 
       { 
        profiletype.Text = dr[0].ToString().Trim(); 
       } 
       con.Close(); 

       if (!string.IsNullOrEmpty(row.Cells[i + 1].Text.Trim())) 
       { 
        int n = Convert.ToInt16(row.Cells[i + 1].Text); 
        //int n = int.Parse(row.Cells[i].Text); 

        for (int m = Asset_List.Items.Count - 1; m >= 0; m--) 
        { 
         Asset_List.Items.Remove(Asset_List.Items[m]); 
        } 

        for (int j = 1; j <= n; j++) 
        { 
         Asset_List.Items.Add(string.Concat(profiletype.Text, j)); 
        } 

        for (int k = 0; k <= Asset_List.Items.Count - 1; k++) 
        { 
         com = new SqlCommand("INSERT INTO " + Label3.Text + " VALUES ('" + Loctype.Text + "','" + Loc + "','" + header + "','" + Asset_List.Items[k] + "')", con); 
         con.Open(); 
         com.ExecuteNonQuery(); 
         con.Close(); 
        } 
       } 
      } 
     } 
     SqlCommand comd = new SqlCommand("SELECT * FROM " + Label3.Text + "", con); 
     SqlDataAdapter da = new SqlDataAdapter(comd); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
    } 

我要检查如果单元格是空的,如果为空我想以递增到下一个单元格不为空单元格执行任何动作。

请帮我解决这个问题。谢谢。

+0

删除* break *。 – adatapost

+0

仍然是一样的错误 –

+0

我已经提到了我的问题中的错误 –

回答

3

只是用修剪和刺痛功能

if (!string.IsNullOrEmpty(row.Cells[i].Text.Trim())) 

你需要在这里检查

int n = Convert.ToInt16(row.Cells[i + 1].Text); 

检查字符串转换为整数或不使用解析或框架的TryParse方法

short n = 0; 
if(Int16.TryParse(row.Cells[i + 1].Text,out n);) 
{ 
    //perform function and user n here now 
} 
+0

仍然是错误 –

+0

我的整个代码已经在我的问题中更新。这显示了我正在做的扫管笏。 –

+0

所以我应该更换int n = Convert.ToInt16(row.Cells [i + 1] .Text);到int n = 0; if(TryParse.int(row.Cells [i + 1] .Text,out n);) {//执行功能和用户n现在在这里 } ?????? –

0

对我最突出的线是这一个:

int n = Convert.ToInt16(row.Cells[i + 1].Text); 

我通常使用这样的事情,以避免例外:

int n = 0; 
bool didParse = Int16.TryParse(row.Cells[i + 1].Text, out n); 
if (didParse) 
{ 
    //add code here 
} 

你的错误可能是引发异常,因为你的文本为空,或者甚至是零。

+0

这也给我一个错误:错误1:'short.TryParse(string,out short)'的最佳重载方法匹配有一些无效参数。错误\t 2:参数2:无法从'out int'转换为'out short' –

+0

哈哈oops对不起,应该是int16 n = 0 –