如何直接在Windows Forms App中编辑/添加/更新datagridview

时间:2019-05-14 23:42:08

标签: c# datagridview

我正在制作一个管理酒店的Windows Forms应用程序。它具有“客户”,“房间”,“入住”类别。客户端和会议室具有一个ArrayList,该数组在运行时由.txt文件填充,然后显示在clientListView和roomDataGridView中。

因此,我有这行代码来填充roomDGV:

roomsDGV.DataSource = roomsArrayList;

对于roomsDGV,我试图通过单击roomsDGV来添加新行,例如未绑定数据时。我还在编辑后或编辑时尝试编辑行并将其保存到txt文件。我可以根据需要发布更多代码,但不确定当前显示更多代码是否有帮助。最后,我正在尝试一种功能,以便可以突出显示列表中的一个客户端,然后单击roomsDGV中的一行,然后将该clientID分配给该会议室或类似的方式。

在加载时,datagridview已从arrayList加载并正确格式化,但是我似乎遇到了能够编辑datagridview的问题。当我单击以下行之一时,它给了我这个错误:

System.IndexOutOfRangeException: 'Index -1 does not have a value.'

这源自Application.Run(new HotelManager());

这是表格:

public partial class HotelManager : Form
{
    // VARIABLES
    string clientID;
    // FILEPATHS
    string clientsTxt = "Clients.txt";
    string occupanciesTxt = "Occupancies.txt";
    string roomsTxt = "Rooms.txt";
    string clientsDat = "Clients.dat";

    // ARRAYLIST FOR ROOMS and CLIENTS
    ArrayList roomsArrayList = new ArrayList();
    ArrayList clientsArrayList = new ArrayList();

    //STACKS AND QUEUES INIT
    // Load occupancies into stack > pop
    Stack roomStack = new Stack();
    Queue vacancyQueue = new Queue();

    // RANDOM for ID
    private readonly Random rand = new Random();

    public HotelManager()
    {
        InitializeComponent();
    }

    private void HotelManager_Load(object sender, EventArgs e)
    {
        roomsDGV.DataSource = roomsArrayList;
        // LOAD clients
        // LoadClients();
        RefreshClientList();
        // LOAD rooms
        LoadRooms();
    }
    private void NewClientButton_Click(object sender, EventArgs e)
    {
        AddClient();
    }
    private void checkInButton_Click(object sender, EventArgs e)
    {
        string clientID = clientList.SelectedItems[0].Text;
        string[] text = File.ReadAllLines(occupanciesTxt);
        foreach (string s in text)
        {
            if (s.Contains(clientID))
            {
                var replace = s;
                Console.WriteLine(s);
                replace = replace.Replace("false", "true");
            }
        }
        File.WriteAllLines(occupanciesTxt, text);
    }

    // METHODS

    private void AddClient()
    {
        //COLLECT DATA > CREATE NEW client > SHOW IN **PROGRAM/DataGridView** > add to clients file
        // ID GENERATION > CHECKS AGAINST clientsTXT
        clientID = rand.Next(0, 999999).ToString();
        if (File.ReadLines(clientsTxt).Contains(clientID))
        {
            clientID = rand.Next(0, 999999).ToString();
        }
        Client client = new Client(clientID, firstNameBox.Text, lastNameBox.Text);
        try
        {
            if (!string.IsNullOrWhiteSpace(phoneNumBox.Text))
            {
                client.PhoneNumber = Convert.ToInt64(phoneNumBox.Text);
            }
            if (!string.IsNullOrWhiteSpace(addressBox.Text))
            {
                client.Address = addressBox.Text;
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Please use the correct format!");
            throw;
        }
        clientsArrayList.Add(client);
        using (StreamWriter file =
        new StreamWriter("Clients.txt", true))
        {
            file.WriteLine(client.ToString());
        }
        RefreshClientList();
        // TEST CODE // SERIALIZATION TO .DAT
        SerializeClientData(client);
    }
    private void LoadClients()
    {
        // LOADS arrayList FROM .txt FILE
        List<string> clientList = File.ReadAllLines(clientsTxt).ToList();
        foreach (var c in clientList)
        {
            Client client = new Client(c);
            clientsArrayList.Add(client);
        }
    }

    private void LoadRooms()
    {
        List<string> roomsList = File.ReadAllLines(roomsTxt).ToList();
        foreach (var r in roomsList)
        {
            var roomDetails = r.Split('|');
            if (r.Contains("BASIC"))
            {
                BasicRoom basic = new BasicRoom();
                basic.RoomNumber = roomDetails[0];
                basic.NumberOfBeds = Convert.ToInt32(roomDetails[1]);
                basic.Balcony = Convert.ToBoolean(roomDetails[2]);
                basic.DownForRepair = Convert.ToBoolean(roomDetails[3]);
                basic.Smoking = Convert.ToBoolean(roomDetails[4]);
                roomsArrayList.Add(basic);

            }
            else if (r.Contains("SUITE"))
            {
                Suite suite = new Suite();
                suite.RoomNumber = roomDetails[0];
                suite.NumberOfBeds = Convert.ToInt32(roomDetails[1]);
                suite.Balcony = Convert.ToBoolean(roomDetails[2]);
                suite.DownForRepair = Convert.ToBoolean(roomDetails[3]);
                suite.NumberOfRooms = Convert.ToInt32(roomDetails[4]);
                roomsArrayList.Add(suite);
            }
        }
        roomStack = new Stack(roomsArrayList);
        foreach (var item in roomStack)
        {
            Console.WriteLine(item);
        }
    }

    private void RoomsDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void RoomsDGV_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {

    }
}

到目前为止,我已经浏览了所有属性,但是似乎找不到合适的属性。我知道我可以添加/使用组合框等将新项目添加到arrayList中,但是我正在尝试使用datagridview功能

我希望编辑行并将其添加到DGV,但是设计器中的某些内容阻止了我? 这是DGV,单击任何行都会将其断开。 https://imgur.com/a/GG7ZwdV