Click here to Skip to main content
15,885,048 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
How to bind a selected record value from datagridview to winforms controls using C#.net?
Posted
Comments
Ankit Rajput 13-Apr-11 7:37am    
Can you please describe a little bit your problem?
Toli Cuturicu 13-Apr-11 8:09am    
Not clear.
Sandeep Mewara 13-Apr-11 8:50am    
Looks incomplete!

1 solution

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();
}
C#
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";
}
}
 
Share this answer
 
Comments
Manfred Rudolf Bihy 13-Apr-11 11:23am    
Use pre tags around code so it will be properly formatted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900