Results 1 to 2 of 2

Thread: [.NET 2.0+] Adding a ComboBox Column to a DataGridView

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    [.NET 2.0+] Adding a ComboBox Column to a DataGridView

    This thread provides C# code equivalents for this thread.
    Code:
    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;
    }
    Code:
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Re: [.NET 2.0+] Adding a ComboBox Column to a DataGridView

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

    bDataGridViewCombo.snippet

    Code:
    <?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>
    Last edited by MaslowB; Jun 30th, 2009 at 09:12 PM. Reason: link updated to new site. fixed a missed variable replacement
    vb.net and C# in 2008 .net 3.5
    ImaginaryDevelopment blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width