简体   繁体   English

如何保存datagridview的输入?

[英]How to save the input of datagridview?

Take a look at my Picture. 看看我的照片。 I wrote dddd ddd in few columns. 我在几栏中写了dddd ddd。 I want to save this row automatically by clicking on "add" button 我想通过单击“添加”按钮自动保存该行

or in another words: 或者换句话说:

After clicking on ADD button i expect the program to save the input but it doesnt work. 单击添加按钮后,我希望程序保存输入,但不起作用。

Im using Visual Studio - Windows Froms c#. 我正在使用Visual Studio-Windows Froms c#。 Whats wrong with my code? 我的代码有什么问题? Any ideas? 有任何想法吗? What am i missing? 我想念什么? Thanks for help. 感谢帮助。

在此处输入图片说明

    private void button1_Click(object sender, EventArgs e)
    {
        dataGridView1.Rows.Clear();
        dataGridView1.Visible = true;
        btn_Add.Visible = true;
        button1.Visible = false;
        label1.Visible = true;
        label2.Visible = true;

        textBox2.Visible = true;


        booklist = new List<Book>();
        booklist.Add(new Book("001", "Lord Of the Rings", "J. R. R. Tolkien", "5", "0 Books Avaible"));
        booklist.Add(new Book("002", "The Hobbit", "J. R. R. Tolkien", "5", "2 Books Avaible"));
        booklist.Add(new Book("003", "The Lion, the Witch and the Wardrobe", "C. S. Lewis", "5", "1 Books Avaible"));
        booklist.Add(new Book("004", "The Alchemist", "Paulo Coelho", "5", "3 Books Avaible"));
        booklist.Add(new Book("005", "Think and Grow Rich", "Napoleon Hill", "5", "4 Books Avaible"));
        booklist.Add(new Book("006", "The Da Vinci Code", "Dan Brown", "5", "3 Books Avaible"));
        booklist.Add(new Book("007", "And Then There Were None", "Agatha Christie", "5", "5 Books Avaible"));
        booklist.Add(new Book("008", "She: A History of Adventure", "H. Rider Haggard", "5", "0 Books Avaible"));
        booklist.Add(new Book("009", "Dream of the Red Chamber", "Cao Xueqin", "5", "5 Books Avaible"));
        booklist.Add(new Book("010", "The Catcher in the Rye", "J. D. Salinger", "5", "5 Books Avaible"));
        booklist.Add(new Book("011", "The Little Prince", "Antoine de Saint-Exupéry", "5", "5 Books Avaible"));
        booklist.Add(new Book("012", "Lolita", "Vladimir Nabokov", "5", "3 Books Avaible"));
        booklist.Add(new Book("013", "The Name of the Rose", "Umberto Eco", "5", "1 Books Abaible"));
        booklist.Add(new Book("014", "Black Beauty", "Anna Sewell", "5", "2 Books Avaible"));
        booklist.Add(new Book("015", "Charlotte's Web", "E.B. White", "5", "0 Books Avaible"));
        booklist.Add(new Book("016", "Harry Potter and the Goblet of Fire", "J. K. Rowling", "5", "2 Books Avaible"));
        booklist.Add(new Book("017", "Harry Potter and the Prisoner of Azkaban", "J. K. Rowling", "5", "1 Books Avaible"));
        booklist.Add(new Book("018", "Harry Potter and the Chamber of Secrets", "J. K. Rowling", "5", "4 Books Avaible"));
        booklist.Add(new Book("019", "Harry Potter and the Half-Blood Prince", "J. K. Rowling", "5", "3 Books Avaible"));
        booklist.Add(new Book("020", "Harry Potter and the Philosopher's Stone", "J. K. Rowling", "5", "3 Books Avaible"));
        booklist.Add(new Book("021", "Harry Potter and the Order of the Phoenix", "J. K. Rowling", "5", "4 Books Avaible"));
        booklist.Add(new Book("022", "Harry Potter and the Deathly Hallows", "J. K. Rowling", "5", "3 Books Avaible"));
        booklist.Add(new Book("023", "", "", "", ""));
        booklist.Add(new Book("024", "", "", "", ""));
        booklist.Add(new Book("025", "", "", "", ""));
        booklist.Add(new Book("026", "", "", "", ""));
        dataGridView1.Columns.Clear();

        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = booklist;


    }

    private void btn_Add_Click(object sender, EventArgs e)
    {
        btn_Add.Visible = false;
        button3.Visible = true;
        dataGridView1.Visible = true;
        dataGridView1.AllowUserToAddRows = true;


    }



    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        var textbox = (TextBox)sender;

        if (string.IsNullOrEmpty(textbox.Text))
        {
            dataGridView1.DataSource = booklist;
            return;
        }

        var search = booklist.Where(b => b.Name.Contains(textbox.Text)).ToList();
        dataGridView1.DataSource = search;

        Console.WriteLine(e.ToString()); 
    }

As it is shown in documentation example, you have to create BindingSource . 如文档示例所示, 您必须创建BindingSource List cannot serve as BindingSource ( see that SO question ) List不能用作BindingSource请参阅该SO问题

private BindingSource bindingSource1;
....
bindingSource1 = new BindingSource();
bindingSource1.DataSource = booklist;
dataGridView1.DataSource = bindingSource1;

Clicking the Add button should finish editing mode if the current string is edited: 如果当前字符串已被编辑,则单击添加按钮应完成编辑模式:

datagridview1.EndEdit();
datagridview1.Refresh();

The other solution from SO question is BindingList SO问题的另一个解决方案是BindingList

private List<Book> booklist; // your book list as usual
private BindingList<Book> booklistUI; // add this list
...
//at the end of button1_Click
booklistUI = new BindingList<Book>(booklist);
dataGridView1.DataSource = booklistUI;

A manually way, I hope you get the idea. 希望您能手动操作。

Method: 方法:

    public void Add()
    {         
        List<Book> bookList = new List<Book>();

        for (int i = 0; i < dataGridView.Rows.Count; i++)
        {
            bookList.Add(new Book()
            {
                Id = (string)dataGridView.Rows[i].Cells["Id"].Value,
                Name = (string)dataGridView.Rows[i].Cells["Name"].Value,
                Author = (string)dataGridView.Rows[i].Cells["Author"].Value,
                NumberOfBooks = (string)dataGridView.Rows[i].Cells["NumberOfBooks "].Value,
                Available = (string)dataGridView.Rows[i].Cells["Available"].Value
            });
        }

       //You can do anything now
    }

Add click event: 添加点击事件:

private void btn_Add_Click(object sender, EventArgs e)
{
    btn_Add.Visible = false;
    button3.Visible = true;
    dataGridView1.Visible = true;
    dataGridView1.AllowUserToAddRows = true;
    Add();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM