Valentak

Hi,
I am starting with C# and I can't solve this problem. Colul you help me please

I need create only one row of cells dynamically (the number of cells is changing for each creation of the form where is the datagridview). Each cell can be integral type or choice of enum (combobox in datagridview), depend on other parametres in program. How to create datatable or databinding for this case And how can I restore data in datagridview after loading saved data

Thank you very much,



Re: Windows Forms Data Controls and Databinding Connect dataset with comboboxdatagridview

Rong-Chun Zhang - MSFT

Hi Valentak,

I am not sure what you want, did you want to add a ComboBox column to your DataGridView If so, check my sample below:

Code Block

namespace NewDGV

{

public partial class Form33 : Form

{

public Form33()

{

InitializeComponent();

}

DataTable pdt = new DataTable();

DataTable cdt = new DataTable();

private void Form33_Load(object sender, EventArgs e)

{

pdt.Columns.Add("order");

pdt.Columns.Add("productid");

pdt.Rows.Add("01","004");

pdt.Rows.Add("02","001");

pdt.Rows.Add("03","001");

pdt.Rows.Add("04","003");

pdt.Rows.Add("05","002");

cdt.Columns.Add("productid");

cdt.Columns.Add("name");

cdt.Rows.Add("001", "aaa");

cdt.Rows.Add("002", "bbb");

cdt.Rows.Add("003", "ccc");

cdt.Rows.Add("004", "ddd");

this.dataGridView1.DataSource = pdt;

this.dataGridView1.Columns.RemoveAt(1);

DataGridViewComboBoxColumn dgvcob = new DataGridViewComboBoxColumn();

dgvcob.HeaderText = "Product";

dgvcob.DataPropertyName = "productid";

dgvcob.DisplayMember = "name";

dgvcob.ValueMember = "productid";

dgvcob.DataSource = cdt;

dgvcob.DefaultCellStyle.NullValue = "Choose a value";

this.dataGridView1.Columns.Add(dgvcob);

}

}

}

Please feel free to let me know if this helps.

Hope this helps.
Best regards.
Rong-Chun Zhang






Re: Windows Forms Data Controls and Databinding Connect dataset with comboboxdatagridview

Valentak

Thank you for your help. Maybe I bad explain what I want. I know how to create datagridview with combobox cells. I don't know how to bind these cells with datatable in dataset, or better how to create datatable in data set, to be created combobox cells in datagridview automaticly.

Thank you wery much.





Re: Windows Forms Data Controls and Databinding Connect dataset with comboboxdatagridview

Rong-Chun Zhang - MSFT

Hi Valentak,

I am afraid that you cannot create ComboBox column in DataGridView automatically. The ComboBoxColumn is not designed to automatically generate when binding. You will need to create the ComboBoxColumn manually either using the designer or by code.

Hope this helps.
Best regards.
Rong-Chun Zhang