using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace BDTC_SysTem
{
//
//填充各种Combbox选择项工具,取到SQL语句,输出对应内容
//
public class FillCombboxClass
{
// protected static string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
protected static string connectionString = Properties.Settings.Default.bdtcConnectionString;
public FillCombboxClass()
{
// TODO: Complete member initialization
}
public static DataTable SelectContent( string SQLString)
{
DataTable dt = null;
using(SqlConnection conn=new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, conn))
{
try
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null && reader.HasRows)
{
dt = new DataTable();
dt.Load(reader);
}
return dt;
}
catch (System.Data.SqlClient.SqlException e)
{
conn.Close();
throw new Exception(e.Message);
}
}
}
}
}
}
下面是绑定到控件。
namespace BDTC_SysTem
{
public partial class frmProductionShow : Form
{
public frmProductionShow()
{
InitializeComponent();
}
private void pro_ProductionTableBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.pro_ProductionTableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.bdtcDataSet);
}
private void frmProductionShow_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“bdtcDataSet.Pro_FactoryTable”中。您可以根据需要移动或删除它。
this.pro_FactoryTableTableAdapter.Fill(this.bdtcDataSet.Pro_FactoryTable);
// TODO: 这行代码将数据加载到表“bdtcDataSet.Pro_ClassTable”中。您可以根据需要移动或删除它。
this.pro_ClassTableTableAdapter.Fill(this.bdtcDataSet.Pro_ClassTable);
// TODO: 这行代码将数据加载到表“bdtcDataSet.Pro_ProductionTable”中。您可以根据需要移动或删除它。
this.pro_ProductionTableTableAdapter.Fill(this.bdtcDataSet.Pro_ProductionTable);
try
{
FillSelectCombbox();
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
private void FillSelectCombbox()
{
string strSql = "Select proclassid,proclassname from pro_classTable Where ProClassFather=0 order by proclassid ";
MessageBox.Show(strSql);
DataTable ds = FillCombboxClass.SelectContent(strSql);
CombboxProClassSearch.ComboBox.DataSource = ds;
CombboxProClassSearch.ComboBox.DisplayMember = ds.Columns[1].ColumnName;
CombboxProClassSearch.ComboBox.ValueMember = ds.Columns[0].ColumnName;
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
string ProductionName, ProductionPinYin;
if ((TextProductionNameSearch.Text!=string.Empty) || (TextPinYinSearch.Text!=string.Empty) || (CombboxProClassSearch.SelectedIndex != -1))
{
if (TextProductionNameSearch.Text != string.Empty) { ProductionName = TextProductionNameSearch.Text; } else { ProductionName = DBNull.Value.ToString(); }
if (TextPinYinSearch.Text != string.Empty) ProductionPinYin = TextPinYinSearch.Text; else ProductionPinYin = DBNull.Value.ToString();
this.pro_ProductionTableTableAdapter.FillByNPC(this.bdtcDataSet.Pro_ProductionTable, ProductionName, ProductionPinYin, int.Parse(CombboxProClassSearch.ComboBox.SelectedValue.ToString()));
}
}
}
}
附上在论坛上的链接
http://topic.csdn.net/u/20120718/10/0de1b9ec-8f3c-44bf-86d0-8dd0221a3ae0.html?4631