问题描述
大家好,
我有一个小查询,
在按钮上单击应从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; 跨度>
跨度>
        {
                        iNumber ++;
                        dr [ " ListOfPhases" ]
= cbInBuildList.SelectedItem.ToString();
的" ListOfPhases" 跨度>的]。ToString());
的                         dr [ " PhaseCount" ]
= tbEnterCountValue.Text;
的" PhaseCount" 跨度>的]。ToString());
的                         dr [ " S_no1" ]
= iNumber;
               &NBSP ;        dt.Rows.Add(dr);
                    }
                } 跨度>
         
            }
            catch(NullReferenceException)
            {
            }
}
私人无效Button_DeleteSelectedRow_Click(对象发件人,RoutedEventArgs E)跨度>
Private void Button_DeleteSelectedRow_Click(object sender, RoutedEventArgs e)
{
   
//需要代码
//Need code here
}
}
}
任何人都可以帮助我完成这个过程。
Can anyone kindly help me through this process.
提前感谢。
关心,
Sravan Kumar Domala
Regards,
Sravan Kumar 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表中删除选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!