C# 用户对列进行排序时排序不起作用
我用C#在WPF中创建了一个笔记应用程序,用于培训目的。C# 用户对列进行排序时排序不起作用,c#,wpf,sorting,datatable,datagridview,C#,Wpf,Sorting,Datatable,Datagridview,我用C#在WPF中创建了一个笔记应用程序,用于培训目的。 我输入了一些数据并将其保存在一个datatable中,该datatable是WPF中的datagridview。数据包括您在文本框“title”中输入的标题和您在文本框“message”中输入的消息。现在我想读出我输入的数据。因此,当我选择第一行并按“Read”时,“1”出现在标题文本框中,“a”出现在“Message”文本框中,当我选择第二行和第三行时同样适用。 现在,我按“标题”对数据网格视图进行排序,再次选择第三行,它在datag
我输入了一些数据并将其保存在一个datatable中,该datatable是WPF中的datagridview。数据包括您在文本框“title”中输入的标题和您在文本框“message”中输入的消息。现在我想读出我输入的数据。因此,当我选择第一行并按“Read”时,“1”出现在标题文本框中,“a”出现在“Message”文本框中,当我选择第二行和第三行时同样适用。 现在,我按“标题”对数据网格视图进行排序,再次选择第三行,它在datagridview中显示为“1”,然后按“读取”。如您所见,数字“3”出现,而不是“1”…因此,似乎datagridview已排序,但保存值的datatable未排序 我不知道如何解决这个问题,因为寻找解决方案对我没有帮助…我想我只是不太了解Microsoft文档…所以我想你可能会帮我解决这个问题 提前感谢,下面是ofc的cs和xaml代码: cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.ComponentModel;
namespace NoteTaking_App
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DataTable table;
int indexSelected; //Index of selected row
//indexSelected = dgrid_data.SelectedIndex;
private void MB(string text, string title)
{
MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Information);
}
public MainWindow()
{
InitializeComponent();
}
private void form_Main_Loaded(object sender, RoutedEventArgs e)
{
table = new DataTable();
table.Columns.Add("Title", typeof(String));
table.Columns.Add("Message", typeof(String));
dgrid_data.ItemsSource = table.DefaultView;
dgrid_data.Columns[1].Visibility = Visibility.Collapsed;
}
private void bttn_new_Click(object sender, RoutedEventArgs e)
{
txt_message.Clear();
txt_title.Clear();
}
private void bttn_save_Click(object sender, RoutedEventArgs e)
{
if (String.IsNullOrWhiteSpace(txt_title.Text))
{
MB("Please put in a Title", "title missing");
}
else
{
table.Rows.Add(txt_title.Text, txt_message.Text);
txt_message.Clear();
txt_title.Clear();
}
}
private void bttn_read_Click(object sender, RoutedEventArgs e)
{
indexSelected = dgrid_data.SelectedIndex;
if (indexSelected > -1)
{
txt_title.Text = table.Rows[indexSelected].ItemArray[0].ToString();
txt_message.Text = table.Rows[indexSelected].ItemArray[1].ToString();
}
else if (table.Rows.Count ==0)
{
MB("There are no saved rows to read", "Selection missing");
}
else
{
MB("Please select a row", "Selection missing");
}
}
private void bttn_delete_Click(object sender, RoutedEventArgs e)
{
indexSelected = dgrid_data.SelectedIndex;
if (indexSelected > -1)
{
this.table.Rows[indexSelected].Delete();
}
else if (table.Rows.Count == 0)
{
MB("There are no saved rows to delete", "Selection missing");
}
else
{
MB("Please select a row", "Selection missing");
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用系统数据;
使用系统组件模型;
名称空间记事本应用程序
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
数据表;
int indexSelected;//所选行的索引
//indexSelected=dgrid_data.SelectedIndex;
私有void MB(字符串文本、字符串标题)
{
显示(文本、标题、MessageBoxButton.OK、MessageBoxImage.Information);
}
公共主窗口()
{
初始化组件();
}
已加载专用无效表单(对象发送方、路由目标)
{
table=新数据表();
表.列.添加(“标题”,类型(字符串));
表.Columns.Add(“Message”,typeof(String));
dgrid_data.ItemsSource=table.DefaultView;
dgrid_data.Columns[1]。可见性=可见性。已折叠;
}
私有无效bttn\u新建\u单击(对象发送者,路由目标e)
{
txt_message.Clear();
txt_title.Clear();
}
私有无效bttn\u保存\u单击(对象发送方,路由目标)
{
if(String.IsNullOrWhiteSpace(txt_title.Text))
{
MB(“请输入标题”,“缺少标题”);
}
其他的
{
table.Rows.Add(txt_title.Text,txt_message.Text);
txt_message.Clear();
txt_title.Clear();
}
}
私有无效bttn\u读取\u单击(对象发送者,路由目标e)
{
indexSelected=dgrid_data.SelectedIndex;
如果(索引选择>-1)
{
txt\u title.Text=表.Rows[indexSelected].ItemArray[0].ToString();
txt_message.Text=table.Rows[indexSelected].ItemArray[1].ToString();
}
else if(table.Rows.Count==0)
{
MB(“没有要读取的保存行”,“缺少选择”);
}
其他的
{
MB(“请选择一行”,“缺少选择”);
}
}
私有无效bttn\u删除\u单击(对象发送者,路由目标e)
{
indexSelected=dgrid_data.SelectedIndex;
如果(索引选择>-1)
{
this.table.Rows[indexSelected].Delete();
}
else if(table.Rows.Count==0)
{
MB(“没有要删除的保存行”,“缺少选择”);
}
其他的
{
MB(“请选择一行”,“缺少选择”);
}
}
}
}
xaml代码
<Window x:Class="NoteTaking_App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NoteTaking_App"
mc:Ignorable="d"
Title="Note Taking" Height="450" Width="800">
<Grid x:Name="form_Main" Loaded="form_Main_Loaded">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="117*"/>
<ColumnDefinition Width="220*"/>
<ColumnDefinition Width="63*"/>
</Grid.ColumnDefinitions>
<DataGrid x:Name="dgrid_data" Grid.Column="1" HorizontalAlignment="Left" Height="263" Margin="192,36,0,0" VerticalAlignment="Top" Width="372" RenderTransformOrigin="0.554,0.552" Grid.ColumnSpan="2" HeadersVisibility="Column" HorizontalScrollBarVisibility="Disabled" UseLayoutRounding="True" CanUserReorderColumns="False" ColumnWidth="*" IsReadOnly="True" IsSynchronizedWithCurrentItem="False" >
<DataGrid.RowHeaderStyle>
<Style/>
</DataGrid.RowHeaderStyle>
<DataGrid.ColumnHeaderStyle>
<Style/>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
<TextBox x:Name="txt_message" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="183" Margin="109,116,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="275" RenderTransformOrigin="0.5,0.5" AcceptsReturn="True" AcceptsTab="True">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="0"/>
<TranslateTransform/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<Button x:Name="bttn_new" Content="New" HorizontalAlignment="Left" Height="31" Margin="109,352,0,0" VerticalAlignment="Top" Width="102" Click="bttn_new_Click"/>
<Button x:Name="bttn_save" Content="Save" HorizontalAlignment="Left" Height="31" Margin="48,352,0,0" VerticalAlignment="Top" Width="102" Grid.Column="1" Click="bttn_save_Click"/>
<Button x:Name="bttn_read" Content="Read" HorizontalAlignment="Left" Height="31" Margin="246,352,0,0" VerticalAlignment="Top" Width="102" Grid.Column="1" Click="bttn_read_Click"/>
<Button x:Name="bttn_delete" Content="Delete" HorizontalAlignment="Left" Height="31" Margin="402,352,0,0" VerticalAlignment="Top" Width="102" Grid.Column="1" Click="bttn_delete_Click" Grid.ColumnSpan="2"/>
<Label x:Name="lbl_titel" Content="Title" HorizontalAlignment="Left" Height="48" Margin="12,41,0,0" VerticalAlignment="Top" Width="89" AutomationProperties.Name="Titel"/>
<Label x:Name="lbl_message" Content="Message" HorizontalAlignment="Left" Height="44" Margin="12,109,0,0" VerticalAlignment="Top" Width="89"/>
<TextBox x:Name="txt_title" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="41" Margin="108,35,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="275"/>
</Grid>
回答实施后的最终代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.ComponentModel;
namespace NoteTaking_App
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DataTable table;
private void MB(string text, string title)
{
MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Information);
}
public MainWindow()
{
InitializeComponent();
DataRowView dataRowView = (DataRowView)dgrid_data.SelectedItem;
}
private void form_Main_Loaded(object sender, RoutedEventArgs e)
{
table = new DataTable();
table.Columns.Add("Title", typeof(String));
table.Columns.Add("Message", typeof(String));
dgrid_data.ItemsSource = table.DefaultView;
dgrid_data.Columns[1].Visibility = Visibility.Collapsed;
}
private void bttn_new_Click(object sender, RoutedEventArgs e)
{
txt_message.Clear();
txt_title.Clear();
}
private void bttn_save_Click(object sender, RoutedEventArgs e)
{
if (String.IsNullOrWhiteSpace(txt_title.Text))
{
MB("Please put in a Title", "title missing");
}
else
{
table.Rows.Add(txt_title.Text, txt_message.Text);
txt_message.Clear();
txt_title.Clear();
}
}
private void bttn_read_Click(object sender, RoutedEventArgs e)
{
DataRowView dataRowView = (DataRowView)dgrid_data.SelectedItem;
if (dataRowView!=null)
{
txt_title.Text = dataRowView[0].ToString();
txt_message.Text = dataRowView[1].ToString();
}
else if (table.Rows.Count == 0)
{
MB("There are no saved rows to read", "Selection missing");
}
else
{
MB("Please select a row", "Selection missing");
}
}
private void bttn_delete_Click(object sender, RoutedEventArgs e)
{
DataRowView dataRowView = (DataRowView)dgrid_data.SelectedItem;
if (dataRowView!=null)
{
dataRowView.Delete();
}
else if (table.Rows.Count == 0)
{
MB("There are no saved rows to delete", "Selection missing");
}
else
{
MB("Please select a row", "Selection missing");
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用系统数据;
使用系统组件模型;
名称空间记事本应用程序
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
数据表;
私有void MB(字符串文本、字符串标题)
{
显示(文本、标题、MessageBoxButton.OK、MessageBoxImage.Information);
}
公共主窗口()
{
初始化组件();
DataRowView DataRowView=(DataRowView)dgrid_data.SelectedItem;
}
已加载专用无效表单(对象发送方、路由目标)
{
table=新数据表();
表.列.添加(“标题”,类型(字符串));
表.Columns.Add(“Message”,typeof(String));
dgrid_data.ItemsSource=table.DefaultView;
dgrid_data.Columns[1]。可见性=可见性。已折叠;
}
私有无效bttn\u新建\u单击(对象发送者,路由目标e)
{
txt_message.Clear();
txt_title.Clear();
}
私有无效bttn\u保存\u单击(对象发送方,路由目标)
{
if(String.IsNullOrWhiteSpace(txt_title.Text))
{
private void bttn_read_Click(object sender, RoutedEventArgs e)
{
DataRowView drv = dgrid_data.SelectedItem as DataRowView;
if (drv != null)
{
txt_title.Text = drv[0].ToString();
txt_message.Text = drv[0].ToString();
}
else if (table.Rows.Count == 0)
{
MB("There are no saved rows to read", "Selection missing");
}
else
{
MB("Please select a row", "Selection missing");
}
}