jim
jim

Reputation: 27396

C# Bind value of datagridview column to DataTable

I have a DataTable as a data source of a GridView. I'm adding a combo box the the GridView .

I'd like to be able to add a column to the DataTable that would automatically update with the value the user selects in the GridView. Can anyone help?

Upvotes: 1

Views: 3300

Answers (2)

jim
jim

Reputation: 27396

and the answer is...

DataTable myTable = getYourDataByMagic();

DataGridViewComboBoxColumn box = new DataGridViewComboBoxColumn();
BindingSource bs = new BindingSource();
bs.add("choice one");
bs.add("choice two");

box.HeaderText = "My Choice";
box.Name = "select";
box.DataSource = bs;
box.DataPropertyName = "select";

myTable.Columns.Add(new DataColumn("select"));
this.dataGridView1.Columns.Add(box);
this.dataGridView1.DataSource = myTable;

now, your "myTable" will update with the values selected in the combobox

Upvotes: 2

sleath
sleath

Reputation: 871

I'd put two grids side by side one containing all info and one that just had the blank column. I would update the datatable with the grid that contained the one column. This would be update based on selected index of the previous grid. First thing to come to mind.

Upvotes: 0

Related Questions