I have a datagrid within a WinForm application that is bound to a list of Addresses. The list of addresses is long, so I have to scroll to select the address I want. However, after I scroll and find the address I want and select it, the datagrid will select the address that was in the same position on the grid when the form was first loaded. I was wondering what I am doing wrong and how to might I get my desired result.
//
// bindingSource1
//
private System.Windows.Forms.BindingSource bindingSource1;
this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
this.bindingSource1.DataSource = typeof(ViewModels.ListAddressViewModel);
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AllowUserToOrderColumns = true;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.HouseNumber,
this.Prefix,
this.StreetName,
this.StreetType,
this.StreetSuffix,
this.SecondaryType,
this.SecondaryNumber,
this.City,
this.State,
this.ZipCode});
this.dataGridView1.DataBindings.Add(new System.Windows.Forms.Binding("DataSource", this.bindingSource1, "AddressList", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.Location = new System.Drawing.Point(0, 50);
this.dataGridView1.MultiSelect = false;
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.ShowCellErrors = false;
this.dataGridView1.ShowCellToolTips = false;
this.dataGridView1.ShowEditingIcon = false;
this.dataGridView1.ShowRowErrors = false;
this.dataGridView1.Size = new System.Drawing.Size(1014, 421);
this.dataGridView1.TabIndex = 2;
this.dataGridView1.SelectionChanged += new System.EventHandler(this.dataGridView1_SelectionChanged);
//Selection Change Handler
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
_vm.SelectedAddress = (Address)dataGridView1.SelectedRows[0].DataBoundItem;
}
}
//My View Model (_vm)
public class ListAddressViewModel
{
public IList<Address> AddressList { get; set; }
private IAddressRepository _repo;
public Address SelectedAddress {get;set;}
public ListAddressViewModel()
{
AddressList = new List<Address>();
}
public ListAddressViewModel(IAddressRepository AddrRepo)
:this()
{
_repo = AddrRepo
init();
}
private void init()
{
if(_repo != null)
{
AddressList = _repo.FindAll();
}
}
... etc..
}
something like this ...
if (dataGridView1.SelectedRows.Count > 0)
{
_vm.SelectedAddress =(Address)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["Address"].Value.ToString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With