我遇到了加载数据网格的问题。实际上,我通过填充填充了从SQL DB获取的值的数据表来显示数据网格,其中3个额外的列具有复选框。当用户单击一个复选框时,必须启用下一个复选框,并在DB中更新。为了达到这个目的,我确实喜欢这样。当用户单击一个复选框时,它将更新第一个任务完成的数据表,因此下次读取db时,它会先通过禁用启用下一个任务。我做了所有的事情,但我很清楚早期的数据网格并显示更新的数据网格。 DB正在更新,甚至在itemsource中,我正在更新更新的。但仍显示前一个。
我的DATAGRID的XAML代码:
<DataGrid Name="DG_FollowUp" ItemsSource="{Binding dt}" AutoGenerateColumns="False" SelectionMode="Single" SelectedIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Grid.Row="01" BorderBrush="Black" BorderThickness="0.5" LoadingRow="DG_FollowUp_LoadingRow" IsEnabled="True" IsReadOnly="True" AlternationCount="2" ><!--VirtualizingStackPanel.IsVirtualizing="True"-->
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding EntryID}" Header="Entry ID" Width="75">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value='Center'/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding [TicketID/Subject]}" Header="TicketID/Subject" Width="300" CanUserSort="False"/>
<DataGridTextColumn Binding="{Binding [AddDate]}" Header="Start Date" Width="90" CanUserSort="False"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding [Status]}" Header="Status" Width="80" CanUserSort="False"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding [OwnerShip]}" Header="Ownership" Width="130" CanUserSort="False"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding [Comments]}" Header="Comments" Width="300" CanUserSort="False"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding [SLA DATE]}" Header="SLA Date" Width="90" CanUserSort="False"></DataGridTextColumn>
<DataGridTemplateColumn Header="Strike1" Width="65">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Click="CHK_Strike1_Click" IsChecked="{Binding Checked}" Name="CHK_Strike1" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Strike2" Width="65">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Click="CHK_Strike2_Click" IsChecked="{Binding Checked}" Name="CHK_Strike2" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Strike3" Width="65">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Click="CHK_Strike3_Click" Name="CHK_Strike3" IsChecked="{Binding Checked}" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Close Tckt" Width="95">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Click="Chk_CloseTckt_Click" Name="Chk_CloseTckt" IsChecked="{Binding Checked}" HorizontalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
用于向DataGrid显示更新数据库的代码:
using (SqlConnection con = new SqlConnection(UserIDDataTableConnString))
{
SqlCommand cmd = new SqlCommand("select * from TBL_TicketMngr where Status = 'Follow Up';", con);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("TBL_TicketMngr");
sda.Fill(dt);
////DG_FollowUp.Items.Clear();
////DG_FollowUp.ItemsSource = null;
//DG_FollowUp.ItemsSource = dt.DefaultView;
////DG_FollowUp.Items.Refresh();
DG_FollowUp.DataContext = dt.DefaultView;
DG_FollowUp.ItemsSource = null;
//DG_FollowUp.ItemsSource = dt.DefaultView;
foreach (DataRow row in dt.Rows)
{
ThreeStrikeStatus.Add(row["StrikeStage"].ToString());
}
for (int i = 0; i < ThreeStrikeStatus.Count; i++)
{
if (ThreeStrikeStatus[i] == "ONE")
{
DG_FollowUp.GetCell(i, 7).IsEnabled = false;
DG_FollowUp.GetCell(i, 7).Visibility = Visibility.Hidden;
DG_FollowUp.GetCell(i, 9).IsEnabled = false;
DG_FollowUp.GetCell(i, 9).Visibility = Visibility.Hidden;
DG_FollowUp.GetCell(i, 10).IsEnabled = false;
}
else if (ThreeStrikeStatus[i] == "TWO")
{
DG_FollowUp.GetCell(i, 7).IsEnabled = false;
DG_FollowUp.GetCell(i, 7).Visibility = Visibility.Hidden;
DG_FollowUp.GetCell(i, 8).IsEnabled = false;
DG_FollowUp.GetCell(i, 8).Visibility = Visibility.Hidden;
DG_FollowUp.GetCell(i, 10).IsEnabled = false;
}
else if (ThreeStrikeStatus[i] == "THREE")
{
DG_FollowUp.GetCell(i, 7).IsEnabled = false;
DG_FollowUp.GetCell(i, 7).Visibility = Visibility.Hidden;
DG_FollowUp.GetCell(i, 8).IsEnabled = false;
DG_FollowUp.GetCell(i, 8).Visibility = Visibility.Hidden;
DG_FollowUp.GetCell(i, 9).IsEnabled = false;
DG_FollowUp.GetCell(i, 9).Visibility = Visibility.Hidden;
DG_FollowUp.GetCell(i, 10).IsEnabled = true;
}
else if (ThreeStrikeStatus[i] == "")
{
DG_FollowUp.GetCell(i, 8).IsEnabled = false;
DG_FollowUp.GetCell(i, 8).Visibility = Visibility.Hidden;
DG_FollowUp.GetCell(i, 9).IsEnabled = false;
DG_FollowUp.GetCell(i, 9).Visibility = Visibility.Hidden;
DG_FollowUp.GetCell(i, 10).IsEnabled = false;
}
}
DG_FollowUp.UpdateLayout();
如果上述内容有任何错误或有任何其他方式可以实现所需功能,请告知我们。
我也遇到了根据从db获取的值检查datagridtemplatecolumn中的复选框的问题。如果您有任何解决方案,请帮助我。
答案 0 :(得分:0)
使用Binding方法时,不应使用刷新或重新加载DataGrid数据。绑定的全部要点是实现INotifyPropertyChanged
并让代理人为您更新UI。
以下是显示如何实施INotifyPropertyChanged
How to implement DataTable property with INotifyPropertyChanged
我个人对DataTable
所做的就是把它放在CollectionViewSource
中并绑定到它。
public class ViewModel
{
//Properties
CollectionViewSource _CollectionViewSource { get; set; }
DataTable dt { get; set; }
//Constructor
public ViewModel()
{
_CollectionViewSource.Source = dt.DefaultView
}
}
<DataGrid ItemsSource="{Binding _CollectionViewSource.View}" />