DataGrid的行是虚拟的,不可见的部分是不存在的,如果用GetCellContent这时会出现空值,所以要对数据源进行遍历,对数据源进行查询
我的每一行前面都有checkbox
如果只遍历选中的行?
数据源怎么判断那一行是不是被选中呢?
@xiao22y: 不知道你的数据源是什么样的,举个例子,如果数据源是一个dataTable或者一个class类可以在里面添加一列或者是属性来保存checkbox的状态是true或者是false,dataTable要绑定好列,class类设置好通知,下面是绑定就简单了
<DataGrid Name="dg" ItemsSource="{Binding}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridCheckBoxColumn Binding="{Binding IsSelected}"/> </DataGrid.Columns> </DataGrid>
@panmin: 这是我的绑定代码你帮我看看
private void GetModelDataList() { try { string conn = "data source=192.168.1.239;initial catalog=OfflineCDManage;persist security info=True;user id=sa;password=bctop"; string sql = "select Row_Number() over (order by getdate()) ID, ID DiskID, Name DiskName, BarCode DiskBarcode, Capacity DiskCapacity, (capacity - OccupySpace) FreeSpace " + "from BlueDisk d inner join (select DiskID, SUM(FileSize) OccupySpace from DiskFile group by DiskID) as f on d.ID = f.DiskID where UseTypeID=1000 order by Name"; SqlDataAdapter da = new SqlDataAdapter(sql, conn); System.Data.DataSet ds = new System.Data.DataSet(); da.Fill(ds, "BlueDisk"); DataGridDiskList.ItemsSource = ds.Tables["BlueDisk"].DefaultView; //List<BlueDisk> queryDiskList = null; //using (OfflineCDManageEntities db = new OfflineCDManageEntities()) //{ // queryDiskList = db.BlueDisks.Where(disk => disk.UseTypeID == 1000 && disk.ShelfID.HasValue && disk.ShelfRowIndex.HasValue && disk.ShelfCellIndex.HasValue && disk.ShelfCellOrderIndex.HasValue).ToList(); //} //DataGridDiskList.ItemsSource = queryDiskList; } catch (Exception mye) { System.Windows.Forms.MessageBox.Show(mye.ToString()); throw; } }
xaml
<DataGrid FontSize="12" AutoGenerateColumns="False" Background="Transparent" Height="544" BorderThickness="0" CellStyle="{StaticResource CellCenterAlignment}" Name="DataGridDiskList" Width="787" RowHeight="30" CanUserAddRows="False" FontWeight="Bold"> <DataGrid.Columns> <DataGridCheckBoxColumn Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}}"> <DataGridCheckBoxColumn.HeaderTemplate> <DataTemplate> <CheckBox Content="全选" Name="cbSelectAll" Uid="{Binding Path=ID}" Tag="{Binding Path=DiskID}" Checked="cbSelectAll_Checked" Unchecked="cbSelectAll_Unchecked" /> </DataTemplate> </DataGridCheckBoxColumn.HeaderTemplate> </DataGridCheckBoxColumn> <DataGridTextColumn Header="光盘编号" HeaderStyle="{StaticResource TableTileCenterAlignment}" Binding="{Binding Path=DiskID}" Width="130" IsReadOnly="True" FontSize="12" ></DataGridTextColumn> <DataGridTextColumn Header="光盘名称" HeaderStyle="{StaticResource TableTileCenterAlignment}" Binding="{Binding Path=DiskName}" Width="150" IsReadOnly="True"></DataGridTextColumn> <DataGridTextColumn Header="光盘条码" HeaderStyle="{StaticResource TableTileCenterAlignment}" Binding="{Binding Path=DiskBarcode}" Width="150" IsReadOnly="True"></DataGridTextColumn> <DataGridTextColumn Header="光盘大小" HeaderStyle="{StaticResource TableTileCenterAlignment}" Binding="{Binding Path=DiskCapacity}" Width="150" IsReadOnly="True"></DataGridTextColumn> <DataGridTextColumn Header="剩余空间" HeaderStyle="{StaticResource TableTileCenterAlignment}" Binding="{Binding Path=FreeSpace}" Width="150" IsReadOnly="True"></DataGridTextColumn> </DataGrid.Columns> </DataGrid>
具体怎么弄能说下吗?我还是糊涂的
@xiao22y: 这样绑定数据源之后还有什么问题出现吗?如果要遍历的话,就直接遍历ds.Tables["BlueDisk"]不就行了吗,它的IsSelected属性不就能知道这一行是否被选中了吗
@panmin: 怎么去判断IsSelected的属性?我这里不会啊?
@xiao22y: 你的DataTable里面没有IsSelected这个列吗?那你是怎么绑定上去的呢;如果真的没有你也可以给查询出来的DataTable添加这么一列啊,用完删除不就行了。
DataColumn column = new DataColumn(); column.DataType = typeof(bool); column.ColumnName = "IsSelected"; table.Columns.Add(column);
最后循环DataTable这个总该会了吧
DataColumn
@panmin: 我binding那个Isselected是实现全选的,我向DataTable里添加了一列,更改CheckBox的状态 新加列的值却是不变的,还需要进行其他的动态更新吗?
@xiao22y: 真没法说了,自己去看看吧,真心没觉得难在哪里http://download.csdn.net/detail/pm_854537719/4726547
@panmin: 我换用class类做数据源了,不过还是谢谢你!!