Search code examples
sql-servergridviewdevexpresseditorlookup

Devexpress: How to show different editors in the same column


Devexpress has gridview with shipping addresses.

When I select the country in the first column in the first row, I want it to bring up the cities in the second column according to the selected country.

I want the cities belonging to that country to be shown in the second column according to the country name I chose in the first column of the second line.

I tried a lot on the subject, I read many articles, but I could not do it.

When I write a code like the one below, when the country name is changed, the cities of the last selected country appear on all lines. I couldn't fix this error.

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

Solution

  • I solved this problem with the following code

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