简体   繁体   中英

System.FormatException: 'Input string was not in a correct format.' data grid

When I run the application it displays no errors. After I load the datagridview from MySql and press the button that leads to the above function it displays error.

错误

int SMSLineID = 0;
List<SendMessageWebSerrvice.WebServiceSmsSend> SendDetail = new List<SendMessageWebSerrvice.WebServiceSmsSend>();
{
    string MessageBody = string.Empty;
    long MobileNo = 0;
    bool IsFlash = false;
    foreach(DataGridViewRow dataGridViewRow in dgvShowUsers.Rows)
    {
        DataGridViewCheckBoxCell Checking = dataGridViewRow.Cells["colSelect"] as DataGridViewCheckBoxCell;
        if (Convert.ToBoolean(Checking.Value) == true)
        {
            MessageBody = "User of " + dataGridViewRow.Cells[1].Value.ToString() + " " + dataGridViewRow.Cells[2].Value.ToString() + " " + txtMessage.Text;
            MobileNo = Convert.ToInt64(dataGridViewRow.Cells[3].Value);
            IsFlash = false;
            SendDetail.Add(new SendMessageWebSerrvice.WebServiceSmsSend(){
                MessageBody = MessageBody,
                MobileNo = MobileNo,
                IsFlash = IsFlash
            });
        }
    }
}
if (!int.TryParse(txtLineSerial.Text, out SMSLineID)) throw new Exception("Error");
SendMessageWebSerrvice.SendReceive WS = new SendMessageWebSerrvice.SendReceive();
string Message = null;
long[] Result = WS.SendMessage(txtUserName.Text.Trim(), txtPassword.Text.Trim(), SendDetail.ToArray(), SMSLineID, null, ref Message);
if (!string.IsNullOrWhiteSpace(Message)) throw new Exception(Message);
MessageBox.Show("OK");
return;

My grid view phone number is 3 .

Please do not post image. About your problem, because your input have some character isn't a number so system can't parse it. You should use this approach instead of the current:

Int64.TryParse($"{dataGridViewRow.Cells[3].Value}", out long value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM