当我使用导航按钮时,组合框wpf数据不显示

时间:2013-07-01 13:22:54

标签: c# wpf sql-server-2008

我创建了一个非常简单的数据库应用程序,因为我对WPF很新。最终我能够在winform中使用导航按钮(不使用数据绑定)。我遇到了一个问题,我希望这是一个非常简单的问题,但我又一次不知道?

问题

当我使用导航按钮时,数据没有显示在combox中,但似乎文本框没有问题,它基本上显示为空。我有一个listview所以当CUD操作时显示整个记录。我错过了什么吗?

我创建的部分类和showdata方法

 public partial class MainWindow : Window
 {
     SqlConnection mconn;
     int MaxRows = 0;
     int rno;
     DataSet ds;

     private void MaxRow()
     {
         MaxRows = ds.Tables[0].Rows.Count;
     }
}

public void ShowData()
    {
        try
        {
            //pardon me this looks rather tacty

            SqlCommand comm = new SqlCommand("Select * from tblTableB", mconn);
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(dt);
            listView1.DataContext = dt.DefaultView;

            da = new SqlDataAdapter("select * from tblTableB", mconn);
            ds = new DataSet();
            da.Fill(ds, "TestDatabaseB");
            da.Update(ds, "TestDatabaseB");
            this.MaxRow();

            txtID.Text = ds.Tables[0].Rows[rno][0].ToString();
            txtName.Text = ds.Tables[0].Rows[rno][1].ToString();
            cBHealthDetails.Text = ds.Tables[0].Rows[rno][2].ToString(); //data not showing 
            when  navigation buttons are used. CB is showning empty

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }
    }

导航方法示例

private void PreRec_Click(object sender, RoutedEventArgs e)
    {
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (rno > 0)
            {
                rno--;
                ShowData();
            }
        }
    }

    private void NxtRec_Click(object sender, RoutedEventArgs e)
    {
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (rno < ds.Tables[0].Rows.Count - 1)
            {
                rno++;
                ShowData();
            }
        }
    }

XAML代码

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="381" Width="406" Loaded="Window_Loaded">
<Grid>
    <Grid Height="342" HorizontalAlignment="Left"  Name="grid1" VerticalAlignment="Top" Width="384">
        <ListView Height="134" HorizontalAlignment="Left" Name="listView1"  ItemsSource="{Binding}" VerticalAlignment="Top" Width="384">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=ID}"></GridViewColumn>
                    <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding Path=Name}"></GridViewColumn>
                    <GridViewColumn Header="Health Details" DisplayMemberBinding="{Binding Path=HealthDetails}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        <Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="7,181,0,0" Name="label1" VerticalAlignment="Top" />
        <Label Content="Health Details" Height="28" HorizontalAlignment="Left" Margin="7,215,0,0" Name="label2" VerticalAlignment="Top" />

        <TextBox Height="23" HorizontalAlignment="Left" Margin="102,152,0,0" Name="txtID" VerticalAlignment="Top" Width="120" DataContext="{Binding ElementName=listView1,Path=SelectedItem}" Text="{Binding Path=ID}" IsReadOnly="True" Background="#26000000"></TextBox>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="102,186,0,0" Name="txtName" VerticalAlignment="Top" Width="243" DataContext="{Binding ElementName=listView1,Path=SelectedItem}" Text="{Binding Path=Name}" />
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="102,220,0,0" Name="cBHealthDetails" VerticalAlignment="Top" Width="120" DataContext="{Binding ElementName=listView1,Path=SelectedItem}" Text="{Binding Path=HealthDetails}">
            <ComboBoxItem Content="Yes" />
            <ComboBoxItem Content="No" />
        </ComboBox>
        <Button Content="New" Height="23" HorizontalAlignment="Left" Margin="25,262,0,0" Name="btnNew" VerticalAlignment="Top" Width="75" Click="btnNew_Click" />
        <Button Content="Insert" Height="23" HorizontalAlignment="Left" Margin="111,262,0,0" Name="btnInsert" VerticalAlignment="Top" Width="75" Click="btnInsert_Click"></Button>
        <Button Content="Update" Height="23" HorizontalAlignment="Left" Margin="197,262,0,0" Name="btnUpdate" VerticalAlignment="Top" Width="75" />
        <Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="280,262,0,0" Name="btnDelete" VerticalAlignment="Top" Width="75" />
        <Button Content="&lt;&lt;" Height="23" HorizontalAlignment="Left" Margin="25,301,0,0" Name="FirstRec" VerticalAlignment="Top" Width="75" Click="FirstRec_Click" />
        <Button Content="&lt;" Height="23" HorizontalAlignment="Left" Margin="111,301,0,0" Name="PreRec" VerticalAlignment="Top" Width="75" Click="PreRec_Click" />
        <Button Content="&gt;" Height="23" HorizontalAlignment="Right" Margin="0,301,112,0" Name="NxtRec" VerticalAlignment="Top" Width="75" Click="NxtRec_Click" />
        <Button Content="&gt;&gt;" Height="23" HorizontalAlignment="Left" Margin="280,301,0,0" Name="LastRec" VerticalAlignment="Top" Width="75" Click="LastRec_Click" />
        <Label Content="ID" Height="28" HorizontalAlignment="Left" Margin="11,147,0,0" Name="label3" VerticalAlignment="Top" />

    </Grid>
</Grid>

非常感谢

更新1

Windows已加载

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
          ShowData();
    }


    void ShowData()
    {
        try
        {
            SqlCommand comm = new SqlCommand("Select * from tblTableB", mconn);
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(dt);
            listView1.DataContext = dt.DefaultView;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }
    }

    private void btnShowAll_Click(object sender, RoutedEventArgs e)
    {
        da = new SqlDataAdapter("select * from tblTableB", mconn);
        ds = new DataSet();
        da.Fill(ds, "tblTableB");
        da.Update(ds, "tblTableB");
        this.MaxRow();

        txtID.Text = ds.Tables[0].Rows[rno][0].ToString(); //rno["ID"]
        txtName.Text = ds.Tables[0].Rows[rno][1].ToString(); //rno["Name"] 
        cBHealthDetails.Text = ds.Tables[0].Rows[rno][2].ToString(); //also tried by replacing 
        //rno["HealthDetails"].ToString();
    }

XAML代码

 <TextBox Height="23" HorizontalAlignment="Left" Margin="102,152,0,0" Name="txtID" VerticalAlignment="Top" Width="120" IsReadOnly="True" Background="#26000000"></TextBox>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="102,186,0,0" Name="txtName" VerticalAlignment="Top" Width="243" />
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="102,220,0,0" Name="cBHealthDetails" VerticalAlignment="Top" Width="120">
            <ComboBoxItem Content="Yes"></ComboBoxItem>
            <ComboBoxItem Content="No"></ComboBoxItem>
        </ComboBox>

因此,无论是wpf combox还是combox属性都存在错误,我需要做一些我错过的事情。我在属性中唯一做的就是...... Items&gt;收集并添加内容一个为是,另一个为否。

我还在研究,一旦找到解决方案,我会更新

找到解决方案

似乎在wpf进程中,或者架构设计与winobox有很大不同。以下代码无效

cBHealthDetails.Text = ds.Tables[0].Rows[rno][2].ToString();

我还必须使用条件循环来消除重复值。

所以下面这段代码对我来说对我所发现的here的combox有用,并感谢sohel khalifa ......

 for (int intCount = 0; intCount < ds.Tables[0].Rows.Count; intCount++)
               {
                   var val = ds.Tables[0].Rows[intCount]["Health"].ToString();

                   //check if it already exists
                   if (!cbHealth.Items.Contains(val))
                   {
                       cbHealth.Items.Add(val);
                   }
               }

我甚至没有触摸xmal文件以使代码正常工作。换句话说,我没有在xaml中编写任何代码。我认为大多数开发人员和/或最终用户更喜欢使用DataGrid。

3 个答案:

答案 0 :(得分:0)

我看到了一些可以改进此代码的方法。首先,每次单击导航按钮时都不应检索数据。其次,不需要在代码中设置任何控件的Text。它们都绑定到SelectedItem上的ListView。这意味着当您更改ListView的{​​{1}}时,其值会自动更新。

您应该将数据加载到SelectedItem一次(我假设您在DataTable事件处理程序中执行此操作)。您的导航功能应如下所示:

Loaded

从那里,你的private void PreRec_Click(object sender, RoutedEventArgs e) { if (ds.Tables[0].Rows.Count > 0) { if (rno > 0) { rno--; listView1.SelectedItem = ds.Tables[0].Rows[rno]; } } } 将完成所有工作(就像点击Binding中的一行时一样)。

答案 1 :(得分:0)

您的主要问题是,您已将ItemsSource ListView设置为"{Binding}" ...这是绑定到您的整个MainWindow.cs类,而不是数据。为您的数据创建一个属性,然后绑定到该属性,然后您将看到数据。

顺便说一下,ListView不是ComboBox

答案 2 :(得分:0)

我已在顶部更新了解决方案