Devexpress:如何在同一栏中显示不同的编辑器

Devexpress: How to show different editors in the same column

Devexpress 有带送货地址的网格视图。

当我 select 第一行第一列的国家时,我希望它根据 selected 国家显示第二列的城市。

我希望根据我在第二行第一列中选​​择的国家/地区名称,将属于该国家/地区的城市显示在第二列中。

我在这个问题上尝试了很多,我看了很多文章,但我做不到。

当我编写如下代码时,当更改国家/地区名称时,最后一个 selected 国家/地区的城市出现在所有行中。我无法修复此错误。

private void gridView4_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
    if (e.Column.Caption == "country")
    {
                string sqlquery2 = @" SELECT [ObjID] as ObjID, [CityName] as CityName FROM [NPDB].[dbo].[YN_Cities] where [Active]=1 and [CountryID]=" + country + " order by [CityName] asc";
            SqlDataAdapter da2 = new SqlDataAdapter(sqlquery2, con);
            DataTable dt_iller = new DataTable();
            dt_iller.Clear();
            da2.Fill(dt_iller);

            RepositoryItemLookUpEdit illookup = new RepositoryItemLookUpEdit();
            illlookup.DataSource = dt_iller;
            illlookup.ValueMember = "ObjID";
            illlookup.DisplayMember = "CityName";
            illookup.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
            illlookup.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;
            illlookup.AutoSearchColumnIndex = 1;
            illlookup.AutoHeight = false;
            illlookup.DropDownRows = 10;
            illlookup.NullText = string.Empty;
            illlookup.PopulateColumns();

            foreach (LookUpColumnInfo col in illookup.Columns)
            {
                if (col.FieldName == "ObjID") { col.Visible = false; }
                if (col.FieldName == "CityName") { col.Caption = "City Name"; }
            }

            gridControl4.RepositoryItems.Add( illookup );
            gridView4.Columns["il"].ColumnEdit = illookup;
    }
}

我使用 PopupFilter 事件解决了这个问题。 我用所有可能的值(你的情况下的所有城市)填充编辑器,然后只显示相关的。

 public partial class MyForm : Form {
     this.InitializeComponent();

       /* don't filter, read all cities */
       var sqlquery2 = @" SELECT [ObjID] as ObjID, [CityName] as CityName FROM [NPDB].[dbo].[YN_Cities] where [Active]=1"; 
        SqlDataAdapter da2 = new SqlDataAdapter(sqlquery2, con);
        DataTable dt_iller = new DataTable();
        dt_iller.Clear();
        da2.Fill(dt_iller);

        RepositoryItemLookUpEdit illookup = new RepositoryItemLookUpEdit();
        illlookup.DataSource = dt_iller;
        illlookup.ValueMember = "ObjID";
        illlookup.DisplayMember = "CityName";
        illookup.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
        illlookup.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;
        illlookup.AutoSearchColumnIndex = 1;
        illlookup.AutoHeight = false;
        illlookup.DropDownRows = 10;
        illlookup.NullText = string.Empty;
        illlookup.PopulateColumns();

        gridControl4.RepositoryItems.Add( illookup );
        gridView4.Columns["il"].ColumnEdit = illookup;
 }


 private void illlookup_Properties_PopupFilter(object sender, DevExpress.XtraEditors.Controls.PopupFilterEventArgs e) {
    e.Criteria = DevExpress.Data.Filtering.CriteriaOperator.Parse("CountryID == '"     + gridView4.GetFocusedDataRow()["country"] + "'");
}

我用下面的代码解决了这个问题

private void gridView4_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
    if (e.Column.Caption == "il" && gridView4.RowCount > 1 && e.RowHandle >= 0)
    {
        DevExpress.XtraGrid.Views.Grid.GridView gv = sender as DevExpress.XtraGrid.Views.Grid.GridView;
        object ob = gv.GetRowCellValue(e.RowHandle, gv.Columns["ulke"]).ToString();
        string ulkem;

        if (ob == null) { return; } else { ulkem = ob.ToString(); }

        string sqlquery2 = @" SELECT [ObjID] as ObjID, [SehirAdi] as SehirAdi FROM [NPDB].[dbo].[YN_Sehirler] where [Aktif]=1 and [UlkeID]=" + ulkem + " order by [SehirAdi] asc";
        SqlDataAdapter da2 = new SqlDataAdapter(sqlquery2, con);

        DataTable dts = new DataTable();
        dts.Clear();
        da2.Fill(dts);

        RepositoryItemLookUpEdit illookup = new RepositoryItemLookUpEdit();
        illookup.DataSource = dts;
        illookup.ValueMember = "ObjID";
        illookup.DisplayMember = "SehirAdi";
        illookup.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
        illookup.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;
        illookup.AutoSearchColumnIndex = 1;
        illookup.AutoHeight = false;
        illookup.DropDownRows = 10;
        illookup.NullText = string.Empty;
        illookup.PopulateColumns();

        foreach (LookUpColumnInfo col in illookup.Columns)
        {
            if (col.FieldName == "ObjID") { col.Visible = false; }
            if (col.FieldName == "SehirAdi") { col.Caption = "il Adı"; }
        }

        e.RepositoryItem = illookup;
    }

    if (e.Column.Caption == "ilçe" && gridView4.RowCount > 1 && e.RowHandle >= 0)
    {
        DevExpress.XtraGrid.Views.Grid.GridView gv = sender as DevExpress.XtraGrid.Views.Grid.GridView;
        object ob = gv.GetRowCellValue(e.RowHandle, gv.Columns["ulke"]).ToString();
        string ulkem;

        if (ob == null) { return; } else { ulkem = ob.ToString(); }

        DevExpress.XtraGrid.Views.Grid.GridView gv2 = sender as DevExpress.XtraGrid.Views.Grid.GridView;
        object ob2 = gv2.GetRowCellValue(e.RowHandle, gv2.Columns["il"]).ToString();
        string ilim;

        string sqlquery2;

        if (ob.ToString() != "" && ob2.ToString() != "") 
        {
            ilim = ob2.ToString(); 
            sqlquery2 = @" SELECT [ObjID] as ObjID, [ilceAdi] as ilceAdi FROM [NPDB].[dbo].[YN_ilceler] where [Aktif]=1 and [UlkeID]=" + ulkem + " and [SehirID]=" + ilim + " order by [ilceAdi] asc"; 
        }
        else { return; }

        SqlDataAdapter da2 = new SqlDataAdapter(sqlquery2, con);

        DataTable dts2 = new DataTable();
        dts2.Clear();
        da2.Fill(dts2);

        RepositoryItemLookUpEdit ilcelookup = new RepositoryItemLookUpEdit();
        ilcelookup.DataSource = dts2;
        ilcelookup.ValueMember = "ObjID";
        ilcelookup.DisplayMember = "ilceAdi";
        ilcelookup.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
        ilcelookup.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;
        ilcelookup.AutoSearchColumnIndex = 1;
        ilcelookup.AutoHeight = false;
        ilcelookup.DropDownRows = 10;
        ilcelookup.NullText = string.Empty;
        ilcelookup.PopulateColumns();

        foreach (LookUpColumnInfo col in ilcelookup.Columns)
        {
            if (col.FieldName == "ObjID") { col.Visible = false; }
            if (col.FieldName == "ilceAdi") { col.Caption = "ilçe Adı"; }
        }

        e.RepositoryItem = ilcelookup;
    }
}