Winform中用comboBox来选择显示Dataset中表格数据
这是一次偷懒的尝试,因为每次都必须打开代码,调试才能看见数据,发现问题。也是借鉴了调试中查看dataset数据的模式,查看不同表格。经历一番研究,总算实现了想要的效果了,故作此一笔记。与人共享。
界面
想要的效果:
代码:
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; using System.Collections; namespace FneGTAForApi { public partial class Form2 : Form { public Form2() { InitializeComponent(); // iniitem(); } DataSet ds = new DataSet() ;//全局变量 //初始化数据 public void iniitem() { // initdata(); //ArrayList mylist = new ArrayList(); //mylist.Add(new DictionaryEntry("table1", "table1")); //mylist.Add(new DictionaryEntry("table2", "table2")); //cbtable.DataSource = mylist; //cbtable.DisplayMember = "Key"; //cbtable.ValueMember = "Value"; comboBox1.BeginUpdate(); for (int i = 0; i < 1000; i++) { comboBox1.Items.Add("Item 1" + i.ToString()); } comboBox1.EndUpdate(); } //初始化数据可以从数据库中取 public DataSet initdata() { ds = CreateHotelPriceDataSet(); for (int i = 0; i <= 5;i++ ) { DataRow dr = ds.Tables["test"].NewRow(); dr["CityCode"] = "CityCode_" + i.ToString(); dr["HotelCode"] = "HotelCode_" + i.ToString(); dr["testHotelCode"] = "testHotelCode_" + i.ToString(); ds.Tables["test"].Rows.Add(dr); DataRow dr2 = ds.Tables["testtest88"].NewRow(); dr2["CityCode"] = "2CityCode_" + i.ToString(); dr2["HotelCode"] = "2HotelCode_" + i.ToString(); dr2["testHotelCode"] = "2testHotelCode_" + i.ToString(); dr2["test"] = "2test_" + i.ToString(); ds.Tables["testtest88"].Rows.Add(dr2); } return ds; } public static DataSet CreateHotelPriceDataSet() { DataSet ds = new DataSet(); DataTable dtHotelDetails = new DataTable("test"); dtHotelDetails.Columns.Add("CityCode", typeof(String)); dtHotelDetails.Columns.Add("HotelCode", typeof(String)); dtHotelDetails.Columns.Add("testHotelCode", typeof(String)); ds.Tables.Add(dtHotelDetails); DataTable RoomsInfo = new DataTable("testtest88"); RoomsInfo.Columns.Add("CityCode", typeof(String)); RoomsInfo.Columns.Add("HotelCode", typeof(String)); RoomsInfo.Columns.Add("testHotelCode", typeof(String)); RoomsInfo.Columns.Add("test", typeof(String)); // ds.Tables.Add(dtHotelDetails); ds.Tables.Add(RoomsInfo); return ds; } private void cbtable_SelectedValueChanged(object sender, EventArgs e) { //string select = cbtable.SelectedItem.ToString(); //string select2 = cbtable.SelectedValue.ToString(); // string select = cbtable.SelectedItem.ToString(); string select2 = cbtable.Text; if (select2 != "System.Collections.DictionaryEntry") { dataGridView1.DataSource = ds.Tables[select2.ToString()].DefaultView; //ds.Tables["table1"].DefaultView;//ds.Tables['"'+select2+'"'].DefaultView; } } private void button1_Click(object sender, EventArgs e) { int selectedIndex = comboBox1.SelectedIndex; Object selectedItem = comboBox1.SelectedItem; // Object selectvale = comboBox1.SelectedValue; MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "\n" + "Index: " + selectedIndex.ToString()); } //填充选择表 private void button2_Click(object sender, EventArgs e) { cbtable.Items.Clear(); ds = null; ds = initdata(); cbtable.BeginUpdate(); for (int i = 0; i < ds.Tables.Count; i++) { cbtable.Items.Add(ds.Tables[i].TableName); } cbtable.EndUpdate(); cbtable.SelectedIndex = 0; } } }
总体来说实现了想要的效果。