我需要一个Common函数来使用WPF中的存储过程来填充组合框

时间:2015-10-31 10:52:19

标签: c# wpf combobox

我需要一个通用方法从数据库中填充带有默认值的组合框。

1 个答案:

答案 0 :(得分:0)

尝试这种方法......我认为这会对你有帮助

    public bool fillComboBox(System.Windows.Controls.ComboBox combobox, string procedureName, string dTable, string dDisplay, string dValue, string defaultValue)
    {
        SqlCommand sqlcmd = new SqlCommand();
        SqlDataAdapter sqladp = new SqlDataAdapter();
        DataSet ds = new DataSet();
        try
        {
            using (SqlConnection _sqlconTeam = new SqlConnection(DBCon.conStr))
            {
                sqlcmd.Connection = _sqlconTeam;
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.CommandText = procedureName;
                _sqlconTeam.Open();
                sqladp.SelectCommand = sqlcmd;
                sqladp.Fill(ds, dTable);
                DataRow nRow = ds.Tables[dTable].NewRow();
                nRow[dDisplay] = defaultValue;
                nRow[dValue] = "-1";
                ds.Tables[dTable].Rows.InsertAt(nRow, 0);
                combobox.ItemsSource = ds.Tables[dTable].DefaultView;
                combobox.DisplayMemberPath = ds.Tables[dTable].Columns[1].ToString();
                combobox.SelectedValuePath = ds.Tables[dTable].Columns[0].ToString();
                combobox.SelectedIndex = 0;

            }
            return true;
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
            return false;
        }
        finally
        {
            sqladp.Dispose();
            sqlcmd.Dispose();
        }
    }