【问题标题】:error message fix错误信息修复
【发布时间】:2012-10-08 15:33:19
【问题描述】:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    //bool sleected = false;

    if (dataGridView1.Rows[i].Cells[3].Value != null)
    {

        selected.Add(i);
    }

}
//string donew = "";


// line off error 
textBox1.Text = ((String)dataGridView1.Rows[1].Cells[2].Value);
/*  for (int i = 0; i < selected.Count; i++)
{

    textAdded.Add((String)dataGridView1.Rows[0].Cells[2].Value);
 //   donew += (String)dataGridView1.Rows[selected[i]].Cells[2].Value;
}*/

我不断收到错误

无法将“System.Double”类型的对象转换为“System.String”类型

我能做些什么来克服这个问题?

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    而不是使用

    textBox1.Text = ((String)dataGridView1.Rows[1].Cells[2].Value);
    

    使用

    textBox1.Text = dataGridView1.Rows[1].Cells[2].Value.ToString();
    

    原因是您尝试显式转换,而从 double 到字符串的转换不存在,但幸运的是 ToString() 方法将数字输出为字符串。

    【讨论】:

      【解决方案2】:

      此外,您可以使用foreach 循环进一步优化和清理它。在许多情况下,这将使其更易于阅读。

      此外,作为一般做法,请避免为这些对象键入 textBox1dataGridView1 类型名称。现在应用特定的命名模式,防止以后不必要的悲伤和担忧。尽早养成是个好习惯。

      foreach(DataGridViewRow r in dataGridView1.Rows)
      {
          if (r.Cells[3].Value != null)
          {
              selected.Add(r);
          }
      }
      // line off error 
      textBox1.Text = dataGridView1.Rows[1].Cells[2].Value.ToString();
      

      【讨论】:

        【解决方案3】:

        你可以试试

        dataGridView1.Rows[1].Cells[2].Value.ToString()
        

        【讨论】:

          猜你喜欢
          • 2022-01-02
          • 1970-01-01
          • 1970-01-01
          • 2018-07-07
          • 2023-03-14
          • 2019-06-24
          • 2012-01-13
          • 2011-04-29
          相关资源
          最近更新 更多