从WPF中的datagrid表中删除选定的行 [英] Delete selected row from datagrid table in WPF
问题描述
大家好,
我有一个小查询,
在按钮上单击应从DataGrid表中删除所选行。
以下是我的代码:
On button click the selected row should be deleted from the DataGrid table.
Below is my code:
public partial class
MainWindow :Window
{
DataView dv;        
DataTable dt;
DataRow dr;
Public MainWindow()
Public MainWindow()
{
{
   InitializeComponent();
            this.SizeToContent = SizeToContent.WidthAndHeight;
DataTable dt1 = new DataTable();
dt1.Columns.Add( " ListOfPhases" );
dt1.Columns.Add( " PhaseCount" );
dt1.Columns.Add( " S_no1" );
DataRow dr = dt1.NewRow();
dgvListOfPhasesAndCount.ItemsSource = dt1.DefaultView; 跨度>
<跨度风格="颜色:蓝;字体家庭:Consolas; font-size:10pt"> }
}
Private void AddButtonClick(object sender,RoutedEventArgs e)
Private void AddButtonClick(object sender, RoutedEventArgs e)
{
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; DV = dgvListOfPhasesAndCount.ItemsSource作为数据视图; 跨度>
<跨度风格="颜色:蓝;字体家庭:Consolas; font-size:10pt"> dt = dv.Table;
dr = dt.NewRow();
尝试
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
<跨度>&NBSP;&NBSP; 如果(System.Text.RegularExpressions.Regex.IsMatch(tbEnterCountValue.Text,
<跨度>&NBSP; 跨度> 跨度> <跨度style ="color:#a31515; font-family:Consolas; font-size:10pt">" ^ [0-9]" )&& cbInBuildList.SelectedItem.ToString()!= null)
if (System.Text.RegularExpressions.Regex.IsMatch(tbEnterCountValue.Text, "^[0-9]") && cbInBuildList.SelectedItem.ToString() != null)
<跨度风格=" 颜色:蓝;字型家族:索拉;字体大小:10PT">&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
<跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
如果跨度>
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; iNumber ++;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; dr [ " ListOfPhases" ]
= cbInBuildList.SelectedItem.ToString();
<跨度风格=" 颜色:蓝;字型家族:索拉;字体大小:10PT">&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; lsSelectedPhases.Add(DR [跨度>的" ListOfPhases" 跨度>的]。ToString());
的&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; dr [ " PhaseCount" ]
= tbEnterCountValue.Text;
<跨度风格= "颜色:蓝;字型家族:索拉;字体大小:10PT">&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N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edRow_Click(对象发件人,RoutedEventArgs E)跨度>
Private void Button_DeleteSelectedRow_Click(object sender, RoutedEventArgs e)
{
&NBSP;&NBSP;&NBSP;
//需要代码
//Need code here
}
}
}
任何人都可以帮助我完成这个过程。
Can anyone kindly help me through this process.
提前感谢。
关心,
Sravan Kumar Domala
Regards,
Sravan Kumar Domala
推荐答案
您好Domala,
Hi Domala,
根据您的描述和相关代码片段,我为您创建一个简单的示例参考。我希望它会对你有所帮助。
Based on your description and related code snippet, I create a simple sample for your reference. I hope it will be helpful to you.
#XAML
<Window x:Class="WpfApp1.Window12"
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:WpfApp1"
mc:Ignorable="d"
Title="Window12" Height="300" Width="600" Loaded="Window_Loaded">
<Grid>
<DataGrid x:Name="gridPosts" ItemsSource="{Binding dataTable}">
<DataGrid.Columns>
<!-- ... -->
<DataGridTemplateColumn Header="Delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="delete" Content="Delete" Click="Button_Click">
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
#Code Behind
#Code Behind
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Input;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for Window12.xaml
/// </summary>
public partial class Window12 : Window
{
public Window12()
{
InitializeComponent();
}
DataTable dt = new DataTable();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Sales;Integrated Security=True";
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = connectionString;
SqlCommand com = new SqlCommand("SELECT * from Blogs", con);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(com);
dt = new DataTable();
sda.Fill(dt);
gridPosts.ItemsSource = dt.DefaultView;
gridPosts.AutoGenerateColumns = true;
gridPosts.CanUserAddRows = false;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DataRowView row = (DataRowView)gridPosts.SelectedItem;
dt.Rows.Remove(row.Row);
}
}
}
致以最诚挚的问候,
张龙
这篇关于从WPF中的datagrid表中删除选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!