DataGridView_RowHeaderMouseClick 事件显示错误输入字符串的格式不正确

DataGridView_RowHeaderMouseClick Event Shows error Input string was not in a correct format

Input string was not in a correct format.

我在 sql 中使用了 uniqueidentifier 作为我的 ID,服务器数据库错误显示在第一行 ID = ...

private void DataGridViewDischarge_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
   ID = Convert.ToInt32(DataGridViewDischarge.Rows[e.RowIndex].Cells[0].Value.ToString());
   TxtHeadOverNotch.Text = DataGridViewDischarge.Rows[e.RowIndex].Cells[1].Value.ToString();
   TxtDischargeQ.Text = DataGridViewDischarge.Rows[e.RowIndex].Cells[2].Value.ToString();
}

Unique Identifier is GUID in sql server and GUID cannot be represent as int so you have to parse it to GUID, see here:

Guid id  = Guid.Parse(DataGridViewDischarge.Rows[e.RowIndex].Cells[0].Value.ToString());
private void dgv_expenditure_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    DataGridViewRow dgv = dgv_expenditure.Rows[e.RowIndex];
    int id = Convert.ToInt32(dgv.Cells[0].Value);
    tabControl1.SelectedIndex = 1;

    loadform(id);
}

private void loadform(int id)
{
    //   throw new NotImplementedException();
    SanmolCAEntities1 _db = new SanmolCAEntities1();

    var u = _db.a_expenditure.Where(p => p.id == id).FirstOrDefault();
    txt_name.Text = u.name;
    txt_CreateBy.Text = u.createby;
    dateTimePicker_excrdate.Value.ToString("yyyy-MM-dd HH:mm:ss");
    txt_updateBy.Text = u.updateby;
    dateTimePicker_exupdate.Value.ToString("yyyy-MM-dd HH:mm:ss");
}

此行错误:

int id = Convert.ToInt32(dgv.Cells[0].Value);

这是错误; 输入字符串的格式不正确