Devexpress GridView内嵌dx:ASPxGridLookup取得控件值乱跳解决方案

一 页面嵌入ASPxGridLookup控件

 <dx:ASPxGridView ID="gridDetail" runat="server"
                        KeyFieldName="ID"
                        OnRowInserting="gridDetail_RowInserting" >
                        <Columns>
                            <dx:GridViewCommandColumn Caption="操作" ButtonType="Image" Width="80px">
                                <HeaderTemplate>
                                    <dx:ASPxButton ID="btnDetailInsert" Image-Url="../Scripts/easyui/themes/icons/edit_Add.png"
                                        ClientInstanceName="btnDetailInsert" AutoPostBack="true" runat="server" OnClick="btnDetailInsert_Click">
                                    </dx:ASPxButton>
                                </HeaderTemplate>
                                <DeleteButton Text="删除" Visible="true">
                                    <Image ToolTip="删除" Url="../Scripts/easyui/themes/icons/no.png" />
                                </DeleteButton>
                                <UpdateButton Text="保存" Visible="true">
                                    <Image ToolTip="保存" Url="../Scripts/easyui/themes/icons/filesave.png" />
                                </UpdateButton>
                                <EditButton Text="编辑" Visible="true">
                                    <Image ToolTip="编辑" Url="../Scripts/easyui/themes/default/images/tree_file.Gif" />
                                </EditButton>
                                <CancelButton Text="取消" Visible="true">
                                    <Image ToolTip="取消" Url="../Scripts/easyui/themes/icons/back.png" />
                                </CancelButton>
                            </dx:GridViewCommandColumn>
                            <dx:GridViewDataTextColumn Caption="管理员" Width="100px" FieldName="GuideAdmin.Name"
                                VisibleIndex="1">
                                <EditItemTemplate>
                                    <dx:ASPxGridLookup ID="GridGuideAdmin" Width="100%" runat="server" IncrementalFilteringMode="StartsWith"
                                        TextFormatString="{1}" ClientInstanceName="GridGuideAdmin" OnInit="GridGuideAdmin_Init"
                                        KeyFieldName="ID">
                                        <Columns>
                                            <dx:GridViewDataTextColumn FieldName="CompanyCode" Caption="代码" />
                                            <dx:GridViewDataTextColumn FieldName="ShortName" Caption="管理员" />
                                        </Columns>
                                    </dx:ASPxGridLookup>
                                </EditItemTemplate>
                            </dx:GridViewDataTextColumn>
                        </Columns>
</dx:ASPxGridView>

二 绑定ASPxGridLookup数据,没有Order By SQ则取到的数据会乱跳(这个花了半天才调出来)
 protected void GridGuideAdmin_Init(object sender, EventArgs e)
        {
            EmployeeRule employeeRule = new EmployeeRule();
            ((ASPxGridLookup)sender).DataSource =  employeeRule.GetEmployeeDataTable();
        }
       
        public DataTable GetEmployeeDataTable()
        {
            string sql = @"select
                        PersonName as ShortName,
                        Sq as ID,ADM_EmployeeCd as CompanyCode
                        from V_Employee order by SQ"; 
            return dao.GetDataTable(sql); ;
        }
        
三、获得页面数据保存

 protected void gridDetail_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
    //判断是否包含导游数据,不包含则根据需求人数从报价中带入
    guideAssignOrderItemInfo.GuideSq = new Guid((sender as ASPxGridView).GetMasterRowKeyValue().ToString());
            guideAssignOrderItemInfo.GuideAdmin.ID = Guid.Parse(GetColumnLookUpValue(gridDetail, "GuideAdmin.Name", "GridGuideAdmin"));
    this.SetDetailParameter(sender as ASPxGridView, e.NewValues);

    this.guideAssignOrderItemInfoRule.Add(guideAssignOrderItemInfo);
    e.Cancel = true;
    (sender as ASPxGridView).CancelEdit();
    this.grid.Focus();
}


public string GetColumnLookUpValue(ASPxGridView grv, string strColumn, string strControlFieldName)
{//从下拉框得到Sq值
    ASPxGridLookup control = (ASPxGridLookup)grv.FindEditRowCellTemplateControl((GridViewDataColumn)grv.Columns[strColumn], strControlFieldName);
    if (control != null && control.Value != null)
    {
        return control.Value.ToString();
    }
    return Guid.Empty.ToString();
}

你可能感兴趣的:(DevExpress)