# VBForums CodeBank > CodeBank - C# >  [.NET 2.0+] Adding a ComboBox Column to a DataGridView

## jmcilhinney

This thread provides C# code equivalents for this thread.

```
private void Form1_Load(object sender, EventArgs e)
{
    this.bindingSource1.DataSource = this.GetChildTable();
    this.dataGridView1.DataSource = this.bindingSource1;
}

private DataTable GetChildTable()
{
    DataTable table = new DataTable();

    table.Columns.Add("ChildID", typeof(int));
    table.Columns.Add("ParentID", typeof(int));
    table.Columns.Add("ChildName", typeof(string));

    table.Rows.Add(1, 3, "Child 1");
    table.Rows.Add(2, 2, "Child 2");
    table.Rows.Add(3, 1, "Child 3");

    return table;
}
```



```
private void Form1_Load(object sender, EventArgs e)
{
    this.bindingSource2.DataSource = this.GetParentTable();
    this.Column1.DisplayMember = "ParentName";
    this.Column1.ValueMember = "ParentID";
    this.Column1.DataSource = this.bindingSource2;

    this.bindingSource1.DataSource = this.GetChildTable();
    this.dataGridView1.DataSource = this.bindingSource1;
}

private DataTable GetParentTable()
{
    DataTable table = new DataTable();

    table.Columns.Add("ParentID", typeof(int));
    table.Columns.Add("ParentName", typeof(string));

    table.Rows.Add(1, "Parent 1");
    table.Rows.Add(2, "Parent 2");
    table.Rows.Add(3, "Parent 3");

    return table;
}

private DataTable GetChildTable()
{
    DataTable table = new DataTable();

    table.Columns.Add("ChildID", typeof(int));
    table.Columns.Add("ParentID", typeof(int));
    table.Columns.Add("ChildName", typeof(string));

    table.Rows.Add(1, 3, "Child 1");
    table.Rows.Add(2, 2, "Child 2");
    table.Rows.Add(3, 1, "Child 3");

    return table;
}
```

Note that, when you add that code, you'll also have to select the form's Load event handler in the designer.

----------


## MaslowB

Hopefully this is helpful and posts ok: here's the idea converted to a snippet:

bDataGridViewCombo.snippet



```
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Shortcut>bDataGridViewCombo</Shortcut>
      <Title>
        bDataGridViewCombo
      </Title>
    </Header>
    <Snippet>
<Declarations>
<Literal><ID>fkeyComboName</ID>
<Default>fkeyComboName</Default>
<ToolTip></ToolTip>
</Literal><Literal><ID>headerText</ID>
<Default>headerText</Default>
<ToolTip></ToolTip>
</Literal><Literal><ID>fkeyTableDisplayMember</ID>
<Default>fkeyTableDisplayMember</Default>
<ToolTip></ToolTip>
</Literal><Literal><ID>fkeyFieldName</ID>
<Default>fkeyFieldName</Default>
<ToolTip></ToolTip>
</Literal><Literal><ID>fkeyIDField</ID>
<Default>fkeyIDField</Default>
<ToolTip></ToolTip>
</Literal><Literal><ID>mainColumnName</ID>
<Default>mainColumnName</Default>
<ToolTip></ToolTip>
</Literal><Literal><ID>dg</ID>
<Default>dg</Default>
<ToolTip></ToolTip>
</Literal><Literal><ID>mainTable</ID>
<Default>mainTable</Default>
<ToolTip></ToolTip>
</Literal></Declarations>

<Code Language="csharp">
	<![CDATA[//Build fkey combobox
var $fkeyComboName$= new DataGridViewComboBoxColumn
{ // most of these are frequently string values or constants
HeaderText = $headerText$, // The column header text
DisplayMember = $fkeyTableDisplayMember$, // what field from the fkey table to display (list of names)
Name = $fkeyFieldName$, // will be the name you use when you reference the column in the dataGridView
ValueMember = $fkeyIDField$, // fkey link column typically the column in the foreign key table you join the main table on
DataPropertyName =  $mainColumnName$ // main table field you are replacing with a comboBox
};
BindingSource fkey1Binding = new BindingSource();
var Fkey1Table = db.getDs().TechObjTypes;
fkey1Binding.DataSource = Fkey1Table;
$fkeyComboName$.DataSource = fkey1Binding;

$dg$.Columns.Add($fkeyComboName$);
//Add other fkey columns here or just delete below, and use the snippet over again


//Sets up the main Table for dataGridView binding comment out or delete if using multiple fkey snippets
//best declared with a larger scope than this function so it can later be submitted to the tableAdapter for updates
/*
var mainTable = $mainTable$;
var MainBinding = new BindingSource();
MainBinding.DataSource = mainTable;

$dg$.DataSource = MainBinding;

//Must call MainBinding.EndEdit(); after updates, so it will update the MainTable
// then you can pass the mainTable to a dataAdapter to do all the updates. 
*/


]]>

      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
```

----------

