-3

How to populate data into text fields whenever I click on the cell of the row?

For standard DataGridView I can use the following the code

void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    txtguard_id.Text = dataGridView1.SelectedRows[0].Cells["GuardId"].Value.ToString();
    DateHired.Value = (DateTime)dataGridView1.SelectedRows[0].Cells["DateHired"].Value;
    Firstname.Text = dataGridView1.SelectedRows[0].Cells["FirstName"].Value.ToString();
    Middlename.Text = dataGridView1.SelectedRows[0].Cells["MiddleName"].Value.ToString();
    Lastname.Text = dataGridView1.SelectedRows[0].Cells["LastName"].Value.ToString();
    txtguard_street.Text = dataGridView1.SelectedRows[0].Cells["Street"].Value.ToString();
    txtguard_brgy.Text = dataGridView1.SelectedRows[0].Cells["Barangay"].Value.ToString();
    txtguard_procity.Text = dataGridView1.SelectedRows[0].Cells["ProvinceorCity"].Value.ToString();
    txtguard_age.Text = dataGridView1.SelectedRows[0].Cells["Age"].Value.ToString();
    txtguard_bday.Value= (DateTime)dataGridView1.SelectedRows[0].Cells["Birthdate"].Value;
    txtguard_male.Checked = (bool)dataGridView1.SelectedRows[0].Cells["Gender"].Value;
    txtguard_female.Checked = (bool)dataGridView1.SelectedRows[0].Cells["Gender"].Value;
}

But in GridControl i dont know how to do it...

DmitryG
  • 17,677
  • 1
  • 30
  • 53
JM Olesco
  • 21
  • 2
  • 2
  • 9
  • Can you please elaborate which textfield you want to populate? Is it edit form or any textbox in the page? Also, there are various forums and help available on devexpress site regarding the same. – Ruchi Feb 18 '13 at 20:34
  • http://www.devexpress.com/Support/Center/p/E2202.aspx – Ruchi Feb 18 '13 at 20:35
  • http://www.devexpress.com/Support/Center/p/Q321001.aspx – Ruchi Feb 18 '13 at 20:36
  • May b above links might help. Please let me know if it worked for u or not. thanks. – Ruchi Feb 18 '13 at 20:36
  • I believe the question is answered here http://stackoverflow.com/questions/12762617/how-to-get-the-selected-row-values-of-devexpress-xtragrid – Stig Feb 25 '13 at 08:02

1 Answers1

-1

With DevExpress XtraGrid you can use practically the same approach:

using DevExpress.XtraGrid.Views.Grid;
//...
gridView1.RowClick += gridView_RowClick;
//...
void gridView_RowClick(object sender, RowClickEventArgs e) {
    object id = ((GridView)sender).GetRowCellValue(e.RowHandle, "ID");
    testBoxId.Text = id.ToString();
    //...
}

or

using DevExpress.XtraGrid.Views.Grid;
//...
gridView1.RowCellClick += gridView_RowCellClick;
//...
void gridView_RowCellClick(object sender, RowCellClickEventArgs e) {
    object id = ((GridView)sender).GetRowCellValue(e.RowHandle, "ID");
    testBoxId.Text = id.ToString();
    //...
}

Related help article: Obtaining and Setting Cell Values

UPDATE:
Note that these events will not fire when clicking on a row cell, if data editing is enabled and the ColumnViewOptionsBehavior.EditorShowMode property is set to MouseDown.

You can dissable inplace editing for view using the GridView.OptionsBehavior.Editable property.

Community
  • 1
  • 1
DmitryG
  • 17,677
  • 1
  • 30
  • 53
  • i want to populate the textbox whenever the cell was clicked .. how to do that in Grid control RowCellCLick.. please help me.. – JM Olesco Feb 19 '13 at 11:12
  • @JMOlesco hmm...my code is quite clear... just pass the value which you've received from grid cell to textbox Text property – DmitryG Feb 19 '13 at 11:41
  • i tried it but it doesnt work.. private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) { radTextBox1.Text = ((GridView)sender).GetRowCellValue(e.RowHandle, "Customer_Id").ToString(); } it doesnt populted the Assigned Textbox i 've used... (The Customer_Id is the database part) – JM Olesco Feb 19 '13 at 11:56
  • @JMOlesco I believe you should use gridView1.OptionsBehavior.Editable = false for this scenario. – DmitryG Feb 19 '13 at 12:02
  • still, it doesnt work for me.. kindly post some piece of code on how to do that.. what i want is when i clicked on the cell it would populate data on the different textfield like "NAME, ADDRESS" this code is working for datagridview textbox1.Text = dataGridView1.SelectedRows[0].Cells["Street"].Value.ToString(); but in gridcontrol how to do that please help me – JM Olesco Feb 19 '13 at 12:07
  • @JMOlesco My code works fine to me. Please clarify your question with details: What the exact structure of data which shown in gridview (table/tables/fields/columns)? Are these data editable in gridView or not? Is the GetRowCellValue() method obtains correct value or not? – DmitryG Feb 19 '13 at 12:14
  • no.. they are not editable.. what i want is whenever i click on the different cell of the gridcontrol the different data on the gridcontrol clicked will move to the different textfield i have like name, address... that's what i want.. and another thing i tried ur code but i got exception which is object is not reference to an instance.. how to solve my problem – JM Olesco Feb 19 '13 at 12:18
  • @JMOlesco just do not use `((GridView)sender).GetRowCellValue(e.RowHandle, ...).ToString()` directly, because grid cell **can contain null values**... use the safe code: `object value = ((GridView)sender).GetRowCellValue(e.RowHandle, ...)` then check `if(value!=null) textBox.Text=value.ToString()`... and use debugger... – DmitryG Feb 19 '13 at 12:29
  • it works now.. thank you but i have another question how to deal with the object reference not to set an instance?.. i clicked the cell of customer_id but i got an exception there.. how to solve it – JM Olesco Feb 19 '13 at 12:37
  • I believe you should create another question about this new problem and post the problemmatic code. Thus the original question is complete, you can close discussion by accepting correct answer... – DmitryG Feb 19 '13 at 12:40