Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
How to combine ComboBox with DataGrid?

已答复 How to combine ComboBox with DataGrid?

  • 2012年5月14日 19:57
     
     

    Hello.

    I have such a problem. I have a combobox with names of my database tables and a datagrid.

    I want to load selected table to DataGrid? How I can do that?

    Best regards

全部回复

  • 2012年5月14日 20:37
     
      包含代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                String[] tables = { "authors", "discount", "employees", "job" };
                comboBox1.DataSource = tables;
    
                //create table and add the values
                DataTable table = new DataTable("TableName");
                table.Columns.Add("Names", typeof(String));
                foreach (String name in tables)            
                    table.Rows.Add(name);
    
                dataGridView1.DataSource = table;
    
                
    
                
            }
        }
    }
    


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/

  • 2012年5月15日 1:50
     
     已答复 包含代码

    Hi AdamBS.

    I post a sample for here,

    hope to help you.

        public partial class Form5 : Form
        {
            public Form5()
            {
                InitializeComponent();
    
                this.comboBox1.Items.Add("[Person].[ContactType]");
                this.comboBox1.Items.Add("[Person].[CountryRegion]");
                this.comboBox1.Items.Add("[Person].[StateProvince]");
    
                _builder.DataSource = ".";
                _builder.InitialCatalog = "AdventureWorks";
                _builder.IntegratedSecurity = true;
                _conn = new System.Data.SqlClient.SqlConnection(_builder.ToString());
                _adapter.SelectCommand = _conn.CreateCommand();
    
            }
    
            System.Data.SqlClient.SqlConnectionStringBuilder _builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
            System.Data.SqlClient.SqlDataAdapter _adapter = new System.Data.SqlClient.SqlDataAdapter();
            System.Data.SqlClient.SqlConnection _conn = null;
            
    
            private string CreateCmdText(string pTable)
            {
                return string.Format("select * from {0}", pTable);
            }
    
    
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                _adapter.SelectCommand.CommandText = this.CreateCmdText(this.comboBox1.SelectedItem.ToString());
                DataTable _table = new DataTable();
    
                _adapter.SelectCommand.Connection.Open();
    
                _adapter.Fill(_table);
    
                _adapter.SelectCommand.Connection.Close();
    
                this.dataGridView1.DataSource = _table;
                
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                
            }
        }

    the runtime screenshot bellow.


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    • 已标记为答案 AdamBS 2012年5月16日 18:12
    •  
  • 2012年5月15日 4:10
     
      包含代码

    Thank you for this example, but I think that I was looking for something different.

    Here I post part of my application code. I'm making a WPF application and I'm using a DataSet.

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using BBD1; using System.Data; namespace BBD1 { /// <summary> /// Interaction logic for Widok.xaml /// </summary> public partial class Widok : Window { //BBDBaza it's the name of my DataSet BBDBaza bbd = new BBDBaza(); public Widok() { InitializeComponent(); //Here I'm loading single table to DataGrid. It worked well.

    BBDBazaTableAdapters.CONV_FACTableAdapter conv_ta = new BBDBazaTableAdapters.CONV_FACTableAdapter(); conv_ta.Fill(bbd.CONV_FAC); this.DataContext = bbd.CONV_FAC.DefaultView; //Below, I'm populating ComboBox with database tables names. DataTableCollection nTab = bbd.Tables; foreach (DataTable nazwaTab in nTab) { cboLista.Items.Add(nazwaTab.TableName); } } private void Window_Loaded(object sender, RoutedEventArgs e) { } private void cboLista_SelectedIndexChanged(object sender, RoutedEventArgs e) { } } }



    • 已编辑 AdamBS 2012年5月15日 4:11
    •  
  • 2012年5月15日 4:44
    版主
     
     

    Adam, did you try Matthew's suggestion? Use DataAdapters instead of TableAdapters. The problem with trying to use a TableAdapter is that there's no easy way to know which one you need. That won't be a problem if you use a DataAdapter instead, as Matthew showed you how to do.


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • 2012年5月15日 20:39
     
      包含代码

    I have problem with finding this method:

    CreateCmdText

    My compiler doesn't see it. I'm using C# with Visual Studio 2010.

    P.S. I didn't mentioned that I'm using DataSet but I don't know if this change something in Matthew's exapmle?

  • 2012年5月15日 22:52
    版主
     
     
    CreateCmdText was a method in Matthew's code sample ... you must have missed that.

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • 2012年5月16日 8:22
     
     
    Yes I did :-). I missed that. Sorry
  • 2012年5月16日 18:16
     
     

    Ok, I have used Matthew's code and everything is working just I wanted to do. Thank you very much for your help. I own you a beer :-).

  • 2012年5月21日 20:05
    版主
     
     
    hahaha ... actually you owe Matthew a beer, not me! But, I'll take a nice Guinness anytime you want to buy me one. ;0)

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • 2012年5月21日 20:33
     
     
    hehe!

    John Grove, Senior Software Engineer http://www.digitizedschematic.com/