WPF中对datagrid删除选中的某一行。 100
前台:<DataGridName="dgv"RowHeaderWidth="0"HorizontalAlignment="Left"Margin="214,206,0,0...
前台:
<DataGrid Name="dgv" RowHeaderWidth="0" HorizontalAlignment="Left" Margin="214,206,0,0" VerticalAlignment="Top" Height="514" Width="856" AutoGenerateColumns="False" CanUserAddRows="False"> <DataGrid.Columns> <DataGridTemplateColumn Header="选择"> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate /> </DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox Tag="{Binding Path= ID}" Name="select"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding ID}" Header="序号" Width="40"/> <DataGridTextColumn Binding="{Binding Name}" Header="姓名" Width="40"/> <DataGridTextColumn Binding="{Binding Sex}" Header="性别" Width="40" /> <DataGridTextColumn Binding="{Binding Age}" Header="年龄" Width="40"/> <DataGridTextColumn Binding="{Binding Jycd}" Header="教育程度" Width="80"/> <DataGridTextColumn Binding="{Binding Hy}" Header="婚否" Width="40"/> <DataGridTextColumn Binding="{Binding Zy}" Header="职业" Width="80"/> <DataGridTextColumn Binding="{Binding XM}" Header="训练项目" Width="100"/> <DataGridTextColumn Binding="{Binding HJ}" Header="训练环境" Width=" 100"/> <DataGridTextColumn Binding="{Binding SJ}" Header="训练时间" Width=" 100"/> <DataGridTextColumn Binding="{Binding Delta}" Header="Delta" Width=" 100"/> </DataGrid.Columns> </DataGrid>
</StackPanel> <StackPanel> <Button Content="删除" Name="Del" Click="Del_Click"></Button> </StackPanel>
后台目前只绑定了数据库:我的Datagrid是用到了联合查询,分别两张表T_grxx、thinkgeardata。
我现在想实现勾选chexkbox ,点击一个删除按钮就能删除该行。 我是新手,尽量多写点代码给我参考,我的qq:269440542,在线等 展开
<DataGrid Name="dgv" RowHeaderWidth="0" HorizontalAlignment="Left" Margin="214,206,0,0" VerticalAlignment="Top" Height="514" Width="856" AutoGenerateColumns="False" CanUserAddRows="False"> <DataGrid.Columns> <DataGridTemplateColumn Header="选择"> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate /> </DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox Tag="{Binding Path= ID}" Name="select"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding ID}" Header="序号" Width="40"/> <DataGridTextColumn Binding="{Binding Name}" Header="姓名" Width="40"/> <DataGridTextColumn Binding="{Binding Sex}" Header="性别" Width="40" /> <DataGridTextColumn Binding="{Binding Age}" Header="年龄" Width="40"/> <DataGridTextColumn Binding="{Binding Jycd}" Header="教育程度" Width="80"/> <DataGridTextColumn Binding="{Binding Hy}" Header="婚否" Width="40"/> <DataGridTextColumn Binding="{Binding Zy}" Header="职业" Width="80"/> <DataGridTextColumn Binding="{Binding XM}" Header="训练项目" Width="100"/> <DataGridTextColumn Binding="{Binding HJ}" Header="训练环境" Width=" 100"/> <DataGridTextColumn Binding="{Binding SJ}" Header="训练时间" Width=" 100"/> <DataGridTextColumn Binding="{Binding Delta}" Header="Delta" Width=" 100"/> </DataGrid.Columns> </DataGrid>
</StackPanel> <StackPanel> <Button Content="删除" Name="Del" Click="Del_Click"></Button> </StackPanel>
后台目前只绑定了数据库:我的Datagrid是用到了联合查询,分别两张表T_grxx、thinkgeardata。
我现在想实现勾选chexkbox ,点击一个删除按钮就能删除该行。 我是新手,尽量多写点代码给我参考,我的qq:269440542,在线等 展开
展开全部
方法如下:
DataTable嵌入DataGrid,DataTable可以通过Rows和Columns删除行和列
代码:
private void AddData_Click(object sender, RoutedEventArgs e)
{
DataRow dr = dt.NewRow();
for (int columIndex = 0; columIndex < dt.Columns.Count; columIndex++)
{
if (columIndex == 0)
dr[columIndex] = "第" + dt.Rows.Count.ToString() + "组";
else
dr[columIndex] = "";
//dr[columIndex] = dt.Rows.Count.ToString() + " - " + columIndex.ToString();
}
dt.Rows.Add(dr);
}
int newColumnIndex = 5;
//增加一列
private void AddColumn_Click(object sender, RoutedEventArgs e)
{
dt.Columns.Add(new DataColumn("" + newColumnIndex++));
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i == 0)
dt.Rows[i][dt.Columns.Count - 1] = "A" + (newColumnIndex - 1).ToString();
else
dt.Rows[i][dt.Columns.Count - 1] = "";
//dt.Rows[i][dt.Columns.Count - 1] = i.ToString() + " - " + (newColumnIndex - 1).ToString();
}
dataGrid.ItemsSource = null;
dataGrid.ItemsSource = dt.DefaultView;
}
//删除一行
private void DeleteData_Click(object sender, RoutedEventArgs e)
{
if (dt.Rows.Count > 2)
{
if (dataGrid.SelectedIndex != -1 && dataGrid.SelectedIndex != 0 && dataGrid.SelectedIndex != dt.Rows.Count)
{
dt.Rows.RemoveAt(dataGrid.SelectedIndex);
}
else
{
dt.Rows.RemoveAt(dt.Rows.Count - 1);
}
}
}
DataTable嵌入DataGrid,DataTable可以通过Rows和Columns删除行和列
代码:
private void AddData_Click(object sender, RoutedEventArgs e)
{
DataRow dr = dt.NewRow();
for (int columIndex = 0; columIndex < dt.Columns.Count; columIndex++)
{
if (columIndex == 0)
dr[columIndex] = "第" + dt.Rows.Count.ToString() + "组";
else
dr[columIndex] = "";
//dr[columIndex] = dt.Rows.Count.ToString() + " - " + columIndex.ToString();
}
dt.Rows.Add(dr);
}
int newColumnIndex = 5;
//增加一列
private void AddColumn_Click(object sender, RoutedEventArgs e)
{
dt.Columns.Add(new DataColumn("" + newColumnIndex++));
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i == 0)
dt.Rows[i][dt.Columns.Count - 1] = "A" + (newColumnIndex - 1).ToString();
else
dt.Rows[i][dt.Columns.Count - 1] = "";
//dt.Rows[i][dt.Columns.Count - 1] = i.ToString() + " - " + (newColumnIndex - 1).ToString();
}
dataGrid.ItemsSource = null;
dataGrid.ItemsSource = dt.DefaultView;
}
//删除一行
private void DeleteData_Click(object sender, RoutedEventArgs e)
{
if (dt.Rows.Count > 2)
{
if (dataGrid.SelectedIndex != -1 && dataGrid.SelectedIndex != 0 && dataGrid.SelectedIndex != dt.Rows.Count)
{
dt.Rows.RemoveAt(dataGrid.SelectedIndex);
}
else
{
dt.Rows.RemoveAt(dt.Rows.Count - 1);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询