1

我有一个有两列的网格,第一列有一个查找编辑。我可以填充网格和查找编辑,但我的问题是我不知道如何根据数据库设置选定的文本?

这是我的网格

这是我填充网格和查找的方法

private void fill_budget_settings(){
        fill_tanim_lookupedit(); 
        gc.DataSource = dataContext.getObjectList("RN_BUDGET_SETTINGS", "*", 0, "", "", 0, 0);
        gv.BestFitColumns();
    }

    private void fill_tanim_lookupedit()
    {
        int cnt = gv.DataRowCount;

        lookUpEditTanim = new RepositoryItemLookUpEdit();
        lookUpEditTanim.DataSource = dataContext.getObjectList("RN_BUDGET_SETTINGS_VARIABLES", "*", 0, "", "", 0, 0);
        lookUpEditTanim.ValueMember = "VARIABLE";
        lookUpEditTanim.DisplayMember = "VARIABLE";

        gv.Columns[0].ColumnEdit = lookUpEditTanim;
    }

当表单被加载时,网格被填充,lookupedits 被填充但是lookupedits 没有根据数据库所需的数据被选中。我怎样才能做到这一点?

4

3 回答 3

1

如果要根据单元格值显示编辑器值,则ValueMember属性的指定列必须包含当前单元格中的值。

例如:
在指定的屏幕截图中,Tanm Column 有变量 id 并且您想在列中显示变量名称,那么ValueMember字段应该是 ID 并且DisplayMember应该是Variable

示例代码片段:

LookUpEdit ledMyControl;
RepositoryItemLookUpEdit _myRepositoryLookup;
RepositoryItemButtonEdit rbtnEdit;
GridControl grid;
GridView view;
GridView detailView;
GridLevelNode detailNode;
List<Category> categories = new List<Category>();
public GridRepositoryItemTest()
{
    InitializeComponent();
    CreateLookupEdit();
    InitializeGrid();
    SetRepositoryLoopEdit();
    InitializeAndAddColumnsToViews();

    InitializeAndBindDataSource();

    detailView.Columns["Category"].ColumnEdit = _myRepositoryLookup;            
}

private void CreateLookupEdit()
{
    ledMyControl = new LookUpEdit();
    ledMyControl.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Key"));
    ledMyControl.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Value"));

    this.Controls.Add(ledMyControl);
    Dictionary<string, string> dic = new Dictionary<string, string>();
    dic.Add("Test", "1");
    dic.Add("Test2", "2");
    dic.Add("Test3", "3");
    dic.Add("Test4", "4");
    dic.Add("Test5", "5");
    dic.Add("Test6", "6");
    dic.Add("Test7", "7");
    dic.Add("Test8", "8");
    dic.Add("Test9", "9");
    dic.Add("Test10", "10"); ledMyControl.Properties.DisplayMember = "Value";
    ledMyControl.Properties.ValueMember = "Key";
    ledMyControl.Properties.DataSource = dic.ToList();

    ledMyControl.Properties.Columns[0].Visible = false;
}

private void InitializeGrid()
{
    grid = new GridControl();
    view = new GridView(grid);
    detailView = new GridView(grid);
    detailNode = grid.LevelTree.Nodes.Add("Books", detailView);
    grid.Dock = DockStyle.Fill;
    this.Controls.Add(grid);
    detailView.ValidateRow += new DevExpress.XtraGrid.Views.Base.ValidateRowEventHandler(detailView_ValidateRow);

}

void detailView_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
{
    MessageBox.Show("It's working");
}        

private void InitializeAndAddColumnsToViews()
{
    if (view != null && detailView != null)
    {
        view.Columns.AddField("CategoryID").VisibleIndex = 0;
        detailView.Columns.AddField("ID").VisibleIndex = 0;
        detailView.Columns.AddField("Name").VisibleIndex = 1;
        detailView.Columns.AddField("Category").VisibleIndex = 2;                    
    }
}

private void InitializeAndBindDataSource()
{
    CreateCategories();
    BindingList<BookDetail> bookDetails = new BindingList<BookDetail>();

    BookDetail bookDetail = null;
    for (int j = 0; j < 5; j++)
    {
        bookDetail = new BookDetail { CategoryID = categories[j].ID };
        for (int i = 0; i < 5; i++)
        {
            bookDetail.Books.Add(new Book
            {
                ID = 1,
                Name = "Book - " + (i + 1),
                Category = categories[j].ID
            });
        }
        bookDetails.Add(bookDetail);
    }
    grid.DataSource = bookDetails;
}

private void CreateCategories()
{
    for (int i = 1; i <= 10; i++)
    {
        categories.Add(new Category
        {
            ID = i,
            Name = "Category - " + i
        });
    }
}

private void SetRepositoryLoopEdit()
{
    //rbtnEdit.Click += new EventHandler(rbtnEdit_Click);
    //rbtnEdit.ButtonClick += new DevExpress.XtraEditors.Controls.ButtonPressedEventHandler(rbtnEdit_ButtonClick);
    _myRepositoryLookup = new RepositoryItemLookUpEdit();
    _myRepositoryLookup.Name = "redCategories";
    _myRepositoryLookup.DataSource = categories;
    _myRepositoryLookup.ValueMember = "ID";
    _myRepositoryLookup.DisplayMember = "Name";
    grid.RepositoryItems.Add(_myRepositoryLookup);         
    _myRepositoryLookup.EditValueChanged += new EventHandler(_myRepositoryLookup_EditValueChanged);               

}

void rbtnEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
    throw new NotImplementedException();
}

void rbtnEdit_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

void _myRepositoryLookup_EditValueChanged(object sender, EventArgs e)
{
    //your code here
}

如果您没有按照上述方式进行操作,并且希望根据列单元格获取值。那么您必须查看Inplace Editors Overview并检查See Also页面中的部分,您将获得一个Assigning Editors to Individual Cells

例子:

using DevExpress.XtraGrid.Views.Grid;

private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) {
   if (e.Column.FieldName == "FieldName") return;
   GridView gv = sender as GridView;
   string fieldName = gv.GetRowCellValue(e.RowHandle, gv.Columns["FieldName"]).ToString();
   switch (fieldName) {
      case "Population":
         e.RepositoryItem = repositoryItemSpinEdit1;
         break;
      case "Country":
         e.RepositoryItem = repositoryItemComboBox1;
         break;
      case "Capital":
         e.RepositoryItem = repositoryItemCheckEdit1;
         break;
   }           
}
于 2013-09-11T08:19:01.700 回答
0

在文本下方的块内插入表单加载

lookUpEditTanim.EditValue = Yours lookUpEdit ID columns items name, which you are binding from the DB;
于 2019-10-22T11:35:18.107 回答
0

在价值的帮助下设置

lookUpEditTanim.EditValue ="2"

在文本的帮助下设置

lookUpEditTanim.EditValue=lookUpEditTanim.Properties.GetKeyValueByDisplayText("text_of_value_2");
于 2020-03-27T12:25:26.770 回答