I need to read only the last record bringing this data here (lblTotalBancoCons.Text). But I receive this error: Object cannot be cast from DBNull to other types.
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
conn.Close();
dvgBancoCons.DataSource = dt;
///////////////////////////////////
int sum = 0;
for (int i = 0; i < dvgBancoCons.Rows.Count; ++i)
{
sum += Convert.ToInt32(dvgBancoCons.Rows[i].Cells[10].Value);
lblTotalBancoCons.Text = " " + sum.ToString();
dvgBancoCons.CurrentCell = dvgBancoCons.Rows[dvgBancoCons.Rows.Count - 1].Cells[0];
VulpesPosted Feb 19, 2015, 2:05 PM
So you'll probably want the row before that which will be:
dvgBancoCons.CurrentCell = dvgBancoCons.Rows[dvgBancoCons.Rows.Count - 2].Cells[0];
IsraelPosted Feb 19, 2015, 1:14 PM
It's quity perfect. But let's me disturb little bit again once.
The error it's resolved. Then I need to read only the last record pushing my cursor at the last line. But I cannot.
VulpesPosted Feb 19, 2015, 12:06 PM
sum += Convert.ToInt32(dvgBancoCons.Rows[i].Cells[10].Value);
with:
object o = dvgBancoCons.Rows[i].Cells[10].Value;
if (!Convert.IsDBNull(o))
{
sum += Convert.ToInt32(o);
}