如何将自动完成框绑定到数据库表列?

发布于 2024-11-05 02:03:53 字数 314 浏览 0 评论 0原文

我目前正在使用 telerik 控件创建一个 Gridview,它显示来自 sql 数据库的数据,这些数据是通过 wcf ria 中使用的域数据源显示的。(ADO.net 实体模型等) 我想在 radgrid 上方添加一个自动完成框,我可以在其中输入名称,并且还列出其他可匹配的条目。

当我单击该条目时,radgrid 可能会显示包含该名称的整行。

我正在使用 silverlight 4、wcf ria、telerik 控件。

请在 xaml 和 xaml.cs 中提供示例编码思路。

我尝试访问 Telerik 演示,但它们没有在我的系统上运行。

i am currently creating a Gridview using telerik control which displays data from the sql database which i displays through domain datasource used in wcf ria.(ADO.net entity model etc)
i want to add an autocomplete box above my radgrid where i type an name and other matchable entries are also listed.

when i click on the entry then radgrid may display whole row containing that name.

i am using silverlight 4,wcf ria,telerik controls.

please provide a sample coding idea in xaml and xaml.cs.

i tries to access telerik demos but they are not running on my system.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2024-11-12 02:03:53

举个例子......假设您有一个客户列表,您希望在自动完成框中显示他们的姓名。此外,您的网格应显示所有客户,并且当在“自动完成”框中选择名称时,将显示网格的选定项目。

您需要做的是绑定 RadGridView 的 SelectedItem 属性 &自动完成框。我要做的是将 AutoCompleteBox 绑定到名为 SelectedName 的属性,如下所示:

<input:AutoCompleteBox ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedName, Mode=TwoWay}" />

强调“Mode=TwoWay” - 这将提醒您背后的代码 UI 已更改。

在后面的代码中,您将创建如下属性:

private string selectedName;
public string SelectedName
{
    get { return selectedName; }
    set 
    {
        if (value != null) 
        {
            var query = (from c in CustomersList
                        where (c.Name == value)
                        select c).FirstOrDefault();
            SelectedCustomer = (Customer)query;
            selectedName = value;
        }
    }
}

请注意,当您设置 SelectedName 时,您如何使用 LINQ 来确定选择了哪些客户。这里的一个陷阱是,如果列表中有多个名称......此代码仅选择第一个。如果这是一个问题,您可能应该重新考虑您的体系结构。

然后,对于您的网格,您将像这样绑定 SelectedItem:

<telerik:RadGridView 
   ....
   SelectedItem={Binding SelectedCustomer, Mode=TwoWay"}
   ....
</telerik:RadGridView>

在后面的代码中,您将创建此属性:

private Customers selectedCustomer;
public Customers SelectedCustomer 
{
    get { return selectedCustomer; }
    set { 
        selectedCustomer = value;
        MyGridView.SelectedItem = selectedCustomer;
    }
}

类似的东西应该可以帮助您开始。

SS

As an example... say you have a list of Customers, of which you want to display their names in your AutoComplete box. Further, your Grid should display all customers, and when a Name is selected in the AutoComplete box, the Selected item of the grid displays.

What you need to do is bind the SelectedItem property of the RadGridView & AutoCompleteBox. What I would do is bind the AutoCompleteBox to a property named SelectedName, like so:

<input:AutoCompleteBox ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedName, Mode=TwoWay}" />

Emphasis on the 'Mode=TwoWay' - this is what will alert your code behind that the UI has changed.

In your code behind, you would create properties like this:

private string selectedName;
public string SelectedName
{
    get { return selectedName; }
    set 
    {
        if (value != null) 
        {
            var query = (from c in CustomersList
                        where (c.Name == value)
                        select c).FirstOrDefault();
            SelectedCustomer = (Customer)query;
            selectedName = value;
        }
    }
}

Notice how, when you're setting the SelectedName, you're using LINQ to determine which of the customers were selected. One pitfall here would be if you have multiple names in a list... this code only selects the first. If this is an issue, you probably should rethink your architecture..

Then for your grid, you would bind the SelectedItem like so:

<telerik:RadGridView 
   ....
   SelectedItem={Binding SelectedCustomer, Mode=TwoWay"}
   ....
</telerik:RadGridView>

In your code behind, you'd create this property:

private Customers selectedCustomer;
public Customers SelectedCustomer 
{
    get { return selectedCustomer; }
    set { 
        selectedCustomer = value;
        MyGridView.SelectedItem = selectedCustomer;
    }
}

Something like that should get you started.

SS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文