2
Answers

How to fetch data in a perticular desired gridview cell?

Ask a question
Hiiii,
 
i am working on a windows form with C# and MS Access database. a form contains a DataGridView and a TextBox.
there are many fields in the table but, i want only four items to be displayed in the gridview, the gridview is created at the time of the form load event and when emp_id to be enter in the text box, it fetches the record from database to the gridview... please see the code...
 
//when form loads.
private void frmReception_Load(object sender, EventArgs e)
{
dgvEmp.ColumnCount = 2; //Create gridview for Emp.
dgvEmp.RowCount = 2;

dgvEmp.Columns[0].Width = 293;
dgvEmp.Columns[1].Width = 293;
dgvEmp.Rows[0].Height = 38;
dgvEmp.Rows[1].Height = 38;
}
//when searching a record
private void txtEmpNo_Validated(object sender, EventArgs e)
{
   if (txtEmpNo.Text.Trim() == "")
{
   return;
}
else
{
OleDbDataAdapter SearchEmp = new OleDbDataAdapter("Select EmpFullName, EmpDOB, EmpMobileNumber, EmpCity From Patient Where EmpCaseNo='" + txtEmpNo.Text + "'", MyConn);

DataSet dsSearchEmp = new DataSet();
SearchEmp.Fill(dsSearchEmp);

if (dsSearchEmp.Tables[0].Rows.Count > 0)
{
dgvEmp.DataSource = dsSearchEmp.Tables[0];   //this gives result but creates new gridview
dgvEmp.Rows[0].Cells[1].Value = dsSearchEmp.Tables[0].Rows[0].ItemArray[2].ToString();   //not working

}
else
{
MessageBox.Show("No Record Found.", "Oops...");
txtEmpNo.Text = "";
return;
}
}
 
 i want to display the four results in four cells that i have created at the time of form load but it shows not according to that. the dataset creates new gridview and appends.
please suggest me what to do? is there any function or something that help me to do as i mentioned?
thank you.
 
Mayank Jani.

Answers (2)