DevExpress使用组合框列在Gridview上更新

时间:2013-07-31 13:33:48

标签: c# asp.net .net entity-framework devexpress

  <dx:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server" 
         KeyFieldName="ProjectID" Width="100%" 
        EnableRowsCache="False"  OnRowUpdating="grid_RowUpdating" AutoGenerateColumns="False" OnCellEditorInitialize="grid_CellEditorInitialize">
        <Settings ShowGroupPanel="True" />
        <SettingsEditing Mode="Inline" />
        <Columns>
            <dx:GridViewCommandColumn VisibleIndex="0">
                <EditButton Visible="true" />
            </dx:GridViewCommandColumn>
            <dx:GridViewDataTextColumn FieldName="ProjectID" ReadOnly="True" VisibleIndex="0"/>

            <dx:GridViewDataColumn FieldName="ProjectName" VisibleIndex="1" />
            <dx:GridViewDataColumn FieldName="ProjectInfo" VisibleIndex="2" />
            <dx:GridViewDataComboBoxColumn   Caption="Project Manager"  FieldName="UserID" VisibleIndex="3">
               <PropertiesComboBox   TextField="DomainName" ValueField="UserID">
                </PropertiesComboBox>
            </dx:GridViewDataComboBoxColumn>         
        </Columns>
    </dx:ASPxGridView>

我上面的aspx文件显示来自连接表User和Project的数据。用户具有字段UserID和DomainName,Project具有ProjectManagerID(USERID上的FK)ProjectInfo和ProjectName 这就是我填充数据的方式

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        //Entity framework query Project.Include("User").ToList();
        grid.DataSource = BUS.Operations.EntityOperations.ProjectOperations.SelectAllProjects();
        grid.DataBind();
    }
}
//Upon edit comboboxes are populated
protected void grid_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
{
    if (!grid.IsEditing || e.Column.FieldName != "User.DomainName") return;
    if (e.KeyValue == DBNull.Value || e.KeyValue == null) return;
    object val = grid.GetRowValuesByKeyValue(e.KeyValue, "ProjectID");
    if (val == DBNull.Value) return;
    int country = (int)val;
    ASPxComboBox combo = e.Editor as ASPxComboBox;
    FillCityCombo(combo);
    combo.Callback += new CallbackEventHandlerBase(cmbCity_OnCallback);
}
    protected void FillCityCombo(ASPxComboBox cmb)
    {
        cmb.DataSource= BUS.Operations.EntityOperations.UserOperations.SelectAllProjectManagers();     
        cmb.DataBindItems();
    }

这些是回调和rowupdating函数

void cmbCity_OnCallback(object source, CallbackEventArgsBase e)
{
    FillCityCombo(source as ASPxComboBox);
}
protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{

    ASPxGridView gridView = (ASPxGridView)sender;
    int ProjectID = (int)e.Keys[gridView.KeyFieldName];
    P p = new P();
    p.ProjectName = e.NewValues["ProjectName"].ToString();
    p.ProjectInfo = e.NewValues["ProjectInfo"].ToString();
    p.ProjectID = ProjectID; 
    //User.DomainName should be inputted as int convertable string  
    p.ProjectManagerID = Int32.Parse(((string)e.NewValues["User.DomainName"]));
    BUS.Operations.EntityOperations.ProjectOperations.UpdateProject(p);
    gridView.CancelEdit();
    e.Cancel = true;
}

现在有两个主要问题。

  1. 首先和次要的是从组合框中选择另一个项目管理器并单击更新我虽然数据库已更改,但在页面刷新后可以看到效果,但我看不到Web视图中的更改。

  2. 第二个主要是如果我不接触组合框并更改项目名称和单击更新等其他字段,我会在rowupdate方法上获得一个字符串,该字符串无法转换为int。但是,如果我从组合框中选择并更新一个int,那么UserID就像我预期的那样。

  3. 这是我在devexpress上的第一次试验,可能这段代码有很多错误和不必要的复杂性。我需要你的帮助以获得更好的工作代码

0 个答案:

没有答案