[英]C# Excel empty rows in combobox (causing error “System.FormatException:Input string was not in a correct format.”)
first of all, sorry for my bad english, but i'm a beginner with C# who's trying to make a simple Excel application (a school work) where you can edit Excel cells of player list and add new players in the list.首先,对不起我的英语不好,但我是 C# 的初学者,他正在尝试制作一个简单的 Excel 应用程序(学校工作),您可以在其中编辑 ZC1D81AF5835844B4E9D936910DED8FDC 的玩家列表中的新玩家列表和添加新玩家列表。 Editing works perfectly, but I can't add new players on the list.编辑工作完美,但我无法在列表中添加新玩家。 When i look at the combobox of the application, there's lots of empty cells, and probably because of that, adding new player isn't working.当我查看应用程序的 combobox 时,有很多空单元格,可能正因为如此,添加新播放器不起作用。
When trying to add a new player, it gives a following error:尝试添加新播放器时,出现以下错误:
System.FormatException:Input string was not in a correct format.
When I'm trying to debug the problem, it shows that the problem is in this line:当我尝试调试问题时,它表明问题出在这一行:
int nextID = Convert.ToInt32(lastID) + 1; // Lasketaan seuraavan ID:n arvo
Thanks already, and don't hesitate to ask, if you need more information to help me!已经谢谢了,如果您需要更多信息来帮助我,请不要犹豫!
Here's the code for the actual program.这是实际程序的代码。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
namespace Luokat.Persons
{
public class Persons : ExcelInstance
{
//variables internal use
private string id;
private string lastname;
private string firstname;
private string email;
private string postalcode;
private string city;
private string phone;
private ArrayList arInfo = new ArrayList();
private string ok = "0";
private string found = "0";
private string error = "0";
//properties external use
public string Id
{
get
{
return id;
}
set
{
id = value;
}
}
public string Lastname
{
get
{
return lastname;
}
set
{
lastname = value;
}
}
public string Firstname
{
get
{
return firstname;
}
set
{
firstname = value;
}
}
public string Email
{
get
{
return email;
}
set
{
email = value;
}
}
public string Postalcode
{
get
{
return postalcode;
}
set
{
postalcode = value;
}
}
public string City
{
get
{
return city;
}
set
{
city = value;
}
}
public string Phone
{
get
{
return phone;
}
set
{
phone = value;
}
}
public ArrayList ArInfo
{
get
{
return arInfo;
}
set
{
arInfo = value;
}
}
public string Ok
{
get
{
return ok;
}
set
{
ok = value;
}
}
public string Found
{
get
{
return found;
}
set
{
found = value;
}
}
public string Error
{
get
{
return error;
}
set
{
error = value;
}
}
//Methods
public void searchPersons()
{
try
{
int totalRows = xlRange.Rows.Count;
int totalColumns = xlRange.Columns.Count;
for (int rowCount = 2; rowCount <= totalRows; rowCount++)
{
this.id = xlRange.Cells[rowCount, 1].Text;
this.lastname = xlRange.Cells[rowCount, 2].Text;
this.firstname = xlRange.Cells[rowCount, 3].Text;
ArInfo.Add(this.Id + " " + this.Lastname + " " + this.Firstname);
}
//xlWorkBook.Close(true);
//xlApp.Quit();
}
catch (Exception e)
{
this.error = e.ToString();
}
}
public void searchPerson(string sID)
{
/*Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(FilePath);
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range xlRange = xlWorkSheet.UsedRange;*/
int totalRows = xlRange.Rows.Count;
int totalColumns = xlRange.Columns.Count;
for (int rowCount = 2; rowCount <= totalRows; rowCount++)
{
this.id = xlRange.Cells[rowCount, 1].Text;
if (this.id == sID)
{
this.Email = xlRange.Cells[rowCount, 4].Text;
this.Postalcode = xlRange.Cells[rowCount, 5].Text;
this.City = xlRange.Cells[rowCount, 6].Text;
this.Phone = xlRange.Cells[rowCount, 7].Text;
}
}
}
public void editPerson(string sID, string sLastname, string sFirstname, string sEmail, string sPostalcode, string sCity, string sPhone)
{
int totalRows = xlRange.Rows.Count;
int totalColumns = xlRange.Columns.Count;
for (int rowCount = 2; rowCount <= totalRows; rowCount++)
{
this.id = xlRange.Cells[rowCount, 1].Text;
if (this.id == sID)
{
xlRange.Cells[rowCount, 2] = sLastname;
xlRange.Cells[rowCount, 3] = sFirstname;
xlRange.Cells[rowCount, 4] = sEmail;
xlRange.Cells[rowCount, 5] = sPostalcode;
xlRange.Cells[rowCount, 6] = sCity;
xlRange.Cells[rowCount, 7] = sPhone;
this.ok = "1";
break;
}
}
}
public void newPerson(string sLastname, string sFirstname, string sEmail, string sPostalcode, string sCity, string sPhone)
{
int totalRows = xlRange.Rows.Count;
int totalColumns = xlRange.Columns.Count;
int nextRow = totalRows + 1; // Lisätään rivien arvoa yhdellä
string lastID = xlRange.Cells[totalRows, 1].Text; // Haetaan viimeinen ID
int nextID = Convert.ToInt32(lastID) + 1; // Lasketaan seuraavan ID:n arvo
// Tarkistetaan sähköposti
for (int rowCount = 2; rowCount <= totalRows; rowCount++)
{
this.email = xlRange.Cells[rowCount, 4].Text;
if (this.email == sEmail)
{
this.found = "1"; // Jos löytyi, annetaan muuttujalle arvo 1
break;
}
}
if (found == "1") // Jos löytyi suljetaan instanssit ja poistutaan
{
xlWorkBook.Close(true);
xlApp.Quit();
return;
}
// Jos sähköposia ei löydy, lisätään uusi henkilö tietoineen
xlRange.Cells[nextRow, 1] = nextID;
xlRange.Cells[nextRow, 2] = sLastname;
xlRange.Cells[nextRow, 3] = sFirstname;
xlRange.Cells[nextRow, 4] = sEmail;
xlRange.Cells[nextRow, 5] = sPostalcode;
xlRange.Cells[nextRow, 6] = sCity;
xlRange.Cells[nextRow, 7] = sPhone;
this.ok = "1";
}
public void removePerson(string sID)
{
int totalRows = xlRange.Rows.Count;
for (int rowCount = 2; rowCount <= totalRows; rowCount++)
{
this.id = xlRange.Cells[rowCount, 1].Text;
if (this.id == sID)
{
xlWorkSheet.Rows[rowCount].EntireRow.Delete();
this.ok = "1";
break;
}
}
}
public void closeExcel()
{
xlWorkBook.Close(true); //true = autosave
xlApp.Quit();
}
}
}
lastID may or may not be int. lastID 可能是也可能不是 int。
Use Int32.TryParse(value, out number) instead.改用 Int32.TryParse(value, out number)。 That will solve your problem.这将解决你的问题。
int lastIDValue;
Int32.TryParse(lastID, out lastIDValue)
Try use int.TryParse尝试使用int.TryParse
if(int.TryParse(yourStr, out yourInt) == true)
{
//if success
}
else
{
//if fail
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.