WPF DataGrid

前台代码

<DataGrid Name="gv_GetWork" AutoGenerateColumns="False" CanUserSortColumns="False" CanUserAddRows="False" Margin="5">
<!--鼠标经过事件-->
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="Foreground" Value="White"/>

</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<!--鼠标选中事件-->
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="SeaGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Width="60" Header="选择">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Click="CheckBox_Click" Tag="{Binding id}" HorizontalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="编号" Width="50" Binding="{Binding id}" />
<DataGridTextColumn Header="条码" Width="180" Binding="{Binding gtin}" />
<DataGridTextColumn Header="类型" Width="180" Binding="{Binding type_name}" />
<DataGridTextColumn Header="名称" Width="200" Binding="{Binding name}"/>
<DataGridTextColumn Header="创建时间" Width="180" Binding="{Binding create_time}"/>
</DataGrid.Columns>
</DataGrid>

后台代码
dbHelper.WebService_DbHelperSoapClient db = new dbHelper.WebService_DbHelperSoapClient();
private void GetData()
{
dataGrid1.ItemsSource = db.GetAdminData().DefaultView;
} List<int> selectID = new List<int>(); private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
int id = Convert.ToInt32(cb.Tag);
var isSelected = cb.IsChecked;
if (isSelected == true)
{
selectID.Add(id);
}
else
{
selectID.Remove(id);
}
} private void btn_Get_Click(object sender, RoutedEventArgs e)
{
foreach (int id in selectID)
{
MessageBox.Show(id.ToString());
}
} private void Button_Click(object sender, RoutedEventArgs e)
{
foreach (DataGridColumn dgc in dataGrid1.Columns)
{ }
}

  

上一篇:URL中带加号的处理


下一篇:利用Python操作Word文档【图片】