5

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>
Rob
  • 2,363
  • 7
  • 36
  • 54

2 Answers2

2

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>
Rob
  • 2,363
  • 7
  • 36
  • 54
0

First, you should use the GridView_RowDataBoundEvent which will fire when data are bounded to the gridview, put your code in it. Second, to be able to set the value in the ComboBox you should do the following:

//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;
yahya kh
  • 751
  • 3
  • 11
  • 23
  • Good guess, but this is a Aspx combobox which is generated when the grid is editted. So it's not a dropdownlist. See the new ASPX code. I'm having a hard time figuring out when exactly that control is generated, and how to actually access the control. – Rob May 23 '12 at 15:50