To bind a selected record value from datagridview..
public void fillGrid()
{
sqlcnn1 = obj_purchase.Connection_create();
sqlcnn1.Open();
SqlCommand cmdPayment = new SqlCommand();
DataSet DSPayment = new DataSet();
SqlDataAdapter DAPayment = new SqlDataAdapter();
string strPayment = "select * from PURCHASE_MASTER";
cmdPayment.CommandText = strPayment;
cmdPayment.Connection = sqlcnn1;
DAPayment.SelectCommand = cmdPayment;
cmdPayment.ExecuteNonQuery();
DAPayment.Fill(DSPayment);
if (DSPayment.Tables[0].Rows.Count > 0)
{
DGVpurchase.DataSource = DSPayment.Tables[0].DefaultView;
}
sqlcnn1.Close();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewCell cell = (DataGridViewCell)DGVpurchase.Rows[e.RowIndex].Cells[0];
if ((string)cell.Value.ToString().Trim() == "null" || (string)cell.Value.ToString().Trim() == "")
{
}
else
oldid = (string)cell.Value;
Show_data();
}
}
private void Show_data()
{
if (oldid != null)
{
obj_purchase.selectpurchase(oldid);
DataTable dt1 = obj_purchase.selectpurchase(oldid);
txtProductId.Text = (string)dt1.Rows[0][0];
txtProductName.Text = (string)dt1.Rows[0][1];
txtQty.Text = (decimal.Round(decimal.Parse(dt1.Rows[0][2].ToString().Trim()), 2)).ToString().Trim();
cmbUnit.Text = (string)dt1.Rows[0][3];
txtcost.Text = (decimal.Round(decimal.Parse(dt1.Rows[0][4].ToString().Trim()), 2)).ToString().Trim();
cmbVat.Text = (decimal.Round(decimal.Parse(dt1.Rows[0][5].ToString().Trim()), 2)).ToString().Trim();
txtSalePrice.Text = (decimal.Round(decimal.Parse(dt1.Rows[0][6].ToString().Trim()), 2)).ToString().Trim();
txtRemarks.Text = (string)dt1.Rows[0][7];
ProductID = (string)dt1.Rows[0][0];
ProductName = (string)dt1.Rows[0][1];
txtProductId.Focus();
btnSave.Enabled = false;
btnDelete.Enabled = true;
btnUpdate.Enabled = true;
btnSearch.Enabled = false;
Mode = "Edit";
}
}