问题描述:

I have 3 nested ASPxGridViews in my project. One is the master grid, the other ones are detail grid connected with primary/foreign key. When I click the new button in a detail grid, the columns in the form comes totally blank. But I want some columns which have ParentID to be automatically selected because I already selected that in its master grid. Here is a specific example:

I have a master grid which shows companies. This grid has a detail grid which shows departments belonging to these companies. I chose one of the companies and departments which blongs to it appeared in the detail grid. Then I cliked new button to create a new department data. But in the form, company combobox column is blank while it should be automatically selected. I have to choose the same company in this combobox again.

I couldn't find anything in DevExpress forums about this problem. I appreciate if you help.

网友答案:

update: to show using CellEditorInitialize:

protected void gvDepartments_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
{
    var grid = sender as ASPxGridView;

    if (grid == null || !grid.IsNewRowEditing)return;
    if (!e.Column.FieldName.Equals("Company")) return;

    (e.Editor as ASPxComboBox).SelectedValue =  grid.GetMasterRowKeyValue();
}

gridView.GetMasterRowKeyValue() on a detail grid gives you he KeyValue of the Master Grid. You can use this in HtmlRowCreated or CellEditorInitialize Event on your detail grid to get the Master key field (company id in yourcase), and have the combobox in the Edit Form selected as shown below:

protected void gvDepartments_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
{
    if (e.RowType == GridViewRowType.EditForm)
    {
        var gridView = sender as ASPxGridView;
        var companyID = gridView.GetMasterRowKeyValue();
        var ddlCompany = gridView.FindEditFormTemplateControl("ddlCompany") as ASPxComboBox;
        ddlCOmpany.SelectedValue = companyID;
    }
}
相关阅读:
Top