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:
- 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.
- 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.
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.