从查询中获取一个值

时间:2018-11-30 08:02:56

标签: c# sql winforms

我想从此查询中获得一个RentalId值。然后我尝试这样,但是有一些错误。上次编码时出现了

之类的错误
  

'System.Windows.Forms.DataGridView.CurrentRow.get'返回null

代码:

public int RentalId { get; set; }

String query2 = "SELECT TOP 1 * FROM Rental ORDER BY RentalId DESC";
ds = new DataSet();
da = new SqlDataAdapter(query2, con);
da.Fill(ds);
builder = new SqlCommandBuilder(da);
dgvInUse.DataSource = ds.Tables[0];          
dgvInUse.Rows[0].Selected = true;

// I don't know how to extract RentalId
RentalId = Convert.ToInt32(dgvInUse.CurrentRow.Cells[0].Value.ToString());

0 个答案:

没有答案