I am developing a wpf application and i have a "buyer" named datagrid and i want access row values when a checkbox is checked
I have read some questions on stackoverflow but all went over my head, i was not able to understand them as amatuer yet :(
Here is my datagrid xaml code:-
<DataGrid x:Name="buyer" SelectionMode="Single" HorizontalAlignment="Left" SelectionUnit="FullRow" VerticalAlignment="Top" Height="550" Width="992" HorizontalScrollBarVisibility="Visible" IsReadOnly="True" AutoGenerateColumns="False" FrozenColumnCount="1" Margin="0,45,0,0" SelectionChanged="RowFocus" TargetUpdated="buyer_TargetUpdated">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Joining" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="ID" Binding="{Binding buy_id}"/>
<DataGridTextColumn Header="Name" Binding="{Binding bname}"/>
<DataGridTextColumn Header="Number" Binding="{Binding mobileno}"/>
</DataGrid.Columns>
</DataGrid>
I have a button on the same window, which on clicking should give me values from the rows where the CheckBox is checked
Edit: Currently, I am checking if the CheckBox is working by writing in console. Also the CheckBox should be the 0th column, right? But when I print it in the console I get the value of the next column ie ID, I used to print the value by putting the following code :-
private void Button_Click_3(object sender, RoutedEventArgs e)
{
/* int i = 0;
Console.WriteLine("hey");
foreach (var item in buyer.Items)
{
string s = (buyer.Items[i] as DataRowView).Row.ItemArray[0].ToString();
if (i==0)
{
Console.WriteLine(s);
var row = buyer.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
}
i++;
}*/
if (buyer.SelectedItems.Count > 0)
{
for (int i = 0; i < buyer.SelectedItems.Count; i++)
{
System.Data.DataRowView selectedFile = (System.Data.DataRowView)buyer.SelectedItems[i];
string str = Convert.ToString(selectedFile.Row.ItemArray[0]);
Console.WriteLine(str);
}
}
}
I used both commented and uncommented code
Try this.... (using RelayCommand found here http://www.kellydun.com/wpf-relaycommand-with-parameter/ )
public class BasePropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
ViewModel.....
class Base_ViewModel : BasePropertyChanged
{
public RelayCommand<ObservableCollection<buyer>> ButtonClickCommand { get; set; }
private ObservableCollection<buyer> _buyer;
public ObservableCollection<buyer> buyer
{
get { return _buyer; }
set { _buyer = value; }
}
public Base_ViewModel()
{
ButtonClickCommand = new RelayCommand<ObservableCollection<buyer>>(OnButtonClickCommand);
buyer = new ObservableCollection<ViewModels.buyer>();
buyer.Add(new buyer() { buy_id = 1, bname = "John Doe", mobileno = "" });
buyer.Add(new buyer() { buy_id = 1, bname = "Jane Doe", mobileno = "" });
buyer.Add(new buyer() { buy_id = 1, bname = "Fred Doe", mobileno = "" });
buyer.Add(new buyer() { buy_id = 1, bname = "Sam Doe", mobileno = "" });
}
private void OnButtonClickCommand(ObservableCollection<buyer> obj)
{ // put a break-point here and obj will be the List of Buyer that you can then step though
}
}
Buyer Class.....
public class buyer : BasePropertyChanged
{
private bool _IsSelected;
public bool IsSelected
{
get { return _IsSelected; }
set { _IsSelected = value; }
}
private string _bname;
public string bname
{
get { return _bname; }
set { _bname = value; NotifyPropertyChanged("bname"); }
}
private int _buy_id;
public int buy_id
{
get { return _buy_id; }
set { _buy_id = value; NotifyPropertyChanged("buy_id"); }
}
private string _mobileno;
public string mobileno
{
get { return _mobileno; }
set { _mobileno = value; NotifyPropertyChanged("mobileno"); }
}
}
XAML.....
<StackPanel>
<DataGrid x:Name="buyer" ItemsSource="{Binding buyer}" SelectionMode="Single" HorizontalAlignment="Left" SelectionUnit="FullRow" IsReadOnly="True" AutoGenerateColumns="False" FrozenColumnCount="1" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Joining" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="ID" Binding="{Binding buy_id}"/>
<DataGridTextColumn Header="Name" Binding="{Binding bname}"/>
<DataGridTextColumn Header="Number" Binding="{Binding mobileno}"/>
</DataGrid.Columns>
</DataGrid>
<Button Content="Button" Command="{Binding ButtonClickCommand}" CommandParameter="{Binding ElementName=buyer, Path=ItemsSource}" Margin="0,202,0,0"></Button>
</StackPanel>
And don't forget to set your DataContext in the View code-behind....
this.DataContext = new Base_ViewModel();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.