English 中文(简体)
在选定浏览量变化时,数据综合指数-1没有价值
原标题:DataGrid gets Index -1 does not have a value when selected row changes
  • 时间:2012-01-14 13:17:17
  •  标签:
  • c#
  • winforms

I have a simple datagrid, that displays contents of some List variable. I have two buttons related to it, one for adding contents to the List variable and refreshing the datagrid, the other for removing it and refreshing also.

然而,如果我改变所选择的行文,我就犯了一个错误,说“Index 1没有价值” 没有任何理由。

建立数据网的代码如下:

allTravellersDataGrid.DataSource = allTravellers;

名单上所有流浪者都在上面谈论。

将内容列入所有游轮名单的序号如下:

private void addAttendee_Click(object sender, EventArgs e)
    {
        if (attendeeName.Text == "" || attendeeSurname.Text == "" || attendeeBirthDate.Text == "" || attendeeIdNumber.Text == "")
        { MessageBox.Show("Not all information regarding the attendee entered"); }
        else
        {
            allTravellers.Add(dt.prepareTraveller(attendeeName.Text, attendeeSurname.Text, attendeeBirthDate.Text, attendeeIdNumber.Text));
            allTravellersDataGrid.DataSource = null;
            allTravellersDataGrid.DataSource = allTravellers;
            allTravellersDataGrid.Refresh();
        }
    }

and at last the code for button for removal of objects from the list looks like this:

private void removeAttendee_Click(object sender, EventArgs e)
    {
        traveller travellerToRemove = (traveller)allTravellersDataGrid.CurrentRow.DataBoundItem;
        allTravellers.Remove(travellerToRemove);
        allTravellersDataGrid.Refresh();
    }

是否有任何人像我这样无助地发现什么造成指数问题?

感谢。

问题回答

我怀疑,当你发现这一错误时,你目前还没有选定项目。 (没有选定物品的分子控制表明有1-1。) 在你试图去除之前检查一个有效项目,可能解决你的问题。

I also used a BindingList, not sure what difference it will make for you.

assume a form with a textbox named attendeeName add button named addBtn remove button named removeBtn grid named allTravellersDataGrid

这项工作:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using StackOverFlowWinForms.Model;

namespace StackOverFlowWinForms
{
public partial class Form1 : Form
{
    private BindingList<Traveller> _allTravellers = new BindingList<Traveller>();
    public BindingList<Traveller> allTravellers { get { return _allTravellers; } }

    public Form1()
    {
        InitializeComponent();

        allTravellers.Add(new Traveller("Fred"));
        allTravellers.Add(new Traveller("George"));
        allTravellers.Add(new Traveller("Sam"));
        allTravellers.Add(new Traveller("Mary"));

        this.allTravellersDataGrid.DataSource = allTravellers;

    }

    private void addBtn_Click(object sender, EventArgs e)
    {
        if (attendeeName.Text == "")
        { MessageBox.Show("Not all information regarding the attendee entered"); }
        else
        {
            allTravellers.Add(new Traveller(attendeeName.Text));
        }

    }

    private void removeBtn_Click(object sender, EventArgs e)
    {
        if (allTravellersDataGrid.CurrentRow != null) 
        {
            Traveller travellerToRemove = (Traveller)allTravellersDataGrid.CurrentRow.DataBoundItem;
            allTravellers.Remove(travellerToRemove);

        }

    }
}
}

using System;
using System.ComponentModel;

namespace StackOverFlowWinForms.Model
{
public class Traveller 

{

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    #endregion 

    private string _attendeeName;
    public string attendeeName 
    { 
        get 
        {
            return _attendeeName; 
        } 
        set 
        {
            _attendeeName = value; 
            NotifyPropertyChanged("attendeeName"); 
        } 
    }


    public Traveller()
    {
        this.attendeeName = "Unknown";
    }
    public Traveller(string name)
    {
        this.attendeeName = name;
    }
}
}




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签