基于文本非值的过滤RadGrid列

时间:2016-05-01 18:22:53

标签: c# winforms telerik

我有一个文本框,我管理其文本更改事件以过滤RadGrid:

private void txtJob_TextChanging(object sender, TextChangingEventArgs e)
{
    this.gridCustomers.Columns["JobColumn"].FilterDescriptor = new FilterDescriptor
    {
        Operator = FilterOperator.Contains,
        Value = txtJob.Text
    };
}

我使用CellFormatting事件更改JobColumn文本:

private void gridCustomers_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.Column.Name == "JobColumn")
        e.CellElement.Text = db.tblJobs.First(x => x.JobID == Convert.ToInt32(e.Row.Cells[9].Value.ToString())).JobName;
}

我用JobColumn中的作业名替换作业ID,在我过滤RadGrid的文本框中我搜索RadGrid中可见的作业名称,但它会根据作业ID进行过滤这是替换前的默认值。 那么如何根据文本而不是值来过滤RadGrid列?

有关详细信息,请将此类表格绑定到我的girdview:

int JobID
nvarchar(10) Name
nvarchar(100) Address
.
.
.

我有一张名为乔布斯的表:

int JobID
nvarchar(30) JobName
.
.
.

我需要从表1中获取JobID,并在数据绑定(单元格格式化)中将Job替换为Job表中的JobName。 为什么我不选择新的并加入两张桌子?因为在这种情况下我没有可以轻松编辑的网格视图,我必须使用Virtual Gird,这不是我的目标。

1 个答案:

答案 0 :(得分:1)

你正试图攻击系统。在您的情况下,正确的方法是使用GridVieWComboBoxColumn,可以绑定,并指定DisplayMember和ValueMember。它还具有FilterMode属性,以确定用于过滤的字段。

了解更多GridViewComboBoxColumn | Telerik UI for WinForms Documentation

<强>更新

以下是开始使用的示例

 protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        DataTable mainTable = new DataTable();
        mainTable.Columns.Add("JobID", typeof(int));
        mainTable.Columns.Add("Name");
        mainTable.Columns.Add("Address");

        Random rand = new Random();
        for (int i = 0; i < 10; i++)
        {
            mainTable.Rows.Add(rand.Next(1,4), "Name " + i, "Address " + i);
        }

        DataTable jobsTable = new DataTable();
        jobsTable.Columns.Add("JobID", typeof(int));
        jobsTable.Columns.Add("JobName");

        jobsTable.Rows.Add(1, "ABC ");
        jobsTable.Rows.Add(2, "DFG");
        jobsTable.Rows.Add(3, "XCV");

        radGridView1 = new RadGridView() { Dock = DockStyle.Fill, AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill };
        this.Controls.Add(radGridView1);
        radGridView1.EnableFiltering = true;

        radGridView1.DataSource = mainTable; //this will create all columns

        radGridView1.Columns.Remove(radGridView1.Columns["JobId"]);

        GridViewComboBoxColumn comboCol = new GridViewComboBoxColumn();
        comboCol.DataSource = jobsTable;
        comboCol.FieldName = "JobID"; //the name of the field in the main table to look for
        comboCol.DisplayMember = "JobName"; //you want to see job names not ids
        comboCol.ValueMember = "JobID";
        comboCol.FilteringMode = GridViewFilteringMode.DisplayMember;
        radGridView1.Columns.Insert(0, comboCol);
    }

    private void radButton1_Click(object sender, EventArgs e)
    {
        radGridView1.Columns["JobID"].FilterDescriptor = new FilterDescriptor
        {
            Operator = FilterOperator.Contains,
            Value = "B"
        };
    }