【问题标题】:How to retrieve and bind data to ListBox from SQLCe Db如何从 SQLCe Db 检索数据并将数据绑定到 ListBox
【发布时间】:2011-07-22 11:24:32
【问题描述】:

我已使用以下代码将数据插入到 SqlCe 数据库中:

使用(DBContacts 上下文 = 新 DBContacts(ConnectionString)) { TblContacts tblCtc = new TblContacts(); tblCtc.FirstName = txtBoxFirstNm.Text; tblCtc.LastName = txtBoxLastNm.Text; tblCtc.Mobile1 = txtMobile1.Text; tblCtc.Email1 = txtEmail1.Text; context.TblContacts.InsertOnSubmit(tblCtc); context.SubmitChanges(); MessageBox.Show("插入成功。"); }

问题:

1) 如何使用 Linq To SQL 中的 select 并显示检索到的数据?

使用(DBContacts 上下文 = 新 DBContacts(ConnectionString)) { IEnumerable ctc = from c in context.TblContacts select c; // 获取结果并显示什么? // txtBlkFirstname.Text = ctc.FirstName ?? // txtBlkLastname.Text = ctc.LastName ?? ...... }

1a) 使用哪个? IQueryable 还是 IEnumerable?

1b) Select 语句返回什么?数据返回如何处理?

1c) 如何从 Iqueryable 或 IEnumerable 中获取数据并将值分配给 TextBlock 控件?

-----如何将数据绑定到ListBox

【问题讨论】:

  • 您好,我可以再获取一些代码,以便我可以帮助您
  • 如果你能告诉我我错过了哪一部分,我将不胜感激。我被困在第二部分:检索数据。

标签: linq-to-sql windows-phone-7 sql-server-ce


【解决方案1】:
public List<TblContacts> GetAllContact()
    {
        using (DBContacts context = new DBContacts(ConnectionString))
        var contacts = from m in context.TblContacts select m;
        return contacts.Take(100).ToList();
    }

我在 WCF 服务中使用的类似的东西,我希望你的应用程序也可以使用,

尝试将列表框源绑定到 GetAllContacts(),然后在该列表框内将其数据绑定到各个字段

       <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel Name="contentP" Opacity="1">
            <ListBox Height="623" Margin="-20,0,0,0" Name="listBox1" Width="473">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="132">
                            <Image Source="{Binding ImageSource}" Height="75" Width="75" VerticalAlignment="Top" Margin="15,10,10,0"/>
                            <StackPanel Width="380">
                                <ContentControl Margin="0,10,0,0" HorizontalAlignment="Left">
                                    <HyperlinkButton HorizontalAlignment="Left" Style="{StaticResource HyperlinkButtonStyle}" Content="{Binding Title}" Foreground="#FFC8AB14" NavigateUri="{Binding Link}" TargetName="_blank"/>
                             </ContentControl>
                             <TextBlock Margin="10,10,30,0" Text="{Binding Message}" Foreground="{StaticResource PhoneAccentBrush}" TextWrapping="Wrap" FontSize="20" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>           
    </Grid> 

在您的代码中,您可以将“listbox1”ItemsSource 设置为您的 DBQuery 的源,但不确定在您的情况下您会如何做,就像我使用 WCF 服务做的那样,但它有点像这个

listbox1.ItemsSource = (result of your query ("e.Result;");

【讨论】:

  • 你能告诉我如何将数据绑定到带有 TextBlock 的 ListBox 吗?像 .... 在 textBlock 中绑定需要包含哪些内容?谢谢
猜你喜欢
  • 2023-03-28
  • 2014-09-28
  • 2016-01-02
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多