如何在C#windows应用程序中使用数据库数据填充DropDownList [英] How do I populate DropDownList with database data in C# windows application

查看:106
本文介绍了如何在C#windows应用程序中使用数据库数据填充DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用基于Windows的应用程序中的数据库中的数据填充下拉列表时遇到问题。

我知道如何在ASP.NET中执行此操作,但我找不到解决方案。 NET 4.5基于Windows。

以下是ASP.NET中的代码

I'm having problem filling the dropdownlist with data from database in a windows based application.
I know how to do that in ASP.NET, but I can't find a solution in .NET 4.5 windows based.
The following is the code in ASP.NET

public void Paraqit_ddListKlasa()
        {
            DataTable dt = new DataTable();
            SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
            sqlConn.Open();

            string SQLstringu = "Select  KlasaViti, ID_Klasa from KLASA ";
            SqlCommand cmd = new SqlCommand(SQLstringu, sqlConn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            ddListKlasa.DataSource = dt;
            ddListKlasa.DataTextField = "KlasaViti";
            ddListKlasa.DataValueField = "ID_Klasa";
            ddListKlasa.DataBind();
            ddListKlasa.Items.Insert(0, new ListItem("--Zgjedh--", "0"));
            sqlConn.Close();
        }





在页面加载中,我提供了以下代码:



In pageload I gave the following code:

if (!IsPostBack)
{

    Paraqit_ddListKlasa();

}





如何修复此问题并使其在基于Windows的应用程序中运行。

提前感谢您的回复。



How to fix this and make it work in windows based application.
Thank you in advance for your reply.

推荐答案

在InitializeComponent()之后调用Paraqit_ddListKlasa();在表单构造方法中。



例如:



call Paraqit_ddListKlasa() after InitializeComponent(); in form constructor method.

for ex:

public MyForm()
   {
       InitializeComponent();
       coutries = new List<Country>();
       OUtility = new Utility();
       coutries = OUtility.GetCoutries();
       cbCountry.DataSource = coutries;
       cbCountry.DisplayMember = "name";
       cbCountry.ValueMember = "code";
   }
// My Method
    public List<country> GetCoutries()
        {
            countryList = new List<country>();            
            string query = "SELECT Id, Code, Name FROM country";
            dataTable = objSqlUtility.ExceuteAdapter(query);
            foreach (var item in dataTable.AsEnumerable())
            {
                objCountry = new Country(Convert.ToInt32(item[0]), item[1].ToString(), item[2].ToString());
                countryList.Add(objCountry);
            }
            return countryList;
        }


与asp.net相同您可以使用Form_load事件来调用任何事件。





快速参考访问

http://www.daveoncsharp.com/2009/11/binding-a-windows-forms-combobox-in-csharp/ [ ^ ]
Same as asp.net You can use Form_load Event to call any event.


for quick reference visit
http://www.daveoncsharp.com/2009/11/binding-a-windows-forms-combobox-in-csharp/[^]


首先删除
if (!IsPostBack)



那里不会在Windows应用程序中回发。



然后在Form_Load()事件中调用你的dropdownlist / combobox populate方法。例如......






there is not post back in windows application.

then call your dropdownlist/combobox populate method at Form_Load() event. for an example...


private void Form1_Load(object sender, System.EventArgs e)
    {
Paraqit_ddListKlasa();
    }


这篇关于如何在C#windows应用程序中使用数据库数据填充DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆