每当我点击行的单元格时,如何将数据填充到文本字段中?
对于标准DataGridView,我可以使用以下代码
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;
}
但在GridControl中,我不知道该怎么做......
答案 0 :(得分:-1)
使用DevExpress XtraGrid,您可以使用几乎相同的方法:
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();
//...
}
或
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();
//...
}
相关帮助文章:Obtaining and Setting Cell Values
<强>更新强>
请注意,如果启用了数据编辑并且ColumnViewOptionsBehavior.EditorShowMode属性设置为MouseDown,则单击行单元格时不会触发这些事件。
您可以使用GridView.OptionsBehavior.Editable属性对视图进行原位编辑。