简体   繁体   中英

Adding multiple “temporary” values in a datagridview and store them in the database

Hello is there somebody here who knows how to add temporary datas into a datagridview?

I have 2 comboboxes: genderComboBox(Items: Female, Male), countryComboBox(Items: USA, Germany) 1 button (buttonAdd): 1 datagridview (genderColumn, countryColumn):

How can I add them to the datagridview? I don't want them to store directly to the database.

And How can I add multiple values of them in the gridview?

Here's what I tried

private void buttonAdd_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable(); 
            table.Columns.Add("Country");
            table.Rows.Add(countryComboBox.Text);
            dataGridView1.DataSource = table;
        }

In this solution I can see 2 problems:

  1. Everytime I click the button it always creates a new column. What I want is that I will just specify what column and fill it with datas.
  2. Every time I click the button it doesn't add what I've added before. Therefore I can't add temporary multiple values in the datagrid.

Thank You!!!

Create a list item that is a datamember of the class that contains the OnClick event. Set the DataSource to the list in the constructor. When someone clicks the 'add' button, add an anoymous object to the list using the values from the comobo boxes, then rebind the data to the datagrid. Here is a brief example.

YourClass
{
private List<object> list;

public Page_Load(object sender, args e)
{
          if (!isPostBack) // only initialize once when the page first loads
          {
              list = new List<object>();
              datagrid.datasource = list;
          }
}

protected void OnClickButton(object sender, args e)
{
     list.add(new { Gender = genderComboBox.Text, Country = countryComoBox.Text });
     datagrid.DataBind();
}
}

Edit: Here's a link for more info on anonymous types, http://msdn.microsoft.com/en-us/library/bb397696.aspx . For me they are much more flexible and dynamic for quickly populating a grid than manually creating columns, etc.

I re-wrote my example a bit after reading one of your comments to better solve your specific problem.

  1. Thats because you create a Column on each click.
  2. Thats because you create a DataTable on each click.

Put the creation code in the constructor, then the add method in the click event.

DataTable table = new DataTable(); 

the result is a new clear Datatable. Thats the reason why your old insert is missing Do it like this

DataTable table = new DataTable(); 
private void buttonAdd_Click(object sender, EventArgs e)
        {
           //insert your value at the right place
           datatable.rows.add();
           datatable.rows[datagrid.rows.count].cells[x].value = countrybox.Text;
           //x = columns index of your country column
        }

create your columns at startup, maybe form_load

datatable.columns.add("country");

You can try this:

//Create 2 datasource for combobox in grid
                DataTable genderTable= new DataTable();
                genderTable.Columns.Add(new DataColumn("Value", typeof(int)));
                genderTable.Columns.Add(new DataColumn("Name", typeof(String)));
                genderTable.Rows.Add(new object[] { 0, 'male'});
                genderTable.Rows.Add(new object[] { 1, 'female'});

                DataTable countryTable= new DataTable();
                countryTable.Columns.Add(new DataColumn("Value", typeof(int)));
                countryTable.Columns.Add(new DataColumn("Name", typeof(String)));
                countryTable.Rows.Add(new object[] { 0, 'USA'});
                countryTable.Rows.Add(new object[] { 1, 'Germany'});

//Create 2 combobox column
                 DataGridViewColumn gender= new DataGridViewColumn(new DataGridViewComboBoxCell());
                 DataGridViewColumn country= new DataGridViewColumn(new DataGridViewComboBoxCell());

//Add column into grid
                dataGridView1.Columns.Add(gender);
                dataGridView1.Columns.Add(country);
// Add rows into grid, number of rows is 10 
                dataGridView1.Rows.Add(10);

                for (int i = 0; i < 10; i++) {
                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).DataSource = genderTable;
                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).DisplayMember = "Name";
                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).ValueMember = "Value";
                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0]).Value = 0;

                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[1]).DataSource = countryTable;
                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[1]).DisplayMember = "Name";
                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[1]).ValueMember = "Value";
                    ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[1]).Value = 0;
         }

HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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