我创建了一个非常简单的数据库应用程序,因为我对 WPF 很陌生。最终,我能够在 winform 中使用导航按钮(不使用数据绑定)。我遇到了一个问题,我希望它是一个非常简单的问题,但我又不知道?
问题
当我使用导航按钮时,数据未显示在组合框中,但文本框似乎没有问题,它基本上显示为空。我有一个列表视图,所以当 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="<<" Height="23" HorizontalAlignment="Left" Margin="25,301,0,0" Name="FirstRec" VerticalAlignment="Top" Width="75" Click="FirstRec_Click" />
<Button Content="<" Height="23" HorizontalAlignment="Left" Margin="111,301,0,0" Name="PreRec" VerticalAlignment="Top" Width="75" Click="PreRec_Click" />
<Button Content=">" Height="23" HorizontalAlignment="Right" Margin="0,301,112,0" Name="NxtRec" VerticalAlignment="Top" Width="75" Click="NxtRec_Click" />
<Button Content=">>" 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 组合框存在错误,要么对于组合框属性,我需要做一些我错过的事情。我在属性中做的唯一一件事是...Items > Collections 并为“是”添加了“内容”,为“否”添加了另一个。
我还在研究,一旦找到解决方案我会更新
找到解决方案
似乎 wpf 进程或架构设计与 winform 非常不同,尤其是组合框。以下代码将不起作用
cBHealthDetails.Text = ds.Tables[0].Rows[rno][2].ToString();
我还必须使用条件循环来消除重复值。
因此,以下代码对我在这里找到的组合框有用,感谢 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。