SQL Server how add ListBox new data to data grid view without delete old data C#?

ffx8fchx  于 2023-03-22  发布在  C#
关注(0)|答案(1)|浏览(149)

I'm trying to first add textbox values to add listed by listbox. after that listbox items adding to datagridview. Its fine. i did this. But i'm facing problem is first time I added listbox lists to datagridview. but second time when I add diffrent data to datagridview, first time added data gone. new data only availbale. Anyone know solutuion.? Because i'm trying to check datagridview rows count, after add, But its not working properley. anyone please...

This is my Interface. when I clicked Serial no generate button, Textboxes with Combobox value adding to listboxes. after when I export to datasheet button clicked, adding datas to datagridview. but again I change the list datagridview old data olso change. please help...

This is code currently.......

//Function of DataLoad Listboxes to Datagridview---------->
   public void Data_ID_Load()
        {
            string cmbItemName = cmbitemname.SelectedItem.ToString();
            string cmbItemID = txtitemsku.Text;

            DataTable dt = new DataTable();
            dt.Columns.Add("Label ID", typeof(string));//column 0
            dt.Columns.Add("Serial Number", typeof(string));//column 1  
            dt.Columns.Add("Item Name", typeof(string));//column 2  
            dt.Columns.Add("Item ID", typeof(string));//column 3  
            for (int i = 0; i < lstlabelid.Items.Count; i++)
            {

                //dt.Rows.Add(lstlabelid.Items[i].ToString(), lstserialno.Items[i].ToString(), cmbItemName, cmbItemID);
                dt.Rows.Add(new Object[] { lstlabelid.Items[i].ToString(), lstserialno.Items[i].ToString(), cmbItemName, cmbItemID });
            }
            gridview.DataSource = dt;

        }

    private void btnexport_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbllabelid.Text == "" || lblserialno.Text == "")
                {
                    MetroMessageBox.Show(this, "Please Make First Label ID and Serial No", "QR Print Submission Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                else
                {
                    Data_ID_Load();
                }
            }
            catch (Exception ex)
            {
                MetroMessageBox.Show(this, ex.Message, "Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
qxgroojn

qxgroojn1#

In your sample code, there is only one gridview.DataSource = dt in the load event; no other data input is found.

You should keep data in dt while adding new data to dt.

To update the data source of datagridview, you need to set it to null first, and then reproduce the binding.

Here's an example of what I wrote:

using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApp1
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

        public List<Person> Persons { get; set; }

        private void Form1_Load(object sender, System.EventArgs e)
        {

            this.Persons = new List<Person>();
            Persons.Add(new Person { Name = "Tom", Age = 20 });
            Persons.Add(new Person { Name = "Jerry", Age = 25 });
            dataGridView1.DataSource = Persons;
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            Persons.Add(new Person() { Age = 20, Name = "abc" });
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = Persons;
        }
    }
}

In fact, databinding in wpf is easier to use.

相关问题