C# 数据网格在滚动后选择了错误的行

C# 数据网格在滚动后选择了错误的行,c#,winforms,datagrid,C#,Winforms,Datagrid,我在WinForm应用程序中有一个绑定到地址列表的datagrid。地址列表很长,所以我必须滚动选择我想要的地址。但是,在我滚动并找到我想要的地址并选择它之后,datagrid将选择第一次加载表单时位于网格上相同位置的地址。我想知道我做错了什么,如何才能得到我想要的结果 // // bindingSource1 // private System.Windows.Forms.BindingSource bindingSource1; this.bindingSource1

我在WinForm应用程序中有一个绑定到地址列表的datagrid。地址列表很长,所以我必须滚动选择我想要的地址。但是,在我滚动并找到我想要的地址并选择它之后,datagrid将选择第一次加载表单时位于网格上相同位置的地址。我想知道我做错了什么,如何才能得到我想要的结果

    // 
// 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..
}
//
//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.allowUserToAddress=false;
this.dataGridView1.AllowUserToDeleteRows=false;
this.dataGridView1.AllowUserToOrderColumns=true;
this.dataGridView1.columnHeadershightSizeMode=System.Windows.Forms.datagridviewColumnHeadershightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(新的System.Windows.Forms.DataGridViewColumn[]{
这是门牌号,
这个前缀,
这是StreetName,
这是StreetType,
这是街道后缀,
这是第二种类型,
这个,第二个号码,
这个城市,,
这个州,,
这个.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=新系统.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=新系统.Drawing.Size(1014421);
this.dataGridView1.TabIndex=2;
this.dataGridView1.SelectionChanged+=新的System.EventHandler(this.dataGridView1\u SelectionChanged);
//选择更改处理程序
private void dataGridView1\u SelectionChanged(对象发送方,事件参数e)
{
如果(dataGridView1.SelectedRows.Count>0)
{
_vm.SelectedAddress=(地址)dataGridView1.SelectedRows[0]。DataBoundItem;
}
}
//我的视图模型(虚拟机)
公共类ListAddressViewModel
{
公共IList地址列表{get;set;}
私人回购;
公共地址所选地址{get;set;}
公共ListAddressViewModel()
{
AddressList=新列表();
}
public ListAddressViewModel(IADRessRepository AddrRepo)
:此()
{ 
_repo=AddrRepo
init();
}
私有void init()
{
如果(_repo!=null)
{ 
地址列表=_repo.FindAll();
}
}
等
}

可能不是一个非常有用的答案,但我无法复制您的问题。我已经发布了我使用的代码,每次都得到了正确的选择。我所做的唯一更改是将datagridview1.DataSource更改为ListAddressViewModel的一个实例,而不是类型-这本身可能无法解决问题,因为这只是我输入数据的一种方式。不管怎样,这是我的代码!希望它能帮助其他人注意我在表格代码中对原始代码进行了修改,并将其注释掉

公共类地址
{
公共字符串AddressLine1{get;set;}
公共字符串City{get;set;}
公共字符串邮政编码{get;set;}
}
公共类ListAddressViewModel
{
公共IList地址列表{get;set;}
公共地址所选地址{get;set;}
公共ListAddressViewModel()
{
AddressList=新列表();
init();
}
私有void init()
{
AddressList=新列表
{
新地址{AddressLine1=“Address 1”,City=“City 1”,PostCode=“PostCode 1”},
新地址{AddressLine1=“Address 2”,City=“City 2”,PostCode=“PostCode 2”},
新地址{AddressLine1=“Address 3”,City=“City 3”,PostCode=“PostCode 3”},
新地址{AddressLine1=“Address 4”,City=“City 4”,PostCode=“PostCode 4”},
新地址{AddressLine1=“Address 5”,City=“City 5”,PostCode=“PostCode 5”},
新地址{AddressLine1=“Address 6”,City=“City 6”,PostCode=“PostCode 6”},
新地址{AddressLine1=“Address 7”,City=“City 7”,PostCode=“PostCode 7”},
新地址{AddressLine1=“Address 8”,City=“City 8”,PostCode=“PostCode 8”},
新地址{AddressLine1=“Address 9”,City=“City 9”,PostCode=“PostCode 9”},
新地址{AddressLine1=“Address 10”,City=“City 10”,PostCode=“PostCode 10”},
新地址{AddressLine1=“Address 11”,City=“City 11”,PostCode=“PostCode 11”},
新地址{AddressLine1=“Address 12”,City=“City 12”,PostCode=“PostCode 12”},
新地址{AddressLine1=“Address 13”,City=“City 13”,PostCode=“PostCode 13”},
新地址{AddressLine1=“Address 14”,City=“City 14”,PostCode=“PostCode 14”},
新地址{AddressLine1=“Address 15”,City=“City 15”,PostCode=“PostCode 15”},
新地址{AddressLine1=“Address 16”,City=“City 16”,PostCode=“PostCode 16”},
新地址
public class Address
{
    public string AddressLine1 { get; set; }
    public string City { get; set; }
    public string PostCode { get; set; }
}

public class ListAddressViewModel
{
    public IList<Address> AddressList { get; set; }
    public Address SelectedAddress { get; set; }

    public ListAddressViewModel()
    {
        AddressList = new List<Address>();
        init();
    }


    private void init()
    {
        AddressList = new List<Address>
        {
            new Address { AddressLine1 = "Address 1", City = "City 1", PostCode = "PostCode 1" },
            new Address { AddressLine1 = "Address 2", City = "City 2", PostCode = "PostCode 2" },
            new Address { AddressLine1 = "Address 3", City = "City 3", PostCode = "PostCode 3" },
            new Address { AddressLine1 = "Address 4", City = "City 4", PostCode = "PostCode 4" },
            new Address { AddressLine1 = "Address 5", City = "City 5", PostCode = "PostCode 5" },
            new Address { AddressLine1 = "Address 6", City = "City 6", PostCode = "PostCode 6" },
            new Address { AddressLine1 = "Address 7", City = "City 7", PostCode = "PostCode 7" },
            new Address { AddressLine1 = "Address 8", City = "City 8", PostCode = "PostCode 8" },
            new Address { AddressLine1 = "Address 9", City = "City 9", PostCode = "PostCode 9" },
            new Address { AddressLine1 = "Address 10", City = "City 10", PostCode = "PostCode 10" },
            new Address { AddressLine1 = "Address 11", City = "City 11", PostCode = "PostCode 11" },
            new Address { AddressLine1 = "Address 12", City = "City 12", PostCode = "PostCode 12" },
            new Address { AddressLine1 = "Address 13", City = "City 13", PostCode = "PostCode 13" },
            new Address { AddressLine1 = "Address 14", City = "City 14", PostCode = "PostCode 14" },
            new Address { AddressLine1 = "Address 15", City = "City 15", PostCode = "PostCode 15" },
            new Address { AddressLine1 = "Address 16", City = "City 16", PostCode = "PostCode 16" },
            new Address { AddressLine1 = "Address 17", City = "City 17", PostCode = "PostCode 17" },
            new Address { AddressLine1 = "Address 18", City = "City 18", PostCode = "PostCode 18" },
            new Address { AddressLine1 = "Address 19", City = "City 19", PostCode = "PostCode 19" }
        };
    }

}

public partial class Form3 : Form
{
    private System.Windows.Forms.BindingSource bindingSource1;
    private ListAddressViewModel VM { get; set; }
    private DataGridView dataGridView1;

    public Form3()
    {
        InitializeComponent();

        this.dataGridView1 = new DataGridView();
        this.VM = new ListAddressViewModel();
        this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
        //this.bindingSource1.DataSource = typeof(ListAddressViewModel);
        this.bindingSource1.DataSource = this.VM;

        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.AddressLine1,
        //    this.City,
        //    this.PostCode});
        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.Location = new System.Drawing.Point(33, 27);
        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.Size = new System.Drawing.Size(345, 150);
        this.dataGridView1.TabIndex = 2;
        this.dataGridView1.SelectionChanged += new System.EventHandler(this.dataGridView1_SelectionChanged);
        this.Controls.Add(this.dataGridView1);
    }



    //Selection Change Handler
    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        if (dataGridView1.SelectedRows.Count > 0)
        {
            var addr = (Address)dataGridView1.SelectedRows[0].DataBoundItem;
            var msg = String.Format("{0}, {1}, {2}", addr.AddressLine1, addr.City, addr.PostCode);
            MessageBox.Show(msg, "Message", MessageBoxButtons.OK);
        //    _vm.SelectedAddress = (Address)dataGridView1.SelectedRows[0].DataBoundItem;
        }
    }
}
     if (dataGridView1.SelectedRows.Count > 0)
    {
    _vm.SelectedAddress =(Address)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["Address"].Value.ToString();
    }
 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
       //e holds column and row index's selected
    }