Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically select value in ASPxComboBox in ASpxGridView

I am required to use the DevExpress ASPxGridView. I have a datasource Object which returns two columns of importance, ObjectType and ObjectID. ObjectType can be Patient or or Physician. ObjectID is a int value giving the ID of the Patient or Physician. Hopefully this makes sense. The ObjectID is selected by either the Patient table or the Physician table, they are indendent tables so I can't join them in any way.

The table structure looks like this:

Object table: ObjectType varchar ("Physician" or "Patient"), ObjectID int

Dogs Table ID int, Name varchar

Cats table ID int, Name varchar

I have been able to write the appropriate objectType by a combobox, and the ObjectID by using two controls, cbPatient and cbPhysician, which are populated by datasources.

What I can't figure out is when I edit the ASPxGridView, how can I show in cbPatient or cbPhysician the object value. For example if ObjectType is Cats and ObjectID is 1 then I want to show in cbCats the name that corresponds with ID of 1.

This is how the code looks. Right now, for whatever reason the selected value goes blank at some point, in some event. I'm not sure what event to run this code in.

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;
            }

        }
    }

}

This is the ASPX code.

</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>
like image 324
Rob Avatar asked Nov 04 '22 23:11

Rob


1 Answers

I finally figured out that the combobox fires a DataBound event after databinding, which occurs after the RowEditting event (or whatever it is called). The best solution I found was this code. Maybe there is a more elegant solution but this works. The session variable is there to avoid having the code fire when doing the row update (yeah it fires then too, don't ask me why).

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;
    }
}

And in the 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>
like image 88
Rob Avatar answered Nov 15 '22 07:11

Rob