-3

每当我单击行的单元格时,如何将数据填充到文本字段中?

对于标准 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 我不知道该怎么做......

4

1 回答 1

-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();
    //...
}

相关帮助文章:获取和设置单元格值

更新:
请注意,如果启用数据编辑并且 ColumnViewOptionsBehavior.EditorShowMode 属性设置为 MouseDown,则单击行单元格时不会触发这些事件。

您可以使用 GridView.OptionsBehavior.Editable 属性禁用视图的就地编辑。

于 2013-02-19T09:41:50.290 回答