【问题标题】:How to programmatically select value in ASPxComboBox in ASpxGridView如何以编程方式在 ASPxGridView 中的 ASPxComboBox 中选择值
【发布时间】:2012-05-20 15:08:17
【问题描述】:

我需要使用 DevExpress ASPxGridView。我有一个数据源对象,它返回重要的两列,ObjectType 和 ObjectID。 ObjectType 可以是 Patient 或 Physician。 ObjectID 是一个 int 值,给出了患者或医生的 ID。希望这是有道理的。 ObjectID 由 Patient 表或 Physician 表选择,它们是缩进表,因此我无法以任何方式加入它们。

表结构如下:

对象表:ObjectType varchar ("Physician" or "Patient"), ObjectID int

Dogs 表 ID int,名称 varchar

Cats 表 ID int,名称 varchar

我已经能够通过组合框编写适当的 objectType,并使用由数据源填充的两个控件 cbPatient 和 cbPhysician 编写 ObjectID。

我想不通的是,当我编辑 ASPxGridView 时,如何在 cbPatient 或 cbPhysician 中显示对象值。例如,如果 ObjectType 是 Cats 并且 ObjectID 是 1,那么我想在 cbCats 中显示与 ID 为 1 对应的名称。

这就是代码的外观。现在,无论出于何种原因,在某些情况下,所选值在某些时候变为空白。我不确定要在什么事件中运行此代码。

protected void grid_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
{
    if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.EditForm) return;
    if (grid.IsNewRowEditing || grid.IsEditing)
    {
        int val = (int)grid.GetRowValues(grid.EditingRowVisibleIndex, "ObjectID" );

        ASPxComboBox cbPatient = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(grid.Columns["PatientID"] as GridViewDataComboBoxColumn, "cbPatient"));
        if (val != 0)
        {
            string objectID = grid.GetRowValues(grid.EditingRowVisibleIndex, "ObjectID").ToString();

            if (cbPatient.Items.Count > 0)
            {
                cbPatient.Items[1].Selected = true;
            }
            else
            {
                cbPatient.DataSource = dsPatName;
                cbPatient.DataBindItems();
                if (cbPatient.Items.Count > 0)
                    cbPatient.Items[1].Selected = true;
            }

        }
    }

}

这是 ASPX 代码。

</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataComboBoxColumn Caption="Patient Name" FieldName="PatientID"  Name="PatName"  Visible="false">
    <PropertiesComboBox TextField="Name" ValueField="ID" ValueType="System.Int32"></PropertiesComboBox>
    <EditItemTemplate>
        <dx:ASPxComboBox ID="cbPatient" runat="server"  TextField="Name" ValueField="ID"
        Value='<%# Bind("PatientID") %>' AutoPostBack="false" ValueType="System.Int32" > </dx:ASPxComboBox>
    </EditItemTemplate>
</dx:GridViewDataComboBoxColumn>

【问题讨论】:

    标签: asp.net aspxgridview


    【解决方案1】:

    我终于发现组合框会在数据绑定之后触发 DataBound 事件,该事件发生在 RowEditting 事件(或任何它被调用的事件)之后。我找到的最佳解决方案是这段代码。也许有一个更优雅的解决方案,但这有效。 session 变量是为了避免在执行行更新时触发代码(是的,它也会触发,不要问我为什么)。

    protected void cbPatient_DataBound(object sender, EventArgs e)
    {
        object rightsindex = grid.GetRowValues(grid.EditingRowVisibleIndex, "Rights");
        if (rightsindex == null) return;
        int rights = Int32.Parse(grid.GetRowValues(grid.EditingRowVisibleIndex, "Rights").ToString());
        object objectID = grid.GetRowValues(grid.EditingRowVisibleIndex, "ObjectID");
    
        ASPxComboBox cbPatient = ((ASPxComboBox)grid.FindEditRowCellTemplateControl(grid.Columns["PatientID"] as GridViewDataComboBoxColumn, "cbPatient"));
        if (cbPatient != null && cbPatient.Items.Count > 1)
        {
    
            if (rights == 8)
            {
                ListEditItem pt = cbPatient.Items.FindByValue(objectID);
                if (pt != null && Session["PatientID"] == null)
                {
                    cbPatient.Items[pt.Index].Selected = true;
                    Session["PatientID"] = (Int32)pt.Value;
    
                }
            }
                //cbPatient.Items[1].Selected = true;
        }
    }
    

    在 ASP.NET 中

    <dx:GridViewDataComboBoxColumn Caption="Patient Name" FieldName="PatientID"  Name="PatName"  Visible="false">
        <PropertiesComboBox DataSourceID="dsPatName" TextField="Name" ValueField="ID" ValueType="System.Int32">
        </PropertiesComboBox>
        <EditItemTemplate>
            <dx:ASPxComboBox ID="cbPatient" runat="server" DataSourceID="dsPatName" TextField="Name" ValueField="ID"
            Value='<%# Bind("PatientID") %>' AutoPostBack="false" ValueType="System.Int32" ondatabound="cbPatient_DataBound" > </dx:ASPxComboBox>
        </EditItemTemplate>
    </dx:GridViewDataComboBoxColumn>
    

    【讨论】:

      【解决方案2】:

      首先,您应该使用 GridView_RowDataBoundEvent,当数据绑定到 gridview 时将触发该事件,将您的代码放入其中。 其次,为了能够在 ComboBox 中设置值,您应该执行以下操作:

      //Ensure that the Dropdownlist dosn't have any value selected, otherwise it will give  an exception
      DropDownList_Country.ClearSelection();
      //You can use either FindByValue or FindByText
      DropDownList_Country.Items.FindByValue("").Selected = true;
      

      【讨论】:

      • 猜得好,但这是在编辑网格时生成的 Aspx 组合框。所以它不是一个下拉列表。请参阅新的 ASPX 代码。我很难弄清楚该控件的确切生成时间,以及如何实际访问该控件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多