【问题标题】:auto generate id to textbox and datagridview自动生成 id 到文本框和 datagridview
【发布时间】:2014-11-12 17:58:19
【问题描述】:

我的代码有问题;当我运行我的程序时,它会产生以下错误:“输入字符串的格式不正确”。

这是我的代码:

public String generateID()
{
    String newID = "";           
    String lastID =dataGridView1.Rows[dataGridView1.RowCount1].Cells[0].Value.ToString();                
    newID = lastID.Substring(1);
    int temp = Int32.Parse(newID) + 1;
    newID = "E" + temp.ToString("00");
    return newID;
}


private void btnInsert_Click(object sender, EventArgs e)
{            
    textBox1.Text = generateID();                       
}

【问题讨论】:

    标签: c# auto-generate


    【解决方案1】:

    这是因为newID 不包含整数的字符串表示形式。单元格是空的吗?您应该确保单元格始终包含有效值。或者,使用Int32.TryParse 优雅地处理错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多