如何忽略GridView中的空单元格

时间:2012-09-03 05:44:53

标签: c# asp.net gridview

每行都有一个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();
    }

我想检查单元格是否为空,如果为空则我想增加到下一个单元格而不对空单元格执行任何操作。

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

2 个答案:

答案 0 :(得分:3)

只使用修剪和刺痛功能

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

你需要在这里查看

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

使用框架

的parse或tryparse方法检查字符串是否可转换为整数
short n = 0;
if(Int16.TryParse(row.Cells[i + 1].Text,out n);)
{
  //perform function and user n here now
}

答案 1 :(得分: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
}

您的错误可能是抛出的异常,因为您的单元格文本为空或甚至为空。