Try something like this:
private DataRowView GetCurrentRowData()
{
if (JOGridView.CurrentCell == null) return null;
int rowIndex = JOGridView.CurrentCell.RowIndex;
if (rowIndex == -1) return null;
return JOGridView.Rows[rowIndex].DataBoundItem as DataRowView;
}
private void JOGridView_DoubleClick(object sender, EventArgs e)
{
DataRowView row = GetCurrentRowData();
if (row != null)
{
TBName.Text = Convert.ToString(row["Name"]);
TBContact.Text = Convert.ToString(row["Contact"]);
CBStatus.Text = Convert.ToString(row["Status"]);
TBModel.Text = Convert.ToString(row["Model"]);
TBSerial.Text = Convert.ToString(row["Serial"]);
TBAccess.Text = Convert.ToString(row["Access"]);
TBRB.Text = Convert.ToString(row["RB"]);
TBRP.Text = Convert.ToString(row["RP"]);
TBIT.Text = Convert.ToString(row["IT"]);
CBRamarks.Text = Convert.ToString(row["Remarks"]);
TBCharge.Text = Convert.ToString(row["Charge"]);
TBRELB.Text = Convert.ToString(row["RELB"]);
}
}
private void BTNEdit_Click(object sender, EventArgs e)
{
DataRowView row = GetCurrentRowData();
if (row != null)
(
DialogResult dr = MessageBox.Show("Are you sure you want to edit this record?", "Update Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
using (var cmd = con.CreateCommand())
{
cmd.CommandText = @"UPDATE
JobOrder
SET
Name = @Name,
Contact = @Contact,
Status = @Status,
Model = @Model,
Serial = @Serial,
Access = @Access,
RB = @RB,
RP = @RP,
IT = @IT,
Remarks = @Remarks,
Charge = @Charge,
RELB = @RELB
WHERE
ID = @ID
;";
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", TBName.Text);
cmd.Parameters.AddWithValue("@Contact", TBContact.Text);
cmd.Parameters.AddWithValue("@Status", CBStatus.Text);
cmd.Parameters.AddWithValue("@Model", TBModel.Text);
cmd.Parameters.AddWithValue("@Serial", TBSerial.Text);
cmd.Parameters.AddWithValue("@Access", TBAccess.Text);
cmd.Parameters.AddWithValue("@RB", TBRB.Text);
cmd.Parameters.AddWithValue("@RP", TBRP.Text);
cmd.Parameters.AddWithValue("@IT", TBIT.Text);
cmd.Parameters.AddWithValue("@Remarks", CBRamarks.Text);
cmd.Parameters.AddWithValue("@Charge", TBCharge.Text);
cmd.Parameters.AddWithValue("@RELB", TBRELB.Text);
cmd.Parameters.AddWithValue("@ID", row["ID"]);
con.Open();
try
{
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
displayrecords();
MessageBox.Show("Selected record has been updated!", "Done Updating", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
clearrecords();
}
(You might need to fix the column names to match your table; I've just guessed based on your control names.)