2014-12-11 57 views
0

這裏我在wpfwin.xaml DataGrid中值(從數據上下文綁定源)。我想收集名單「Challan_No」的價值,但我不能做到這一點..please幫我如何閱讀檢查行單元格中的WPF DataGrid中使用C#

  <DataGridTextColumn Header="Chalaan ID" Width="Auto" Binding="{Binding Id, Mode=OneWay}"/> 
      <DataGridTextColumn Header="Challan No" Width="Auto" Binding="{Binding Challan_No, Mode=OneWay}" IsReadOnly="True"/> 
      <DataGridTextColumn Header="Company" Width="Auto" Binding="{Binding Organization_Name, Mode=OneWay}"/> 
      <DataGridTextColumn Header="Client" Width="Auto" Binding="{Binding Organization, Mode=OneWay}"/> 
      <DataGridTextColumn Header="Date" Width="Auto" Binding="{Binding Date, Mode=OneWay}"/> 

      <DataGridTemplateColumn Header="select" Width="100" > 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <CheckBox x:Name="ckselect" ></CheckBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 

在這裏,我嘗試了一些代碼來獲取它的.cs頁

    ArrayList list = new ArrayList(); 
        for (int i = 0; i < myGrid.Items.Count; i++) 
        { 
         CheckBox mycheckbox = myGrid.Columns[5].GetCellContent(myGrid.Items[i]) as CheckBox; 
         if (mycheckbox.IsChecked == true) 
         { 
          int inde = this.myGrid.SelectedIndex; 
          DataRowView drv = (DataRowView)myGrid.Items[inde]; 
          object ch = drv[1]; 
          list.Add(ch); 
         } 
        } 

回答

1

首先,你可以簡單地使用一個DataGridCheckBoxColumn而不是添加CheckBoxDataGridTemplateColumn

  <DataGridTextColumn Header="Chalaan ID" Width="Auto" Binding="{Binding Id, Mode=OneWay}"/> 
      <DataGridTextColumn Header="Challan No" Width="Auto" Binding="{Binding Challan_No, Mode=OneWay}" IsReadOnly="True"/> 
      <DataGridTextColumn Header="Company" Width="Auto" Binding="{Binding Organization_Name, Mode=OneWay}"/> 
      <DataGridTextColumn Header="Client" Width="Auto" Binding="{Binding Organization, Mode=OneWay}"/> 
      <DataGridTextColumn Header="Date" Width="Auto" Binding="{Binding Date, Mode=OneWay}"/> 
      <DataGridCheckBoxColumn Header="select" Width="100" /> 
      </DataGrid.Columns> 
     </DataGrid> 

,並得到檢查的項目清單:

var SelectedList=new List<YourDataGridItemType>(); 
    for (int i = 0; i < MyDataGrid.Items.Count; i++) 
      { 
       var item = MyDataGrid.Items[i]; 
       var mycheckbox = MyDataGrid.Columns[1].GetCellContent(item) as CheckBox; 
       if ((bool)mycheckbox.IsChecked) 
       {     
        SelectedList.Add(YourDataGridItemsList[i]); 
       } 
      } 

其中YourDataGridItemsList表示你的DataGrid被綁定到對象的列表。

+0

如何啓用複選框選中 – 2014-12-11 10:19:24

+0

你的意思是讓DataGridCheckBoxColumn的器isChecked默認值爲true? – Usama 2014-12-11 10:26:41

+0

我實現您的代碼在我的網頁BU山雀無法使選擇它們 – 2014-12-11 10:35:10

0

我認爲,如果你增加了一個IsSelected屬性到任何類型它是要顯示,然後只是雙向該屬性的複選框列綁定這將是最好的。這樣,你可以簡單地這樣做是爲了獲得所選擇的要素:

mycollection.Where(x=>x.IsSelected); 
相關問題